Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Django1 #102

Open
wants to merge 9 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
Empty file added __init__.py
Empty file.
Empty file added src/ppz/ppz/api/__init__.py
Empty file.
3 changes: 3 additions & 0 deletions src/ppz/ppz/api/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.contrib import admin

# Register your models here.
5 changes: 5 additions & 0 deletions src/ppz/ppz/api/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django.apps import AppConfig


class ApiConfig(AppConfig):
name = 'api'
Empty file.
3 changes: 3 additions & 0 deletions src/ppz/ppz/api/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.db import models

# Create your models here.
24 changes: 24 additions & 0 deletions src/ppz/ppz/api/serializers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from django.contrib.auth.models import User

from rest_framework import serializers

from blog.models import Article

from blog.models import Book

class BookSerializer(serializers.ModelSerializer):
class Meta:
model = Book
fields = 'id', 'title', 'author', 'description', 'author_id'

class AuthorSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = '__all__'


class ArticleSerializer(serializers.ModelSerializer):
author = AuthorSerializer(read_only=True)
class Meta:
model = Article
fields = 'id', 'title', 'author', 'url', 'description', 'author_id'
3 changes: 3 additions & 0 deletions src/ppz/ppz/api/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.test import TestCase

# Create your tests here.
16 changes: 16 additions & 0 deletions src/ppz/ppz/api/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from django.urls import path, include

from rest_framework.routers import DefaultRouter

from api.views import ArticleViewSet
from api.views import BookViewSet

router = DefaultRouter()
router.register(r"articles", ArticleViewSet)

urlpatterns = [path("", include(router.urls))]



router.register(r"books", BookViewSet)
urlpatterns = [path("", include(router.urls))]
19 changes: 19 additions & 0 deletions src/ppz/ppz/api/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from rest_framework.viewsets import ModelViewSet


from api.serializers import BookSerializer
from api.serializers import ArticleSerializer

from blog.models import Article
from blog.models import Book


class ArticleViewSet(ModelViewSet):
queryset = Article.objects.all()
serializer_class = ArticleSerializer

class BookViewSet(ModelViewSet):
queryset = Book.objects.all()
serializer_class = BookSerializer


Empty file added src/ppz/ppz/blog/__init__.py
Empty file.
14 changes: 14 additions & 0 deletions src/ppz/ppz/blog/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from django.contrib import admin
from blog.models import Article


class ArticleAdmin(admin.ModelAdmin):

search_fields = ('title', 'short_description', 'author',)
list_display = 'title', 'short_description', 'author_name', 'author'
list_filter = 'title',




admin.site.register(Article, ArticleAdmin)
5 changes: 5 additions & 0 deletions src/ppz/ppz/blog/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django.apps import AppConfig


class BlogConfig(AppConfig):
name = 'blog'
24 changes: 24 additions & 0 deletions src/ppz/ppz/blog/forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from django import forms

from blog.models import Article
from blog.models import Book

class ArticleForm(forms.ModelForm):

description = forms.CharField(
label='Описание'
)

class Meta:
model = Article
fields = ('title', 'description', 'author')

class BookForm(forms.ModelForm):

description = forms.CharField(
label='Описание'
)

class Meta:
model = Book
fields = ('title', 'description', 'author')
23 changes: 23 additions & 0 deletions src/ppz/ppz/blog/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Generated by Django 3.1.1 on 2020-09-09 17:12

from django.db import migrations, models


class Migration(migrations.Migration):

initial = True

dependencies = [
]

operations = [
migrations.CreateModel(
name='Article',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('title', models.CharField(max_length=10)),
('description', models.CharField(max_length=256)),
('author_name', models.CharField(max_length=20)),
],
),
]
21 changes: 21 additions & 0 deletions src/ppz/ppz/blog/migrations/0002_article_author.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Generated by Django 3.1.1 on 2020-09-09 18:33

from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('blog', '0001_initial'),
]

operations = [
migrations.AddField(
model_name='article',
name='author',
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL),
),
]
18 changes: 18 additions & 0 deletions src/ppz/ppz/blog/migrations/0003_auto_20200909_2156.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 3.1.1 on 2020-09-09 18:56

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('blog', '0002_article_author'),
]

