Skip to content

Commit

Permalink
First model trials, setting up the basics (views.py, models.py). Toug…
Browse files Browse the repository at this point in the history
…h part will be html templates and register dialogue.
  • Loading branch information
kpoeppel committed Mar 21, 2019
1 parent 86f796d commit 2ebd505
Show file tree
Hide file tree
Showing 12 changed files with 147 additions and 5 deletions.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
8 changes: 8 additions & 0 deletions bildungsfeiertag/bildungsfeiertag/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from django.contrib import admin
from django.db import models
from .models import *

admin.site.register(Site)
admin.site.register(Room)
admin.site.register(Talk)
admin.site.register(Vote)
67 changes: 67 additions & 0 deletions bildungsfeiertag/bildungsfeiertag/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import docupy
from datetime import datetime
from django.db import models
import bildungsfeiertag.settings as settings


class Site(models.Model):
name = models.CharField(max_length=100)
address = models.TextField()
image = models.CharField(max_length=128)

def __str__(self):
return "{}".format(name)

class Room(models.Model):
site = models.ForeignKey('Site',
on_delete=models.CASCADE)
name = models.CharField(max_length=100)
seats = models.PositiveIntegerField()

def __str__(self):
return "Room {}".format(self.name)


class Talk(models.Model):
title = models.TextField()
date = models.DateField()
description = models.TextField()
room = models.ForeignKey('Room',
on_delete=models.CASCADE)
speaker = models.ForeignKey(settings.AUTH_USER_MODEL,
on_delete=models.CASCADE)
duration = models.DurationField()
time = models.TimeField()
accepted = models.BooleanField()
image = models.CharField(max_length=128)

def __str__(self):
return "\"{}\" by {}".format(self.title, self.speaker.name)


class Vote(models.Model):
talk = models.OneToOneField(
Talk,
on_delete=models.CASCADE,
)
user = models.OneToOneField(
settings.AUTH_USER_MODEL,
on_delete=models.CASCADE,
)


class MediaFile(models.Model):
def create_filename(instance, filename):
extension = "." + filename.split(".")[-1] if "." in filename else ""
return datetime.strftime(datetime.now(), "%Y%m%d-%H%M%S") + extension

def media_lookup():
return {
media.name: media.mediafile.url for media in MediaFile.objects.all()
}

name = models.TextField(primary_key=True)
mediafile = models.FileField(upload_to=create_filename)

def __str__(self):
return "MediaFile ({})".format(self.name)
7 changes: 4 additions & 3 deletions bildungsfeiertag/bildungsfeiertag/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
# See https://docs.djangoproject.com/en/2.1/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '!83t*9=be*x3+i_oe^cd$_p5&b##$5ti)!#^+qoi209j9d%^d3'
SECRET_KEY = '_-ijxz1hdg4cw&#fhd!i__%cniv#26vukzco#^vcc)lp%bj-6_'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
Expand All @@ -37,6 +37,7 @@
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'material'
]

MIDDLEWARE = [
Expand All @@ -54,7 +55,7 @@
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
Expand Down Expand Up @@ -103,7 +104,7 @@
# Internationalization
# https://docs.djangoproject.com/en/2.1/topics/i18n/

LANGUAGE_CODE = 'en-us'
LANGUAGE_CODE = 'de-de'

TIME_ZONE = 'UTC'

Expand Down
Empty file.
10 changes: 8 additions & 2 deletions bildungsfeiertag/bildungsfeiertag/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,15 @@
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""

from django.urls import path, include
from django.conf.urls.static import static
from django.contrib import admin
from django.urls import path
from django.conf import settings
import bildungsfeiertag.views as views


urlpatterns = [
path('admin/', admin.site.urls),
path(r"admin/", admin.site.urls),
path(r"", views.index)
]
60 changes: 60 additions & 0 deletions bildungsfeiertag/bildungsfeiertag/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import os
from django.shortcuts import render
from django.http import Http404
from django.shortcuts import get_object_or_404
from django.db import IntegrityError
from models import Site, Room, Talk, Vote, MediaFile


def index(request):
return render(request, "index.html")


def overview(request):
sites = Site.objects.all()
return render(request, "overview.html")


def site(request, id):
site = get_object_or_404(Site, id=id)
rooms = Room.objects.filter(site=site)
talks = [Talk.objects.filter(room=room) for room in rooms]
return render(request, "site.html", {"site": site,
"rooms": rooms,
"talks": talks})


def talk(request, id):
if request.method == "POST":
talk = get_object_or_404(Talk, id=id)
return render(request, "talk.html", {"talk": talk})


def room(request, id):
room = get_object_or_404(Room, id=id)
talks = Talk.objects.filter(room=room).order_by("time")
return render(request, "room.html", {"room": room, "talks": talks})


def media(request):
if request.method == "POST":
if "delete" in request.POST:
media = MediaFile.objects.get(name=request.POST["name"])
try:
os.remove(media.mediafile.path)
except FileNotFoundError: pass
media.delete()
return redirect("/media/")
try:
MediaFile.objects.create(
name=request.POST["name"], mediafile=request.FILES["file"]
)
except IntegrityError:
return render(request, "media.html", {
"error": "There is already media with that name",
"media": MediaFile.objects.all()
})
return redirect("/media/")
return render(request, "media.html", {
"media": MediaFile.objects.all()
})
Binary file added bildungsfeiertag/db.sqlite3
Binary file not shown.

0 comments on commit 2ebd505

Please sign in to comment.