diff --git a/homework/app/migrations/0001_initial.py b/homework/app/migrations/0001_initial.py new file mode 100644 index 0000000..41ea478 --- /dev/null +++ b/homework/app/migrations/0001_initial.py @@ -0,0 +1,32 @@ +# Generated by Django 2.0.7 on 2018-07-25 10:06 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ] + + operations = [ + migrations.CreateModel( + name='Category', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('title', models.CharField(max_length=100)), + ('popularity', models.PositiveSmallIntegerField(default=0)), + ], + ), + migrations.CreateModel( + name='Page', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('title', models.CharField(max_length=100)), + ('text', models.TextField()), + ('category', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='pages', to='app.Category')), + ], + ), + ] diff --git a/homework/app/migrations/0002_auto_20180725_1154.py b/homework/app/migrations/0002_auto_20180725_1154.py new file mode 100644 index 0000000..f2cb8a9 --- /dev/null +++ b/homework/app/migrations/0002_auto_20180725_1154.py @@ -0,0 +1,19 @@ +# Generated by Django 2.0.7 on 2018-07-25 11:54 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('app', '0001_initial'), + ] + + operations = [ + migrations.AlterField( + model_name='page', + name='category', + field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, related_name='pages', to='app.Category'), + ), + ] diff --git a/homework/app/models.py b/homework/app/models.py index aedcc0d..c747487 100644 --- a/homework/app/models.py +++ b/homework/app/models.py @@ -8,7 +8,11 @@ class Category(models.Model): title - название категории popularity - популярность категории, измеряется в положительных целых числах, по умолчанию 0 """ - pass + title = models.CharField(max_length=100) + popularity = models.PositiveSmallIntegerField(default=0) + + def __str__(self): + return f"Category {self.title}, category's popularity is {self.popularity}" class Page(models.Model): @@ -19,4 +23,9 @@ class Page(models.Model): text - тест страницы category - необязательная категория (при удалении категории должны удалятся все её страницы) """ - pass + title = models.CharField(max_length=100) + text = models.TextField() + category = models.ForeignKey('Category', null=True, related_name='pages', on_delete=models.CASCADE) + + def __str__(self): + return f'Page {self.title} related to {self.category}' diff --git a/homework/app/templates/base.html b/homework/app/templates/base.html new file mode 100644 index 0000000..f3e0c41 --- /dev/null +++ b/homework/app/templates/base.html @@ -0,0 +1,10 @@ +<!DOCTYPE html> +<html lang="en"> +<head> + <meta charset="UTF-8"> + <title>{{ page_title }}</title> +</head> +<body> + {% block content %}{% endblock %} +</body> +</html> \ No newline at end of file diff --git a/homework/app/templates/category.html b/homework/app/templates/category.html new file mode 100644 index 0000000..0f96a9f --- /dev/null +++ b/homework/app/templates/category.html @@ -0,0 +1,9 @@ +{% extends 'base.html' %} + +{% block content %} + <ul> + {% for page in category_pages %} + <li>{{ page.title }}</li> + {% endfor %} + </ul> +{% endblock %} diff --git a/homework/app/templates/page.html b/homework/app/templates/page.html new file mode 100644 index 0000000..c8a74d7 --- /dev/null +++ b/homework/app/templates/page.html @@ -0,0 +1,5 @@ +{% extends 'base.html' %} + +{% block content %} + {{ text }} +{% endblock %} diff --git a/homework/app/templates/pages.html b/homework/app/templates/pages.html new file mode 100644 index 0000000..27d1dcd --- /dev/null +++ b/homework/app/templates/pages.html @@ -0,0 +1,9 @@ +{% extends 'base.html' %} + +{% block content %} + <ul> + {% for title in titles %} + <li>{{ title.title }}</li> + {% endfor %} + </ul> +{% endblock %} diff --git a/homework/app/templates/unknown_category.html b/homework/app/templates/unknown_category.html new file mode 100644 index 0000000..a074b2a --- /dev/null +++ b/homework/app/templates/unknown_category.html @@ -0,0 +1,9 @@ +{% extends 'base.html' %} + +{% block content %} + <ul> + {% for page in pages %} + <li>{{ page.title }}</li> + {% endfor %} + </ul> +{% endblock %} diff --git a/homework/app/views.py b/homework/app/views.py index 8b13789..9b573ce 100644 --- a/homework/app/views.py +++ b/homework/app/views.py @@ -1 +1,45 @@ +from django.shortcuts import render +from homework.app.models import Category, Page +from django.db.models import Q + +def show_page(request, id=1): + page = Page.objects.get(id=id) + return render(request, 'page.html', { + 'page_title': page.title, + 'text': page.text, + }) + + +def show_pages(request): + titles = Page.objects.values('title').order_by('-title') + page_title = 'Pages' + return render(request, 'pages.html', { + 'page_title': page_title, + 'titles': titles, + }) + + +def show_category(request, id): + category = Category.objects.get(id=id) + category_title = category.title + category_pages = category.pages.order_by('title') + return render(request, 'category.html', { + 'page_title': category_title, + 'category_pages': category_pages, + }) + + +def show_unknown_category(request): + pages = Page.objects.filter(category__isnull=True).order_by('title') + page_title = 'unknown category' + return render(request, 'unknown_category.html', { + 'page_title': page_title, + 'pages': pages, + }) + + +def get_queryset_result(min_pop, max_pop): + category_ids_between_max_and_min = Category.objects.values('id').filter(Q(popularity__gt=max_pop) + | Q(popularity__lt=min_pop)) + return [item['id'] for item in category_ids_between_max_and_min] diff --git a/homework/urls.py b/homework/urls.py index dfc7362..7952f42 100644 --- a/homework/urls.py +++ b/homework/urls.py @@ -1,6 +1,14 @@ from django.contrib import admin from django.urls import path +from homework.app.views import show_page, show_pages, show_category, show_unknown_category + urlpatterns = [ path('admin/', admin.site.urls), + + path('', show_page), + path('<int:id>/', show_page), + path('pages/', show_pages), + path('category/<int:id>', show_category), + path('category/unknown', show_unknown_category), ]