version 0.0.6 new models and api for News
This commit is contained in:
parent
05556ed58e
commit
68f6e0bc9c
|
|
@ -26,7 +26,7 @@ class AddressAdmin(admin.OSMGeoAdmin):
|
|||
readonly_fields = ['geo_lon', 'geo_lat',]
|
||||
fieldsets = (
|
||||
(_('Main'), {
|
||||
'fields': ['street_name_1', 'street_name_2',]
|
||||
'fields': ['street_name_1', 'street_name_2', 'number']
|
||||
}),
|
||||
(_('Location'), {
|
||||
'fields': ['coordinates', 'geo_lon', 'geo_lat', ]
|
||||
|
|
|
|||
|
|
@ -78,3 +78,10 @@ class Address(models.Model):
|
|||
def get_street_name(self):
|
||||
return self.street_name_1 or self.street_name_2
|
||||
|
||||
@property
|
||||
def latitude(self):
|
||||
return self.coordinates.y
|
||||
|
||||
@property
|
||||
def longitude(self):
|
||||
return self.coordinates.x
|
||||
|
|
|
|||
|
|
@ -1,2 +1,72 @@
|
|||
from rest_framework import serializers
|
||||
from location import models
|
||||
|
||||
|
||||
class CountrySerializer(serializers.ModelSerializer):
|
||||
"""Country serializer."""
|
||||
class Meta:
|
||||
model = models.Country
|
||||
fields = [
|
||||
'id',
|
||||
'name'
|
||||
]
|
||||
|
||||
|
||||
class RegionSerializer(serializers.ModelSerializer):
|
||||
"""Region serializer"""
|
||||
country = CountrySerializer()
|
||||
|
||||
class Meta:
|
||||
model = models.Region
|
||||
fields = [
|
||||
'id',
|
||||
'name',
|
||||
'code',
|
||||
'parent_region',
|
||||
'country'
|
||||
]
|
||||
|
||||
|
||||
class CitySerializer(serializers.ModelSerializer):
|
||||
"""City serializer."""
|
||||
country = CountrySerializer()
|
||||
region = RegionSerializer()
|
||||
|
||||
class Meta:
|
||||
model = models.City
|
||||
fields = [
|
||||
'id',
|
||||
'name',
|
||||
'code',
|
||||
'region',
|
||||
'country',
|
||||
'postal_code',
|
||||
'is_island',
|
||||
]
|
||||
|
||||
|
||||
class AddressSerializer(serializers.ModelSerializer):
|
||||
"""Address serializer."""
|
||||
city = CitySerializer()
|
||||
longitude = serializers.DecimalField(max_digits=10, decimal_places=6)
|
||||
latitude = serializers.DecimalField(max_digits=10, decimal_places=6)
|
||||
|
||||
class Meta:
|
||||
model = models.Address
|
||||
fields = [
|
||||
'city',
|
||||
'street_name_1',
|
||||
'street_name_2',
|
||||
'number',
|
||||
'postal_code',
|
||||
'longitude',
|
||||
'latitude'
|
||||
]
|
||||
|
||||
# def validate(self, attrs):
|
||||
# geo_lat = attrs.pop('geo_lat') if 'geo_lat' in attrs else None
|
||||
# geo_lon = attrs.pop('geo_lon') if 'geo_lon' in attrs else None
|
||||
# if geo_lat and geo_lon:
|
||||
# # Point(longitude, latitude)
|
||||
# attrs['coordinates'] = Point(geo_lon, geo_lat)
|
||||
# return attrs
|
||||
0
apps/main/__init__.py
Normal file
0
apps/main/__init__.py
Normal file
3
apps/main/admin.py
Normal file
3
apps/main/admin.py
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
from django.contrib import admin
|
||||
|
||||
# Register your models here.
|
||||
5
apps/main/apps.py
Normal file
5
apps/main/apps.py
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class MainConfig(AppConfig):
|
||||
name = 'main'
|
||||
0
apps/main/migrations/__init__.py
Normal file
0
apps/main/migrations/__init__.py
Normal file
86
apps/main/models.py
Normal file
86
apps/main/models.py
Normal file
|
|
@ -0,0 +1,86 @@
|
|||
from django.db import models
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
from django.contrib.postgres.fields import JSONField
|
||||
|
||||
|
||||
class Tag(models.Model):
|
||||
"""Tag model."""
|
||||
name = models.CharField(max_length=250)
|
||||
|
||||
|
||||
class MediaAsset(models.Model):
|
||||
"""Assets model."""
|
||||
PHOTO = 0
|
||||
VIDEO = 1
|
||||
VIDEO_URL = 2
|
||||
PDF = 3
|
||||
DOCUMENT = 4
|
||||
|
||||
ASSET_TYPE_CHOICES = (
|
||||
(PHOTO, _('photo')),
|
||||
(VIDEO, _('video')),
|
||||
(VIDEO_URL, _('video url')),
|
||||
(PDF, _('pdf')),
|
||||
(DOCUMENT, _('document'))
|
||||
)
|
||||
|
||||
AMAZON_S3 = 0
|
||||
|
||||
STORAGE_TYPE_CHOICES = (
|
||||
(AMAZON_S3, _('amazon s3')),
|
||||
)
|
||||
|
||||
PUBLIC = 0
|
||||
PRIVATE = 1
|
||||
ROLE = 2
|
||||
|
||||
ACCESS_TYPE_CHOICES = (
|
||||
(PUBLIC, _('public')),
|
||||
(PRIVATE, _('private')),
|
||||
(ROLE, _('role')),
|
||||
)
|
||||
|
||||
asset_type = models.PositiveSmallIntegerField(
|
||||
_('asset type'), choices=ASSET_TYPE_CHOICES, default=PHOTO
|
||||
)
|
||||
storage_type = models.PositiveSmallIntegerField(
|
||||
_('storage type'), choices=STORAGE_TYPE_CHOICES, default=AMAZON_S3
|
||||
)
|
||||
storage_unit = models.CharField(_('storage unit'), blank=True, default='')
|
||||
path = models.CharField(_('path'))
|
||||
access_type = models.PositiveSmallIntegerField(
|
||||
_('access type'), choices=ACCESS_TYPE_CHOICES, default=PUBLIC)
|
||||
asset_text = JSONField(_('asset_text'))
|
||||
size = models.IntegerField(_('size'))
|
||||
version = models.CharField(_('version'), max_length=250)
|
||||
text = JSONField(_('text'))
|
||||
|
||||
|
||||
class Contacts(models.Model):
|
||||
"""Contacts model."""
|
||||
address = models.ForeignKey(
|
||||
'location.Address', verbose_name=_('address'), on_delete=models.CASCADE
|
||||
)
|
||||
|
||||
|
||||
class Social(models.Model):
|
||||
"""Social model."""
|
||||
WEBSITE = 0
|
||||
SOCIAL = 1
|
||||
|
||||
SOCIAL_TYPE_CHOICES = (
|
||||
(WEBSITE, _('website')),
|
||||
(SOCIAL, _('social')),
|
||||
)
|
||||
|
||||
social_type = models.PositiveSmallIntegerField(
|
||||
_('social type'), choices=SOCIAL_TYPE_CHOICES
|
||||
)
|
||||
|
||||
|
||||
class Note(models.Model):
|
||||
"""Note model."""
|
||||
|
||||
|
||||
class Ad(models.Model):
|
||||
"""Ad model."""
|
||||
3
apps/main/tests.py
Normal file
3
apps/main/tests.py
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
from django.test import TestCase
|
||||
|
||||
# Create your tests here.
|
||||
3
apps/main/views.py
Normal file
3
apps/main/views.py
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
from django.shortcuts import render
|
||||
|
||||
# Create your views here.
|
||||
0
apps/news/__init__.py
Normal file
0
apps/news/__init__.py
Normal file
12
apps/news/admin.py
Normal file
12
apps/news/admin.py
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
from django.contrib import admin
|
||||
from news import models
|
||||
|
||||
|
||||
@admin.register(models.NewsType)
|
||||
class NewsTypeAdmin(admin.ModelAdmin):
|
||||
"""News type admin."""
|
||||
|
||||
|
||||
@admin.register(models.News)
|
||||
class NewsAdmin(admin.ModelAdmin):
|
||||
"""News admin."""
|
||||
7
apps/news/apps.py
Normal file
7
apps/news/apps.py
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
from django.apps import AppConfig
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
|
||||
class NewsConfig(AppConfig):
|
||||
name = 'news'
|
||||
verbose_name = _('news')
|
||||
49
apps/news/migrations/0001_initial.py
Normal file
49
apps/news/migrations/0001_initial.py
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
# Generated by Django 2.2.4 on 2019-08-12 08:29
|
||||
|
||||
from django.conf import settings
|
||||
import django.contrib.postgres.fields.jsonb
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
import django.utils.timezone
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
initial = True
|
||||
|
||||
dependencies = [
|
||||
('location', '0001_initial'),
|
||||
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='NewsType',
|
||||
fields=[
|
||||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('name', models.CharField(max_length=250, verbose_name='name')),
|
||||
],
|
||||
),
|
||||
migrations.CreateModel(
|
||||
name='News',
|
||||
fields=[
|
||||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('created', models.DateTimeField(default=django.utils.timezone.now, editable=False, verbose_name='Date created')),
|
||||
('modified', models.DateTimeField(auto_now=True, verbose_name='Date updated')),
|
||||
('title', django.contrib.postgres.fields.jsonb.JSONField(blank=True, default=None, help_text='{"en":"some text"}', null=True, verbose_name='title')),
|
||||
('subtitle', django.contrib.postgres.fields.jsonb.JSONField(blank=True, default=None, help_text='{"en":"some text"}', null=True, verbose_name='subtitle')),
|
||||
('description', django.contrib.postgres.fields.jsonb.JSONField(blank=True, default=None, help_text='{"en":"some text"}', null=True, verbose_name='subtitle')),
|
||||
('start', models.DateTimeField(verbose_name='start')),
|
||||
('end', models.DateTimeField(verbose_name='end')),
|
||||
('playlist', models.IntegerField(verbose_name='playlist')),
|
||||
('address', models.ForeignKey(blank=True, default=None, null=True, on_delete=django.db.models.deletion.CASCADE, to='location.Address', verbose_name='address')),
|
||||
('created_by', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='news_records_created', to=settings.AUTH_USER_MODEL, verbose_name='created by')),
|
||||
('modified_by', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='news_records_modified', to=settings.AUTH_USER_MODEL, verbose_name='modified by')),
|
||||
('news_type', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='news.NewsType', verbose_name='news type')),
|
||||
],
|
||||
options={
|
||||
'verbose_name': 'news',
|
||||
'verbose_name_plural': 'news',
|
||||
},
|
||||
),
|
||||
]
|
||||
0
apps/news/migrations/__init__.py
Normal file
0
apps/news/migrations/__init__.py
Normal file
42
apps/news/models.py
Normal file
42
apps/news/models.py
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
from django.db import models
|
||||
from utils.models import ProjectBaseMixin, BaseAttributes
|
||||
from django.contrib.postgres.fields import JSONField, ArrayField
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
|
||||
class NewsType(models.Model):
|
||||
"""NewsType model."""
|
||||
name = models.CharField(_('name'), max_length=250)
|
||||
|
||||
|
||||
class News(BaseAttributes):
|
||||
"""News model."""
|
||||
news_type = models.ForeignKey(
|
||||
NewsType, verbose_name=_('news type'), on_delete=models.CASCADE)
|
||||
|
||||
title = JSONField(
|
||||
_('title'), null=True, blank=True,
|
||||
default=None, help_text='{"en":"some text"}')
|
||||
subtitle = JSONField(
|
||||
_('subtitle'), null=True, blank=True,
|
||||
default=None, help_text='{"en":"some text"}'
|
||||
)
|
||||
description = JSONField(
|
||||
_('subtitle'), null=True, blank=True,
|
||||
default=None, help_text='{"en":"some text"}'
|
||||
)
|
||||
start = models.DateTimeField(_('start'))
|
||||
end = models.DateTimeField(_('end'))
|
||||
playlist = models.IntegerField(_('playlist'))
|
||||
address = models.ForeignKey(
|
||||
'location.Address', verbose_name=_('address'), blank=True,
|
||||
null=True, default=None, on_delete=models.CASCADE)
|
||||
# TODO: metadata_keys - описание ключей для динамического построения полей метаданных
|
||||
# TODO: metadata_values - Описание значений для динамических полей из MetadataKeys
|
||||
|
||||
class Meta:
|
||||
verbose_name = _('news')
|
||||
verbose_name_plural = _('news')
|
||||
|
||||
def __str__(self):
|
||||
return f'news: {self.id}'
|
||||
0
apps/news/serializers/__init__.py
Normal file
0
apps/news/serializers/__init__.py
Normal file
51
apps/news/serializers/common.py
Normal file
51
apps/news/serializers/common.py
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
from rest_framework import serializers
|
||||
from news import models
|
||||
from location.serializers import AddressSerializer
|
||||
from location.models import Address
|
||||
|
||||
|
||||
class NewsSerializer(serializers.ModelSerializer):
|
||||
"""News serializer."""
|
||||
address = AddressSerializer()
|
||||
|
||||
class Meta:
|
||||
model = models.News
|
||||
fields = [
|
||||
'id',
|
||||
'news_type',
|
||||
'title',
|
||||
'subtitle',
|
||||
'description',
|
||||
'start',
|
||||
'end',
|
||||
'playlist',
|
||||
'address'
|
||||
]
|
||||
|
||||
|
||||
class NewsCreateUpdateSerializer(NewsSerializer):
|
||||
"""News update serializer."""
|
||||
title = serializers.JSONField()
|
||||
subtitle = serializers.JSONField()
|
||||
description = serializers.JSONField()
|
||||
news_type = serializers.PrimaryKeyRelatedField(
|
||||
queryset=models.NewsType.objects.all(), write_only=True)
|
||||
address = serializers.PrimaryKeyRelatedField(
|
||||
queryset=Address.objects.all(), write_only=True)
|
||||
|
||||
class Meta:
|
||||
model = models.News
|
||||
read_only_fields = [
|
||||
'id'
|
||||
]
|
||||
fields = [
|
||||
'id',
|
||||
'news_type',
|
||||
'title',
|
||||
'subtitle',
|
||||
'description',
|
||||
'start',
|
||||
'end',
|
||||
'playlist',
|
||||
'address'
|
||||
]
|
||||
0
apps/news/serializers/web.py
Normal file
0
apps/news/serializers/web.py
Normal file
3
apps/news/tests.py
Normal file
3
apps/news/tests.py
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
from django.test import TestCase
|
||||
|
||||
# Create your tests here.
|
||||
0
apps/news/urls/__init__.py
Normal file
0
apps/news/urls/__init__.py
Normal file
14
apps/news/urls/web.py
Normal file
14
apps/news/urls/web.py
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
"""Location app urlconf."""
|
||||
from django.urls import path
|
||||
|
||||
from news.views import common
|
||||
|
||||
app_name = 'news'
|
||||
|
||||
urlpatterns = [
|
||||
path('', common.NewsList.as_view(), name='news_list'),
|
||||
path('create/', common.NewsCreate.as_view(), name='news_create'),
|
||||
path('<int:pk>/', common.NewsDetail.as_view(), name='news_detail'),
|
||||
path('<int:pk>/update/', common.NewsUpdate.as_view(), name='news_update'),
|
||||
path('<int:pk>/delete/', common.NewsDelete.as_view(), name='news_delete'),
|
||||
]
|
||||
0
apps/news/views/__init__.py
Normal file
0
apps/news/views/__init__.py
Normal file
37
apps/news/views/common.py
Normal file
37
apps/news/views/common.py
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
from rest_framework import generics, permissions
|
||||
from news.models import News
|
||||
from news.serializers import common as serializers
|
||||
|
||||
|
||||
class NewsList(generics.ListAPIView):
|
||||
"""News list view."""
|
||||
queryset = News.objects.all()
|
||||
permission_classes = (permissions.AllowAny, )
|
||||
serializer_class = serializers.NewsSerializer
|
||||
|
||||
|
||||
class NewsCreate(generics.CreateAPIView):
|
||||
"""News list view."""
|
||||
queryset = News.objects.all()
|
||||
permission_classes = (permissions.IsAuthenticated, )
|
||||
serializer_class = serializers.NewsCreateUpdateSerializer
|
||||
|
||||
|
||||
class NewsDetail(generics.RetrieveAPIView):
|
||||
"""News detail view."""
|
||||
queryset = News.objects.all()
|
||||
permission_classes = (permissions.AllowAny, )
|
||||
serializer_class = serializers.NewsSerializer
|
||||
|
||||
|
||||
class NewsDelete(generics.DestroyAPIView):
|
||||
"""News delete view."""
|
||||
queryset = News.objects.all()
|
||||
permission_classes = (permissions.IsAuthenticated, )
|
||||
|
||||
|
||||
class NewsUpdate(generics.UpdateAPIView):
|
||||
"""News update view."""
|
||||
queryset = News.objects.all()
|
||||
permission_classes = (permissions.IsAuthenticated, )
|
||||
serializer_class = serializers.NewsCreateUpdateSerializer
|
||||
0
apps/news/views/web.py
Normal file
0
apps/news/views/web.py
Normal file
|
|
@ -35,6 +35,21 @@ class OAuthProjectMixin:
|
|||
basemixin_fields = ['created', 'modified']
|
||||
|
||||
|
||||
class BaseAttributes(ProjectBaseMixin):
|
||||
created_by = models.ForeignKey(
|
||||
'account.User', on_delete=models.SET_NULL, verbose_name=_('created by'),
|
||||
null=True, related_name='%(class)s_records_created'
|
||||
)
|
||||
modified_by = models.ForeignKey(
|
||||
'account.User', on_delete=models.SET_NULL, verbose_name=_('modified by'),
|
||||
null=True, related_name='%(class)s_records_modified'
|
||||
)
|
||||
|
||||
class Meta:
|
||||
"""Meta class."""
|
||||
|
||||
abstract = True
|
||||
|
||||
def generate_code():
|
||||
"""Generate code method."""
|
||||
return '%06d' % random.randint(0, 999999)
|
||||
|
|
|
|||
|
|
@ -54,6 +54,8 @@ CONTRIB_APPS = [
|
|||
PROJECT_APPS = [
|
||||
'account.apps.AccountConfig',
|
||||
'authorization.apps.AuthorizationConfig',
|
||||
'location.apps.LocationConfig',
|
||||
'news.apps.NewsConfig',
|
||||
|
||||
]
|
||||
|
||||
|
|
|
|||
|
|
@ -19,4 +19,5 @@ app_name = 'web'
|
|||
|
||||
urlpatterns = [
|
||||
path('account/', include('account.urls.web')),
|
||||
path('news/', include('news.urls.web')),
|
||||
]
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user