Skip to content
This repository has been archived by the owner on Oct 16, 2019. It is now read-only.

pull request for homework 7 #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions homework/app/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -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')),
],
),
]
19 changes: 19 additions & 0 deletions homework/app/migrations/0002_auto_20180725_1154.py
Original file line number Diff line number Diff line change
@@ -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'),
),
]
13 changes: 11 additions & 2 deletions homework/app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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}'
10 changes: 10 additions & 0 deletions homework/app/templates/base.html
Original file line number Diff line number Diff line change
@@ -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>
9 changes: 9 additions & 0 deletions homework/app/templates/category.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{% extends 'base.html' %}

{% block content %}
<ul>
{% for page in category_pages %}
<li>{{ page.title }}</li>
{% endfor %}
</ul>
{% endblock %}
5 changes: 5 additions & 0 deletions homework/app/templates/page.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{% extends 'base.html' %}

{% block content %}
{{ text }}
{% endblock %}
9 changes: 9 additions & 0 deletions homework/app/templates/pages.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{% extends 'base.html' %}

{% block content %}
<ul>
{% for title in titles %}
<li>{{ title.title }}</li>
{% endfor %}
</ul>
{% endblock %}
9 changes: 9 additions & 0 deletions homework/app/templates/unknown_category.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{% extends 'base.html' %}

{% block content %}
<ul>
{% for page in pages %}
<li>{{ page.title }}</li>
{% endfor %}
</ul>
{% endblock %}
44 changes: 44 additions & 0 deletions homework/app/views.py
Original file line number Diff line number Diff line change
@@ -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]
8 changes: 8 additions & 0 deletions homework/urls.py
Original file line number Diff line number Diff line change
@@ -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),
]