Skip to content

Commit

Permalink
Merge branch 'main' of github.com:sledilnik/website-backend into main
Browse files Browse the repository at this point in the history
  • Loading branch information
joahim committed Dec 15, 2020
2 parents 75c97bb + fc36aa2 commit 2437237
Show file tree
Hide file tree
Showing 15 changed files with 175 additions and 5 deletions.
1 change: 1 addition & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ django-tastypie = "*"
django-cors-headers = "*"
psycopg2 = "*"
sorl-thumbnail = "*"
django-admin-sortable2 = "*"

[requires]
python_version = "3.8"
15 changes: 11 additions & 4 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion sledilnik/posts/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from django.conf import settings
from django.utils import translation
from django.db.models import Q

from .models import Post

Expand All @@ -15,7 +16,7 @@ class Meta:
resource_name = "posts"
queryset = Post.objects.all()
fields = ["id", "created", "updated", "author", "title", "image", "blurb", "link_to", "body"]
cache = SimpleCache(timeout=60)
cache = SimpleCache(timeout=60, public=True)

def get_object_list(self, request):
kwargs = {
Expand Down
Empty file.
11 changes: 11 additions & 0 deletions sledilnik/restrictions/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from django.contrib import admin
from sledilnik.utils import TranslationAdmin
from adminsortable2.admin import SortableAdminMixin

from .models import Restriction

# Register your models here.
@admin.register(Restriction)
class RestrictionAdmin(SortableAdminMixin, TranslationAdmin):
list_display = ['title', 'regions', 'valid_since', 'valid_until', 'published']
# ordering = ('-order',)
35 changes: 35 additions & 0 deletions sledilnik/restrictions/api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from django.utils import timezone

from tastypie.resources import ModelResource
from tastypie.cache import SimpleCache
from sorl.thumbnail import get_thumbnail

from django.conf import settings
from django.utils import translation

from .models import Restriction
from django.db.models import Max

class RestrictionResource(ModelResource):
class Meta:
resource_name = "restrictions"
queryset = Restriction.objects.all()
fields = ["id", "title", "rule", "regions", "exceptions", "extra_rules",
"valid_since", "valid_until", "validity_comment", "comments", "legal_link", "order"]
cache = SimpleCache(timeout=60, public=True)

def get_object_list(self, request):
return super().get_object_list(request).filter(published=True).order_by("order")

def alter_list_data_to_serialize(self, request, data):
data['meta']['last_update'] = self.get_object_list(request).aggregate(Max("updated")).get('updated__max')
return data

def dehydrate(self, bundle):
lang = bundle.request.GET.get("lang")

if lang not in [lng[0] for lng in settings.LANGUAGES]:
lang = settings.LANGUAGE_CODE
translation.activate(lang)

return bundle
5 changes: 5 additions & 0 deletions sledilnik/restrictions/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django.apps import AppConfig


class RestrictionsConfig(AppConfig):
name = 'restrictions'
52 changes: 52 additions & 0 deletions sledilnik/restrictions/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Generated by Django 3.1.4 on 2020-12-15 09:14

from django.db import migrations, models
import sledilnik.easymde.models


class Migration(migrations.Migration):

initial = True

dependencies = [
]

operations = [
migrations.CreateModel(
name='Restriction',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('title', models.CharField(max_length=255, verbose_name='Title')),
('title_sl', models.CharField(max_length=255, null=True, verbose_name='Title')),
('title_en', models.CharField(max_length=255, null=True, verbose_name='Title')),
('rule', sledilnik.easymde.models.MarkdownField(verbose_name='Rule')),
('rule_sl', sledilnik.easymde.models.MarkdownField(null=True, verbose_name='Rule')),
('rule_en', sledilnik.easymde.models.MarkdownField(null=True, verbose_name='Rule')),
('regions', models.CharField(max_length=255, verbose_name='Regions')),
('regions_sl', models.CharField(max_length=255, null=True, verbose_name='Regions')),
('regions_en', models.CharField(max_length=255, null=True, verbose_name='Regions')),
('exceptions', sledilnik.easymde.models.MarkdownField(blank=True, verbose_name='Rule exceptions')),
('exceptions_sl', sledilnik.easymde.models.MarkdownField(blank=True, null=True, verbose_name='Rule exceptions')),
('exceptions_en', sledilnik.easymde.models.MarkdownField(blank=True, null=True, verbose_name='Rule exceptions')),
('extra_rules', sledilnik.easymde.models.MarkdownField(blank=True, verbose_name='Extra rules')),
('extra_rules_sl', sledilnik.easymde.models.MarkdownField(blank=True, null=True, verbose_name='Extra rules')),
('extra_rules_en', sledilnik.easymde.models.MarkdownField(blank=True, null=True, verbose_name='Extra rules')),
('valid_since', models.DateField(blank=True, null=True, verbose_name='Valid since')),
('valid_until', models.DateField(blank=True, null=True, verbose_name='Valid until')),
('validity_comment', sledilnik.easymde.models.MarkdownField(blank=True, verbose_name='Validity comment')),
('validity_comment_sl', sledilnik.easymde.models.MarkdownField(blank=True, null=True, verbose_name='Validity comment')),
('validity_comment_en', sledilnik.easymde.models.MarkdownField(blank=True, null=True, verbose_name='Validity comment')),
('comments', sledilnik.easymde.models.MarkdownField(blank=True, verbose_name='Comments')),
('comments_sl', sledilnik.easymde.models.MarkdownField(blank=True, null=True, verbose_name='Comments')),
('comments_en', sledilnik.easymde.models.MarkdownField(blank=True, null=True, verbose_name='Comments')),
('legal_link', models.URLField(blank=True, null=True, verbose_name='Legal link')),
('created', models.DateTimeField(auto_now_add=True, verbose_name='Created')),
('updated', models.DateTimeField(auto_now=True, verbose_name='Updated')),
('published', models.BooleanField(default=False, verbose_name='Published')),
('order', models.PositiveIntegerField(default=0, verbose_name='Order')),
],
options={
'ordering': ['order'],
},
),
]
Empty file.
41 changes: 41 additions & 0 deletions sledilnik/restrictions/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from django.db import models
from django.utils.translation import gettext_lazy as _
from sledilnik.easymde.models import MarkdownField

