Post app & Settings

This commit is contained in:
2020-05-03 18:53:54 +00:00
parent 52623ef98d
commit 88ad66de73
13 changed files with 200 additions and 11 deletions

4
.gitignore vendored
View File

@@ -1,2 +1,6 @@
.env/* .env/*
*.pyc *.pyc
*_design1
fet2020/files/*
*.sqlite3
fet2020/posts/migrations/*

View File

@@ -0,0 +1,15 @@
from django.contrib.auth.models import User
from django.contrib.auth.middleware import RemoteUserMiddleware
import django
#import logging
#logger=logging.getLogger("django.request")
class FETHeaderMiddleware(RemoteUserMiddleware):
header="Remote-User"
def process_request(self, request):
request.META[self.header]=request.META.get(self.header, request.headers.get(self.header,None))
super().process_request(request)
if request.user.is_authenticated:
request.user.is_admin=True
request.user.is_superuser=True
request.user.is_staff=True

View File

@@ -27,16 +27,21 @@ DEBUG = True
ALLOWED_HOSTS = [] ALLOWED_HOSTS = []
DATA_UPLOAD_MAX_MEMORY_SIZE = 1024*1024*1024
# Application definition # Application definition
CKEDITOR_UPLOAD_PATH = 'upload'
INSTALLED_APPS = [ INSTALLED_APPS = [
'posts.apps.PostsConfig',
'django.contrib.admin', 'django.contrib.admin',
'django.contrib.auth', 'django.contrib.auth',
'django.contrib.contenttypes', 'django.contrib.contenttypes',
'django.contrib.sessions', 'django.contrib.sessions',
'django.contrib.messages', 'django.contrib.messages',
'django.contrib.staticfiles', 'django.contrib.staticfiles',
'taggit',
'ckeditor',
'ckeditor_uploader'
] ]
MIDDLEWARE = [ MIDDLEWARE = [
@@ -45,6 +50,7 @@ MIDDLEWARE = [
'django.middleware.common.CommonMiddleware', 'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware', 'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware',
'fet2020.middleware.FETHeaderMiddleware',
'django.contrib.messages.middleware.MessageMiddleware', 'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware',
] ]
@@ -54,7 +60,7 @@ ROOT_URLCONF = 'fet2020.urls'
TEMPLATES = [ TEMPLATES = [
{ {
'BACKEND': 'django.template.backends.django.DjangoTemplates', 'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [], 'DIRS': ['templates_design1'],
'APP_DIRS': True, 'APP_DIRS': True,
'OPTIONS': { 'OPTIONS': {
'context_processors': [ 'context_processors': [
@@ -80,6 +86,9 @@ DATABASES = {
} }
} }
AUTHENTICATION_BACKENDS = [
'django.contrib.auth.backends.RemoteUserBackend'
]
# Password validation # Password validation
# https://docs.djangoproject.com/en/3.0/ref/settings/#auth-password-validators # https://docs.djangoproject.com/en/3.0/ref/settings/#auth-password-validators
@@ -103,9 +112,9 @@ AUTH_PASSWORD_VALIDATORS = [
# Internationalization # Internationalization
# https://docs.djangoproject.com/en/3.0/topics/i18n/ # https://docs.djangoproject.com/en/3.0/topics/i18n/
LANGUAGE_CODE = 'en-us' LANGUAGE_CODE = 'de-at'
TIME_ZONE = 'UTC' TIME_ZONE = 'CET'
USE_I18N = True USE_I18N = True
@@ -117,4 +126,16 @@ USE_TZ = True
# Static files (CSS, JavaScript, Images) # Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.0/howto/static-files/ # https://docs.djangoproject.com/en/3.0/howto/static-files/
STATIC_URL = '/static/' STATIC_URL = '/assets/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
os.path.join(BASE_DIR, "static_design1"),
]
MEDIA_ROOT=os.path.join(BASE_DIR, 'files/')
MEDIA_URL='/files/'
TAGGIT_FORCE_LOWERCASE = True

View File

@@ -1,4 +1,4 @@
"""fet2020 URL Configuration """test1 URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see: The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/3.0/topics/http/urls/ https://docs.djangoproject.com/en/3.0/topics/http/urls/
@@ -14,8 +14,14 @@ Including another URLconf
2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) 2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
""" """
from django.contrib import admin from django.contrib import admin
from django.urls import path from django.urls import path, include
from django.conf.urls.static import static
from django.conf import settings
from . import views
urlpatterns = [ urlpatterns = [
path('posts/',include('posts.urls')),
path('admin/', admin.site.urls), path('admin/', admin.site.urls),
] path('', views.index, name='home'),
path('index.html', views.index, name='home'),
path('ckeditor/', include('ckeditor_uploader.urls'))
]+static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

14
fet2020/fet2020/views.py Normal file
View File

@@ -0,0 +1,14 @@
from django.shortcuts import render
from django.http import HttpResponse
from collections import deque
from posts.models import Post
def index(request):
posts=deque(Post.objects.all())
l=len(posts)
if l>=1:
featured_post=posts.popleft()
return render(request, 'home.html',{'posts':posts, 'featured_post':featured_post})

View File

@@ -1,3 +1,10 @@
from django.contrib import admin from django.contrib import admin
from .models import Post
# Register your models here. # Register your models here.
from .forms import MyPostForm
class MyPostAdmin(admin.ModelAdmin):
form = MyPostForm
model = Post
admin.site.register(Post,MyPostAdmin)

14
fet2020/posts/forms.py Normal file
View File

@@ -0,0 +1,14 @@
from django import forms
from ckeditor_uploader.widgets import CKEditorUploadingWidget
from .models import Post
class MyPostForm(forms.ModelForm):
class Meta:
model = Post
fields = ['title','subtitle', 'image','body','slug','author']
widgets = {'body': CKEditorUploadingWidget(config_name='default')
}

View File

@@ -1,3 +1,70 @@
from django.db import models from django.db import models
from django.contrib.auth.models import User
from taggit.managers import TaggableManager
from django.utils.text import slugify
#from ckeditor_uploader import RichTextUploadingField
import django
import uuid
import re
# Create your models here. # Create your models here.
class Post(models.Model):
# id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
legacy_id=models.IntegerField(null=True)
legacy_rubrik_id=models.IntegerField(null=True)
title=models.CharField(max_length=200) # Titel des Posts
subtitle=models.CharField(max_length=500, null=True, blank=True) # subtitle
slug=models.SlugField(unique=True,null=True,blank=True) # Slug = Text Basierter url bestandteil zb Fetsitzung 22.1.2020 --> fetsitzung_22_1_2020 für Url
body=models.TextField(null=True, blank=True) # Body Text Artikel Text soll später mit WYSIWG Editor bearbeitet werden
image=models.ImageField(null=True,blank=True) # Ein Haupt Bild für den Post
author=models.ForeignKey(User,on_delete=models.SET_NULL, null=True) # Wer hat das geschrieben
tags=TaggableManager(blank=True) # Tags
public_date=models.DateField('date published',null=True,blank=True, default=django.utils.timezone.now) # Datum ab dem etwas öffentlich sein soll
imported_from = models.CharField(max_length=200, null=True, blank=True) # Titel des Posts
is_fetsitzung=models.BooleanField(default=False)
is_event=models.BooleanField(default=False)
# Zusatz Info wenn ein Event gepostet wird
event_start=models.DateTimeField('Event Start', null=True,blank=True)
event_end=models.DateTimeField('Event Ende', null=True,blank=True)
event_place=models.CharField(max_length=200, null=True,blank=True)
# Dokumente v.a. fuer Sitzungen
has_protocol = models.BooleanField(default=False)
has_agenda = models.BooleanField(default=False)#
protocol_key=models.CharField(max_length=200, null=True,blank=True)
agenda_key=models.CharField(max_length=200, null=True,blank=True)
# TimeStamps
date_modified=models.DateTimeField(auto_now=True)
date_created=models.DateTimeField(auto_now_add=True)
def get_tags(self):
return ",".join(self.tags.names())
def imageurl(self):
if self.image:
return self.image.url
else:
return ""
def key(self):
return self.slug or self.id
def save(self, *args, **kwargs):
if self.id is None and (self.slug is None or self.slug ==""):
self.slug=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)))

View File

View File

@@ -0,0 +1,13 @@
from django import template
import re
from django.template.defaultfilters import stringfilter
from django.utils.safestring import mark_safe
register = template.Library()
@register.filter(is_safe=True)
@stringfilter
def tags_to_url(value):
#return "Tag to url: %s" % value
return mark_safe(re.sub(r'\#([\d\w]+)', '<a href="#\g<1>">#\g<1></a>', value))

7
fet2020/posts/urls.py Normal file
View File

@@ -0,0 +1,7 @@
from django.urls import path
from . import views
urlpatterns=[
path('',views.index,name='posts.index'),
path('<str:id>',views.show, name='posts.show'),
]

View File

@@ -1,3 +1,20 @@
from django.shortcuts import render from django.shortcuts import render
from django.http import HttpResponse
from collections import deque
from .models import Post
from taggit.models import Tag
# Create your views here. # Create your views here.
def index(request):
posts=deque(Post.objects.all())
return render(request, 'posts/index.html', {"posts": posts})
def show(request,id=None):
print("id: %s" % id)
print("is_digit:%s" % id.isdigit())
if id.isdigit() or id is int:
p=Post.objects.get(id=int(id))
elif id != ""and not id is None:
p=Post.objects.get(slug=(id))
return render(request, 'posts/show.html', {"post":p})

4
fet2020/requirements.txt Normal file
View File

@@ -0,0 +1,4 @@
django
django-taggit
django-ckeditor
Pillow