├── myproject ├── __init__.py ├── core │ ├── __init__.py │ ├── migrations │ │ ├── __init__.py │ │ ├── 0002_auto_20200505_0338.py │ │ └── 0001_initial.py │ ├── tests.py │ ├── apps.py │ ├── urls.py │ ├── admin.py │ ├── models.py │ ├── views.py │ └── templates │ │ ├── base.html │ │ ├── includes │ │ └── nav.html │ │ └── index.html ├── urls.py ├── wsgi.py └── settings.py ├── requirements.txt ├── contrib └── env_gen.py ├── fix └── products.csv ├── manage.py ├── .gitignore └── README.md /myproject/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /myproject/core/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /myproject/core/migrations/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /myproject/core/tests.py: -------------------------------------------------------------------------------- 1 | from django.test import TestCase 2 | 3 | # Create your tests here. 4 | -------------------------------------------------------------------------------- /myproject/core/apps.py: -------------------------------------------------------------------------------- 1 | from django.apps import AppConfig 2 | 3 | 4 | class CoreConfig(AppConfig): 5 | name = 'core' 6 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | dj-database-url==0.5.0 2 | Django==2.2.27 3 | django-extensions==2.2.9 4 | django-widget-tweaks==1.4.8 5 | python-decouple==3.3 6 | pytz==2019.3 7 | six==1.14.0 8 | sqlparse==0.3.1 9 | -------------------------------------------------------------------------------- /myproject/urls.py: -------------------------------------------------------------------------------- 1 | from django.urls import include, path 2 | from django.contrib import admin 3 | urlpatterns = [ 4 | path('', include('myproject.core.urls', namespace='core')), 5 | path('admin/', admin.site.urls), 6 | ] 7 | -------------------------------------------------------------------------------- /myproject/core/urls.py: -------------------------------------------------------------------------------- 1 | from django.urls import path 2 | from myproject.core import views as v 3 | 4 | 5 | app_name = 'core' 6 | 7 | 8 | urlpatterns = [ 9 | path('', v.index, name='index'), 10 | path('api/products/', v.products, name='products'), 11 | path('api/categories/', v.categories, name='categories'), 12 | ] 13 | -------------------------------------------------------------------------------- /myproject/core/admin.py: -------------------------------------------------------------------------------- 1 | from django.contrib import admin 2 | from .models import Category, Product 3 | 4 | 5 | @admin.register(Product) 6 | class ProductAdmin(admin.ModelAdmin): 7 | list_display = ('__str__', 'category',) 8 | search_fields = ('title',) 9 | list_filter = ('category',) 10 | 11 | 12 | admin.site.register(Category) 13 | -------------------------------------------------------------------------------- /myproject/wsgi.py: -------------------------------------------------------------------------------- 1 | """ 2 | WSGI config for myproject project. 3 | 4 | It exposes the WSGI callable as a module-level variable named ``application``. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/2.2/howto/deployment/wsgi/ 8 | """ 9 | 10 | import os 11 | 12 | from django.core.wsgi import get_wsgi_application 13 | 14 | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproject.settings') 15 | 16 | application = get_wsgi_application() 17 | -------------------------------------------------------------------------------- /contrib/env_gen.py: -------------------------------------------------------------------------------- 1 | """ 2 | Django SECRET_KEY generator. 3 | """ 4 | from django.utils.crypto import get_random_string 5 | 6 | 7 | chars = 'abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*(-_=+)' 8 | 9 | CONFIG_STRING = """ 10 | DEBUG=True 11 | SECRET_KEY=%s 12 | ALLOWED_HOSTS=127.0.0.1, .localhost 13 | """.strip() % get_random_string(50, chars) 14 | 15 | # Writing our configuration file to '.env' 16 | with open('.env', 'w') as configfile: 17 | configfile.write(CONFIG_STRING) 18 | -------------------------------------------------------------------------------- /fix/products.csv: -------------------------------------------------------------------------------- 1 | title,price,category 2 | suco de laranja,6,Bebida 3 | refrigerante,5.5,Bebida 4 | água mineral sem gás,4.5,Bebida 5 | pudim de leite,4,Sobremesa 6 | gelatina,1.5,Sobremesa 7 | ambrosia,5,Sobremesa 8 | X-salada,7,Lanche 9 | X-bacon,7,Lanche 10 | X-egg,6,Lanche 11 | X-tudo,10,Lanche 12 | banana,3,Fruta 13 | manga,3,Fruta 14 | melancia,5,Fruta 15 | uva,5,Fruta 16 | morango,5,Fruta 17 | picadinho,15,Refeição 18 | bife com ovo,15,Refeição 19 | frango ao molho,15,Refeição 20 | frango assado,17,Refeição 21 | costela cozida,20,Refeição 22 | costela assada,22,Refeição 23 | prato light,20,Refeição 24 | -------------------------------------------------------------------------------- /myproject/core/migrations/0002_auto_20200505_0338.py: -------------------------------------------------------------------------------- 1 | # Generated by Django 2.2.12 on 2020-05-05 03:38 2 | 3 | from django.db import migrations, models 4 | import django.db.models.deletion 5 | 6 | 7 | class Migration(migrations.Migration): 8 | 9 | dependencies = [ 10 | ('core', '0001_initial'), 11 | ] 12 | 13 | operations = [ 14 | migrations.AlterField( 15 | model_name='product', 16 | name='category', 17 | field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='categories', to='core.Category', verbose_name='categoria'), 18 | ), 19 | ] 20 | -------------------------------------------------------------------------------- /manage.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | """Django's command-line utility for administrative tasks.""" 3 | import os 4 | import sys 5 | 6 | 7 | def main(): 8 | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproject.settings') 9 | try: 10 | from django.core.management import execute_from_command_line 11 | except ImportError as exc: 12 | raise ImportError( 13 | "Couldn't import Django. Are you sure it's installed and " 14 | "available on your PYTHONPATH environment variable? Did you " 15 | "forget to activate a virtual environment?" 16 | ) from exc 17 | execute_from_command_line(sys.argv) 18 | 19 | 20 | if __name__ == '__main__': 21 | main() 22 | -------------------------------------------------------------------------------- /myproject/core/models.py: -------------------------------------------------------------------------------- 1 | from django.db import models 2 | 3 | 4 | class Category(models.Model): 5 | title = models.CharField('categoria', max_length=50, unique=True) 6 | 7 | class Meta: 8 | verbose_name = 'categoria' 9 | verbose_name_plural = 'categorias' 10 | 11 | def __str__(self): 12 | return self.title 13 | 14 | 15 | class Product(models.Model): 16 | title = models.CharField('título', max_length=100, unique=True) 17 | price = models.DecimalField('preço', max_digits=8, decimal_places=2) 18 | category = models.ForeignKey( 19 | Category, 20 | verbose_name='categoria', 21 | related_name='categories', 22 | on_delete=models.SET_NULL, 23 | null=True, 24 | blank=True 25 | ) 26 | 27 | class Meta: 28 | ordering = ('title',) 29 | verbose_name = 'produto' 30 | verbose_name_plural = 'produtos' 31 | 32 | def __str__(self): 33 | return self.title 34 | -------------------------------------------------------------------------------- /myproject/core/views.py: -------------------------------------------------------------------------------- 1 | from django.db.models import Count 2 | from django.http import JsonResponse 3 | from django.shortcuts import render 4 | from .models import Product 5 | 6 | 7 | def index(request): 8 | template_name = 'index.html' 9 | return render(request, template_name) 10 | 11 | 12 | def products(request): 13 | products = Product.objects.values('title', 'price') 14 | data = { 15 | 'data': [ 16 | { 17 | 'title': item['title'], 18 | 'value': float(item['price']) 19 | } 20 | for item in products 21 | ] 22 | } 23 | return JsonResponse(data) 24 | 25 | 26 | def categories(request): 27 | categories = Product.objects\ 28 | .values('category')\ 29 | .annotate(value=Count('category'))\ 30 | .order_by('category')\ 31 | .values('category__title', 'value') 32 | data = { 33 | 'data': [ 34 | { 35 | 'label': item['category__title'], 36 | 'value': item['value'], 37 | } 38 | for item in categories 39 | ] 40 | } 41 | return JsonResponse(data) 42 | -------------------------------------------------------------------------------- /myproject/core/templates/base.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 |
5 | 6 | 7 | 8 | 9 |