coding style
This commit is contained in:
@@ -1,10 +1,9 @@
|
||||
from django.contrib import admin, messages
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
from .models import Post, Event, News, FetMeeting, FileUpload
|
||||
from .forms import PostForm, EventForm, NewsForm, FetMeetingForm
|
||||
|
||||
from documents.api import create_pad
|
||||
from .forms import EventForm, FetMeetingForm, NewsForm, PostForm
|
||||
from .models import Event, FetMeeting, FileUpload, News, Post
|
||||
|
||||
|
||||
def make_fetmeeting(self, request, queryset):
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
from ckeditor_uploader.widgets import CKEditorUploadingWidget
|
||||
from taggit.models import Tag
|
||||
|
||||
from django import forms
|
||||
from django.utils import timezone
|
||||
from django.utils.dates import MONTHS
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
from taggit.models import Tag
|
||||
|
||||
from .models import Post, Event, News, FetMeeting
|
||||
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
import re
|
||||
import logging
|
||||
|
||||
import re
|
||||
from datetime import timedelta
|
||||
|
||||
from taggit.managers import TaggableManager
|
||||
|
||||
from django.contrib.auth.models import User
|
||||
from django.core.validators import ValidationError
|
||||
from django.db import models
|
||||
@@ -10,7 +12,6 @@ from django.urls import reverse
|
||||
from django.utils import timezone
|
||||
from django.utils.text import slugify
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
from taggit.managers import TaggableManager
|
||||
|
||||
from core.models import CustomFlatPage
|
||||
from documents import create_pad, get_pad_html, set_pad_html
|
||||
@@ -118,9 +119,35 @@ class Post(models.Model):
|
||||
objects = PostManager()
|
||||
articles = ArticleManager()
|
||||
|
||||
def get_tags(self):
|
||||
"""Returns assigned tags as a comma seperated list."""
|
||||
return ",".join(self.tags.names())
|
||||
def __str__(self):
|
||||
return "Post (%s, %s): %s" % (
|
||||
self.slug,
|
||||
self.public_date.strftime("%d.%m.%Y"),
|
||||
self.title,
|
||||
)
|
||||
|
||||
@property
|
||||
def url(self):
|
||||
# TODO: replace with get_absolute_url
|
||||
return reverse("posts:posts.show", kwargs={"id": self.slug})
|
||||
|
||||
def get_absolute_url(self):
|
||||
return self.url
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
# save the post with some defaults
|
||||
if not self.public_date:
|
||||
self.public_date = timezone.now().date()
|
||||
|
||||
if not self.slug:
|
||||
self.slug = slugify(self.public_date) + "-" + slugify(self.title)
|
||||
|
||||
super().save(*args, **kwargs)
|
||||
|
||||
self.tags.set(
|
||||
*re.findall(r"\#([\d\w-]+)", str(self.subtitle)),
|
||||
*re.findall(r"\#([\d\w-]+)", str(self.title)),
|
||||
)
|
||||
|
||||
@property
|
||||
def agenda_html(self):
|
||||
@@ -147,9 +174,7 @@ class Post(models.Model):
|
||||
|
||||
set_pad_html(self.agenda_key, value)
|
||||
|
||||
request_logger.info(
|
||||
"set etherpad! for post %s id: %s", self.slug, self.agenda_key
|
||||
)
|
||||
request_logger.info(f"set etherpad for post {self.slug} id: {self.agenda_key}")
|
||||
return value
|
||||
|
||||
@protocol_html.setter
|
||||
@@ -162,24 +187,34 @@ class Post(models.Model):
|
||||
set_pad_html(self.protocol_key, value)
|
||||
|
||||
request_logger.info(
|
||||
"set etherpad! for post %s id: %s", self.slug, self.protocol_key
|
||||
f"set etherpad for post {self.slug} id: {self.protocol_key}"
|
||||
)
|
||||
return value
|
||||
|
||||
def get_agenda_key(self):
|
||||
"""Create a Etherpad Id for the Pad associated to this post.
|
||||
Creates the pad if it doesn't exist"""
|
||||
"""
|
||||
Create a Etherpad Id for the Pad associated to this post.
|
||||
Create the pad if it doesn't exist.
|
||||
"""
|
||||
if not self.slug:
|
||||
return None
|
||||
return create_pad_for_post(self.slug, "agenda")
|
||||
|
||||
def get_protocol_key(self):
|
||||
"""Create a Etherpad Id for the Pad associated to this post.
|
||||
Creates the pad if it doesn't exist"""
|
||||
"""
|
||||
Create a Etherpad Id for the Pad associated to this post.
|
||||
Create the pad if it doesn't exist.
|
||||
"""
|
||||
if not self.slug:
|
||||
return None
|
||||
return create_pad_for_post(self.slug, "protocol")
|
||||
|
||||
def get_tags(self):
|
||||
"""
|
||||
Returns assigned tags as a comma seperated list.
|
||||
"""
|
||||
return ",".join(self.tags.names())
|
||||
|
||||
@property
|
||||
def get_tagnames(self):
|
||||
return ["#%s" % t for t in self.tags.names()]
|
||||
@@ -190,7 +225,9 @@ class Post(models.Model):
|
||||
|
||||
@property
|
||||
def imageurl(self):
|
||||
""" returns the url to the image"""
|
||||
"""
|
||||
returns the url to the image
|
||||
"""
|
||||
if self.image:
|
||||
return self.image.url
|
||||
|
||||
@@ -200,7 +237,9 @@ class Post(models.Model):
|
||||
return ""
|
||||
|
||||
def find_an_image(self):
|
||||
"find an image via another post"
|
||||
"""
|
||||
find an image via another post
|
||||
"""
|
||||
# TODO: Explain why this image is selected on save of the image
|
||||
# Query all posts that have a slug that equals one of the tags
|
||||
posts1 = (
|
||||
@@ -213,42 +252,15 @@ class Post(models.Model):
|
||||
|
||||
return None
|
||||
|
||||
@property
|
||||
def url(self):
|
||||
return reverse("posts:posts.show", kwargs={"id": self.slug})
|
||||
|
||||
def get_absolute_url(self):
|
||||
return self.url
|
||||
|
||||
def clean(self):
|
||||
if self.event_end and self.event_end < self.event_start:
|
||||
raise ValidationError(_("Das Ende des Events liegt vor dem Beginn."))
|
||||
if self.event_start and self.post_type not in ["E", "F"]:
|
||||
raise ValidationError("Für diesen Post Typ ist kein Event Start zulässig")
|
||||
raise ValidationError(
|
||||
_("Für diesen Post Typ ist kein Event Start zulässig")
|
||||
)
|
||||
super().clean()
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
# save the post with some defaults
|
||||
if not self.public_date:
|
||||
self.public_date = timezone.now().date()
|
||||
|
||||
if not self.slug:
|
||||
self.slug = slugify(self.public_date) + "-" + slugify(self.title)
|
||||
|
||||
super().save(*args, **kwargs)
|
||||
|
||||
self.tags.set(
|
||||
*re.findall(r"\#([\d\w-]+)", str(self.subtitle)),
|
||||
*re.findall(r"\#([\d\w-]+)", str(self.title)),
|
||||
)
|
||||
|
||||
def __str__(self):
|
||||
return "Post (%s, %s): %s" % (
|
||||
self.slug,
|
||||
self.public_date.strftime("%d.%m.%Y"),
|
||||
self.title,
|
||||
)
|
||||
|
||||
|
||||
class News(Post):
|
||||
objects = NewsManager()
|
||||
@@ -270,21 +282,16 @@ class Event(Post):
|
||||
only_events = EventManager()
|
||||
all_events = AllEventManager()
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
self.post_type = "E"
|
||||
|
||||
class Meta:
|
||||
proxy = True
|
||||
|
||||
verbose_name = "Event"
|
||||
verbose_name_plural = "Events"
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
self.post_type = "E"
|
||||
|
||||
def clean(self):
|
||||
if not self.event_start:
|
||||
raise ValidationError(_("Das Datum des Events fehlt."))
|
||||
super().clean()
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
if not self.post_type:
|
||||
self.post_type = "E"
|
||||
@@ -293,36 +300,25 @@ class Event(Post):
|
||||
|
||||
super().save(*args, **kwargs)
|
||||
|
||||
def clean(self):
|
||||
if not self.event_start:
|
||||
raise ValidationError(_("Das Datum des Events fehlt."))
|
||||
super().clean()
|
||||
|
||||
|
||||
class FetMeeting(Event):
|
||||
objects = FetMeetingManager()
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
self.post_type = "F"
|
||||
|
||||
class Meta:
|
||||
proxy = True
|
||||
|
||||
verbose_name = "Fet Sitzung"
|
||||
verbose_name_plural = "Fet Sitzungen"
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
self.post_type = "F"
|
||||
|
||||
def __get_slug(self):
|
||||
slug = slugify(self.event_start.date()) + "-" + slugify("Fachschaftssitzung")
|
||||
|
||||
if Post.objects.filter(slug=slug).exists():
|
||||
if Post.objects.get(slug=slug).id != self.id:
|
||||
raise ValidationError(
|
||||
_("Es existiert bereits eine Sitzung mit demselben Datum.")
|
||||
)
|
||||
|
||||
return slug
|
||||
|
||||
def clean(self):
|
||||
super().clean()
|
||||
if not self.slug:
|
||||
self.slug = self.__get_slug()
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
self.title = "Fachschaftssitzung"
|
||||
if not self.slug:
|
||||
@@ -346,6 +342,22 @@ class FetMeeting(Event):
|
||||
|
||||
super().save(*args, **kwargs)
|
||||
|
||||
def __get_slug(self):
|
||||
slug = slugify(self.event_start.date()) + "-" + slugify("Fachschaftssitzung")
|
||||
|
||||
if Post.objects.filter(slug=slug).exists():
|
||||
if Post.objects.get(slug=slug).id != self.id:
|
||||
raise ValidationError(
|
||||
_("Es existiert bereits eine Sitzung mit demselben Datum.")
|
||||
)
|
||||
|
||||
return slug
|
||||
|
||||
def clean(self):
|
||||
super().clean()
|
||||
if not self.slug:
|
||||
self.slug = self.__get_slug()
|
||||
|
||||
|
||||
class FileUpload(models.Model):
|
||||
title = models.CharField(verbose_name="Titel", max_length=200)
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
from .models import Post
|
||||
|
||||
from rest_framework import serializers
|
||||
|
||||
from .models import Post
|
||||
|
||||
|
||||
class PostSerializer(serializers.HyperlinkedModelSerializer):
|
||||
agenda_html = serializers.CharField(required=False)
|
||||
|
||||
@@ -3,8 +3,8 @@ from datetime import timedelta
|
||||
from django.test import TestCase
|
||||
from django.utils import timezone
|
||||
|
||||
from .models import Post
|
||||
from .forms import PostForm
|
||||
from .models import Post
|
||||
|
||||
|
||||
class PostTestCase(TestCase):
|
||||
|
||||
@@ -1,13 +1,14 @@
|
||||
from django.urls import path, re_path
|
||||
|
||||
from . import apps
|
||||
from . import views
|
||||
|
||||
from .utils import slug_calc, tag_complete
|
||||
|
||||
app_name = apps.PostsConfig.name
|
||||
|
||||
urlpatterns = [
|
||||
path("func/tag_complete", views.tag_complete),
|
||||
path("func/slug_calc", views.slug_calc),
|
||||
path("func/tag_complete", tag_complete),
|
||||
path("func/slug_calc", slug_calc),
|
||||
path("t/<str:tag>", views.tags, name="posts.tags"),
|
||||
path("", views.index, name="posts.index"),
|
||||
path("fet_calendar.ics", views.calendar, name="posts.calendar"),
|
||||
|
||||
@@ -1,7 +1,12 @@
|
||||
from django.http import HttpResponse
|
||||
from io import BytesIO
|
||||
|
||||
from taggit.models import Tag
|
||||
from xhtml2pdf import pisa
|
||||
|
||||
from django.http import HttpResponse, JsonResponse, HttpResponseServerError
|
||||
from django.utils import timezone
|
||||
from django.utils.text import slugify
|
||||
|
||||
|
||||
def render_to_pdf(html):
|
||||
src = BytesIO(html.encode("ISO-8859-1"))
|
||||
@@ -13,3 +18,40 @@ def render_to_pdf(html):
|
||||
return HttpResponse(dest.getvalue(), content_type="application/pdf")
|
||||
|
||||
return None
|
||||
|
||||
|
||||
def slug_calc(request):
|
||||
"""
|
||||
Ajax function that is called to calculate a slug from the title
|
||||
"""
|
||||
if request.method == "GET":
|
||||
get = request.GET.copy()
|
||||
title = get["title"]
|
||||
slug_str = get["slug"]
|
||||
|
||||
if not slug_str:
|
||||
date_str = timezone.now().strftime("%Y-%m-%d")
|
||||
slug_str = slugify(date_str) + "-" + slugify(title)
|
||||
|
||||
return HttpResponse(slug_str)
|
||||
|
||||
return HttpResponseServerError("Requires a title field.")
|
||||
|
||||
|
||||
def tag_complete(request):
|
||||
"""
|
||||
Ajax function that returns autocomplete suggestions for a given tag input
|
||||
"""
|
||||
if request.method == "GET":
|
||||
get = request.GET.copy()
|
||||
term = get["term"]
|
||||
|
||||
tag_objects = Tag.objects.filter(name__istartswith=term)
|
||||
|
||||
tag_array = []
|
||||
for elem in tag_objects:
|
||||
tag_array.append(elem.name)
|
||||
|
||||
return JsonResponse(tag_array, safe=False)
|
||||
|
||||
return HttpResponseServerError("Requires a term field.")
|
||||
|
||||
@@ -1,20 +1,18 @@
|
||||
import logging
|
||||
|
||||
from collections import deque
|
||||
|
||||
from taggit.models import Tag
|
||||
|
||||
from django.conf import settings
|
||||
from django.contrib import messages
|
||||
from django.http import HttpResponse, JsonResponse, HttpResponseServerError, Http404
|
||||
from django.http import HttpResponse, Http404
|
||||
from django.shortcuts import render
|
||||
from django.template.loader import render_to_string
|
||||
from django.utils import timezone
|
||||
from django.utils.text import slugify
|
||||
from taggit.models import Tag
|
||||
|
||||
from authentications.decorators import authenticated_user
|
||||
from documents.api import get_pad_link
|
||||
from documents.etherpadlib import add_ep_cookie
|
||||
from members.models import Member, JobMember
|
||||
|
||||
from .forms import PostSearchForm
|
||||
from .models import Post, FetMeeting, FileUpload
|
||||
from .utils import render_to_pdf
|
||||
@@ -23,11 +21,6 @@ from .utils import render_to_pdf
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
##################
|
||||
# RENDERED VIEWS #
|
||||
##################
|
||||
|
||||
|
||||
def index(request):
|
||||
posts = None
|
||||
taglist = None
|
||||
@@ -175,7 +168,7 @@ def show(request, id=None):
|
||||
"files": files,
|
||||
"author": author,
|
||||
"author_image": author_image,
|
||||
"next": get_next_dict(p),
|
||||
"next": __get_next_dict(p),
|
||||
"related_posts": related_posts[0:6],
|
||||
"ep_agenda_link": ep_agenda_link,
|
||||
"ep_protocol_link": ep_protocol_link,
|
||||
@@ -236,51 +229,10 @@ def show_pdf_protocol(request, id):
|
||||
return show_pdf(request, html, p.slug + "-protokoll")
|
||||
|
||||
|
||||
###########
|
||||
# HELPERS #
|
||||
###########
|
||||
|
||||
|
||||
def slug_calc(request):
|
||||
def __get_next_dict(post=None):
|
||||
"""
|
||||
Ajax function that is called to calculate a slug from the title
|
||||
Helper function for getting next post
|
||||
"""
|
||||
if request.method == "GET":
|
||||
get = request.GET.copy()
|
||||
title = get["title"]
|
||||
slug_str = get["slug"]
|
||||
|
||||
if not slug_str:
|
||||
datetime_now = timezone.now()
|
||||
date_str = datetime_now.strftime("%Y-%m-%d")
|
||||
|
||||
slug_str = slugify(date_str) + "-" + slugify(title)
|
||||
|
||||
return HttpResponse(slug_str)
|
||||
|
||||
return HttpResponseServerError("Requires a title field.")
|
||||
|
||||
|
||||
def tag_complete(request):
|
||||
"""
|
||||
Ajax function that returns autocomplete suggestions for a given tag input
|
||||
"""
|
||||
if request.method == "GET":
|
||||
get = request.GET.copy()
|
||||
term = get["term"]
|
||||
|
||||
tag_objects = Tag.objects.filter(name__istartswith=term)
|
||||
|
||||
tag_array = []
|
||||
for elem in tag_objects:
|
||||
tag_array.append(elem.name)
|
||||
|
||||
return JsonResponse(tag_array, safe=False)
|
||||
|
||||
return HttpResponseServerError("Requires a term field.")
|
||||
|
||||
|
||||
def get_next_dict(post=None):
|
||||
# TODO: Docstring
|
||||
posts = None
|
||||
d = post.slug
|
||||
|
||||
Reference in New Issue
Block a user