operations = [
migrations.AlterField(
model_name='article',
name='description',
field=models.TextField(),
),
]
18 changes: 18 additions & 0 deletions src/ppz/ppz/blog/migrations/0004_article_image.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 3.1.1 on 2020-09-28 16:17

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('blog', '0003_auto_20200909_2156'),
]

operations = [
migrations.AddField(
model_name='article',
name='image',
field=models.ImageField(blank=True, null=True, upload_to=''),
),
]
27 changes: 27 additions & 0 deletions src/ppz/ppz/blog/migrations/0005_book.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Generated by Django 3.1.1 on 2020-10-01 15:16

from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('blog', '0004_article_image'),
]

operations = [
migrations.CreateModel(
name='Book',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('title', models.CharField(max_length=10)),
('description', models.TextField()),
('author_name', models.CharField(max_length=20)),
('image', models.ImageField(blank=True, null=True, upload_to='')),
('author', models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
],
),
]
Empty file.
34 changes: 34 additions & 0 deletions src/ppz/ppz/blog/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from django.db import models
from django.contrib.auth.models import User

class Article(models.Model):
title = models.CharField(max_length=10)
description = models.TextField()
author_name = models.CharField(max_length=20)
author = models.ForeignKey(User, on_delete=models.CASCADE, null=True)
image = models.ImageField(blank=True, null=True)

@property
def short_description(self):
return self.description[:1]


def __str__(self):
return self.title

class Book(models.Model):
title = models.CharField(max_length=10)
description = models.TextField()
author_name = models.CharField(max_length=20)
author = models.ForeignKey(User, on_delete=models.CASCADE, null=True)
image = models.ImageField(blank=True, null=True)



@property
def short_description(self):
return self.description[:1]


def __str__(self):
return self.title
3 changes: 3 additions & 0 deletions src/ppz/ppz/blog/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.test import TestCase

# Create your tests here.
11 changes: 11 additions & 0 deletions src/ppz/ppz/blog/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from django.urls import path
from blog.views import UserListView, UserDetailView, ArticleDetailView, BookDetailView



urlpatterns = [
path('', UserListView.as_view()),
path('users/<username>/', UserDetailView.as_view()),
path('article/<pk>/', ArticleDetailView.as_view()),
path('book/<pk>/', BookDetailView.as_view()),
]
45 changes: 45 additions & 0 deletions src/ppz/ppz/blog/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
from django.views.generic import ListView, DetailView
from django.contrib.auth.models import User
from blog.forms import ArticleForm
from blog.forms import BookForm
from blog.models import Article
from blog.models import Book


class UserListView(ListView):
model = User
template_name = 'main.html'

def get_context_data(self, *, object_list=None, **kwargs):
context = super().get_context_data(object_list=None, **kwargs)
context['users'] = self.object_list
context['form'] = ArticleForm
return context

def post(self, request, *args, **kwargs):
form = ArticleForm(request.POST)
form.is_valid()
form.save()
return self.get(request, *args, **kwargs)


class UserDetailView(DetailView):
model = User
template_name = 'user.html'
slug_field = 'username'
slug_url_kwarg = 'username'

class ArticleDetailView(DetailView):
model = Article
template_name = 'article.html'

class BookDetailView(DetailView):
model = Book
template_name = 'article.html'







22 changes: 22 additions & 0 deletions src/ppz/ppz/manage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/env python
"""Django's command-line utility for administrative tasks."""
import os
import sys


def main():
"""Run administrative tasks."""
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'ppz.settings')
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
raise ImportError(
"Couldn't import Django. Are you sure it's installed and "
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?"
) from exc
execute_from_command_line(sys.argv)


if __name__ == '__main__':
main()
Empty file added src/ppz/ppz/ppz/__init__.py
Empty file.
16 changes: 16 additions & 0 deletions src/ppz/ppz/ppz/asgi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""
ASGI config for ppz project.

It exposes the ASGI callable as a module-level variable named ``application``.

For more information on this file, see
https://docs.djangoproject.com/en/3.1/howto/deployment/asgi/
"""

import os

from django.core.asgi import get_asgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'ppz.settings')

application = get_asgi_application()
Loading