diff options
Diffstat (limited to 'cms')
-rw-r--r-- | cms/admin.py | 6 | ||||
-rw-r--r-- | cms/models.py | 38 | ||||
-rw-r--r-- | cms/templates/cms_base.html | 63 | ||||
-rw-r--r-- | cms/urls.py | 8 | ||||
-rw-r--r-- | cms/views.py | 29 |
5 files changed, 132 insertions, 12 deletions
diff --git a/cms/admin.py b/cms/admin.py index 8c38f3f..8918797 100644 --- a/cms/admin.py +++ b/cms/admin.py @@ -1,3 +1,9 @@ from django.contrib import admin +from cms.models import * # Register your models here. + +admin.site.register(Nav) +admin.site.register(SubNav) +admin.site.register(Page) +admin.site.register(StaticFile) diff --git a/cms/models.py b/cms/models.py index 1811a70..05b761f 100644 --- a/cms/models.py +++ b/cms/models.py @@ -1,11 +1,15 @@ +import os + +from django.core.exceptions import ValidationError +from django.core.files.storage import FileSystemStorage from django.db import models # Create your models here. class Nav(models.Model): - name = models.CharField(max=20) - link = models.CharField(max=20) + name = models.CharField(max_length=20) + link = models.CharField(max_length=20) position = models.IntegerField() def __str__(self): @@ -14,8 +18,8 @@ class Nav(models.Model): class SubNav(models.Model): nav = models.ForeignKey(Nav, on_delete=models.CASCADE) - name = models.CharField(max=20) - link = models.CharField(max=100) + name = models.CharField(max_length=20) + link = models.CharField(max_length=100) position = models.IntegerField() def __str__(self): @@ -23,10 +27,10 @@ class SubNav(models.Model): class Page(models.Model): - permalink = models.CharField(max=100, unique=True) - title = models.CharField(max=50) + permalink = models.CharField(max_length=100, unique=True) + title = models.CharField(max_length=50) imports = models.TextField(help_text='External imports like css,js files, will be placed in <head> tag (already ' - 'includes bootstrap4 and jQuery)') + 'includes bootstrap4 and jQuery)', null=True, blank=True) content = models.TextField(help_text='Body of the page') pub_date = models.DateTimeField('date published', auto_now_add=True) @@ -34,10 +38,22 @@ class Page(models.Model): return self.title -class StaticFiles(models.Model): - filename = models.CharField(max=70, unique=True) - file = models.FileField(upload_to='static/{}'.format(filename), blank=False, - help_text='Please upload static files (images, css, js, etc) one by one') +def get_filename(instance, _): + return 'static/cms/' + str(instance.filename) + + +def validate_filename(value): + if os.path.exists('workshop_app/static/' + value): + raise ValidationError('Static file with that name already exists! Please choose a unique name. You may use ' + 'foldername/filename to upload to a folder') + + +class StaticFile(models.Model): + filename = models.CharField(max_length=70, unique=True, validators=[validate_filename]) + file = models.FileField(upload_to=get_filename, storage=FileSystemStorage(location='workshop_app', base_url='/'), + blank=False, + help_text='Please upload static file (image, css, js, etc). This file will be accessible ' + 'at static/cms/filename') def __str__(self): return self.filename diff --git a/cms/templates/cms_base.html b/cms/templates/cms_base.html new file mode 100644 index 0000000..c2683f3 --- /dev/null +++ b/cms/templates/cms_base.html @@ -0,0 +1,63 @@ +<!DOCTYPE html> +<html lang="en"> +<head> + <meta charset="UTF-8"> + <title>{{ page.title }}</title> + <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" + integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous"> + + <script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" + integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" + crossorigin="anonymous"></script> + <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" + integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" + crossorigin="anonymous"></script> + {{ page.imports | safe }} + <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" + integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" + crossorigin="anonymous"></script> + <link rel="stylesheet" href="{{ URL_ROOT }}/static/workshop_app/css/base.css" type="text/css"/> +</head> +<body> + +<nav class=" navbar-custom navbar navbar-expand-lg bg-light"> + <a class="navbar-brand" href="/">FOSSEE Python Workshops</a> + <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" + aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation"> + <span class="navbar-toggler-icon"></span> + </button> + + <div class="collapse navbar-collapse" id="navbarSupportedContent"> + <ul class="navbar-nav mr-auto"> + </ul> + <div class="my-2 my-lg-0"> + <ul class="navbar-nav mr-auto"> + {% for nav in navs %} + <li class="nav-item"> + {% if nav.subnavs %} + <ul class="navbar-nav mr-auto"> + <li class="nav-item dropdown"> + <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" + data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"><span + class="material-icons" style="vertical-align: middle">{{ nav.name }}</span> + </a> + <div class="dropdown-menu" aria-labelledby="navbarDropdown"> + {% for subnav in nav.subnavs %} + <a class="dropdown-item" href="{{ subnav.link }}">{{ subnav.name }}</a> + {% endfor %} + </div> + </li> + </ul> + {% else %} + <a class="nav-link" href="{{ nav.link }}">{{ nav.name }}</a> + {% endif %} + </li> + {% endfor %} + + </ul> + </div> + </div> +</nav> +{{ page.content | safe }} +</body> +</html>
\ No newline at end of file diff --git a/cms/urls.py b/cms/urls.py new file mode 100644 index 0000000..b0c7612 --- /dev/null +++ b/cms/urls.py @@ -0,0 +1,8 @@ +from django.conf.urls import url + +from cms import views + +urlpatterns = [ + url('^$', views.home, name='home'), + url('^(?P<permalink>.+)$', views.home, name='home') +]
\ No newline at end of file diff --git a/cms/views.py b/cms/views.py index 91ea44a..8a324f4 100644 --- a/cms/views.py +++ b/cms/views.py @@ -1,3 +1,30 @@ +# Create your views here. +from django.forms import model_to_dict +from django.http import Http404 from django.shortcuts import render -# Create your views here. +from cms.models import Page, Nav, SubNav + + +def home(request, permalink=''): + if permalink == '': + permalink = 'home' + page = Page.objects.filter(permalink=permalink) + nav_objs = Nav.objects.all().order_by('-position') + subnav_objects = SubNav.objects.all() + + navs = [] + + for nav in nav_objs: + nav_obj = model_to_dict(nav) + nav_obj['subnavs'] = subnav_objects.filter(nav=nav).order_by('position') + navs.insert(-1, nav_obj) + + print(navs) + + if page.exists(): + page = page.first() + else: + raise Http404() + + return render(request, 'cms_base.html', {'page': page, 'navs': navs}) |