# class Region(models.Model):
# RED = 'RED'
# ORANGE = 'ORANGE'
# GREEN = 'GREEN'
# STATUS_CHOICES = [
# (RED, _('Red')),
# (ORANGE, _('Orange')),
# (GREEN, _('Green')),
# ]

# title = models.CharField(max_length=255, blank=False, null=False, unique=True)
# status = models.CharField(max_length=255, choices=STATUS_CHOICES, blank=True, null=True)

# Create your models here.
class Restriction(models.Model):
title = models.CharField(_("Title"), max_length=255, blank=False, null=False)
rule = MarkdownField(_("Rule"), blank=False, null=False)
regions = models.CharField(_("Regions"), max_length=255, null=False, blank=False)
exceptions = MarkdownField(_("Rule exceptions"), blank=True, null=False)
extra_rules = MarkdownField(_("Extra rules"), blank=True, null=False)
valid_since = models.DateField(_("Valid since"), blank=True, null=True)
valid_until = models.DateField(_("Valid until"), blank=True, null=True)
validity_comment = MarkdownField(_("Validity comment"), blank=True, null=False)
comments = MarkdownField(_("Comments"), blank=True, null=False)
legal_link = models.URLField(_("Legal link"), blank=True, null=True)

created = models.DateTimeField(_("Created"), auto_now_add=True)
updated = models.DateTimeField(_("Updated"), auto_now=True)
published = models.BooleanField(_("Published"), default=False)

order = models.PositiveIntegerField(_("Order"), default=0, null=False, blank=False)

class Meta(object):
ordering = ["order"]

def __str__(self):
return self.title
3 changes: 3 additions & 0 deletions sledilnik/restrictions/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.
7 changes: 7 additions & 0 deletions sledilnik/restrictions/translation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from modeltranslation.translator import register, TranslationOptions
from .models import Restriction


@register(Restriction)
class RestrictionTranslationOptions(TranslationOptions):
fields = ["title", "rule", "regions", "exceptions", "extra_rules", "validity_comment", "comments"]
3 changes: 3 additions & 0 deletions sledilnik/restrictions/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.shortcuts import render

# Create your views here.
2 changes: 2 additions & 0 deletions sledilnik/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,14 @@
'sledilnik',
'sledilnik.posts',
'sledilnik.models',
'sledilnik.restrictions',
'sledilnik.easymde',

'tastypie',
'corsheaders',
'sorl.thumbnail',
'modeltranslation',
'adminsortable2',

'django.contrib.admin',
'django.contrib.auth',
Expand Down
2 changes: 2 additions & 0 deletions sledilnik/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from django.utils.translation import gettext_lazy as _

from sledilnik.posts.api import PostResource
from sledilnik.restrictions.api import RestrictionResource

admin.site.enable_nav_sidebar = False
admin.site.site_header = _("Sledilnik Administration")
Expand All @@ -15,6 +16,7 @@

api = Api(api_name='v1')
api.register(PostResource())
api.register(RestrictionResource())


urlpatterns = [
Expand Down

0 comments on commit 2437237

Please sign in to comment.