From 909a0d97eee9b602d8107f413945ab979a300cbe Mon Sep 17 00:00:00 2001 From: j Date: Fri, 8 Dec 2017 22:43:35 +0100 Subject: [PATCH] use existing relations --- content/admin.py | 26 +++++++---- content/models.py | 83 +++++++++++++++++++++++++---------- content/static/css/main.css | 8 ++-- content/templates/detail.html | 51 ++++++++++++++++++++- 4 files changed, 132 insertions(+), 36 deletions(-) diff --git a/content/admin.py b/content/admin.py index 46c6a11..282018a 100644 --- a/content/admin.py +++ b/content/admin.py @@ -4,12 +4,8 @@ from __future__ import unicode_literals from django.contrib import admin # Register your models here. -from models import * +from . import models - -class ContentParentsInline(admin.TabularInline): - model = ContentContent - fk_name = 'contentid1' ''' class SubdomainInline(admin.StackedInline): model = Subdomain @@ -21,16 +17,30 @@ class ServerAdmin(admin.ModelAdmin): pass ''' +class ResourcesAdmin(admin.ModelAdmin): + search_fields = ['href'] + list_filter = ['mime'] + +admin.site.register(models.Resources, ResourcesAdmin) + + +class ResourcesInline(admin.StackedInline): + model = models.ContentResource + extra = 2 # how many rows to show + + class ContentAdmin(admin.ModelAdmin): save_on_top = True list_display = ('__unicode__', 'datestart', 'type') - list_filter = ['datestart', 'type'] + list_filter = ['datestart', 'type', 'view'] search_fields = ['title', 'body', 'header'] - inlines = [ContentParentsInline] + raw_id_fields = ['parent'] + + inlines = (ResourcesInline,) # inlines = [SubdomainInline, DomainAliasInline] # list_display = ('url', 'server', 'manage_nameserver', 'domain_registrar', 'email', 'is_active') # list_editable = ('server', 'manage_nameserver', 'domain_registrar', 'email', 'is_active') -admin.site.register(Content, ContentAdmin) +admin.site.register(models.Content, ContentAdmin) diff --git a/content/models.py b/content/models.py index 7137678..9d6f7dd 100644 --- a/content/models.py +++ b/content/models.py @@ -43,10 +43,11 @@ class Comments(models.Model): #not used name = models.CharField(max_length=150, blank=True, null=True) email = models.CharField(max_length=255, blank=True, null=True) personid = models.IntegerField(db_column='personID', blank=True, null=True) # Field name made lowercase. - dateadded = models.DateTimeField(db_column='dateAdded') # Field name made lowercase. - datemodified = models.DateTimeField(db_column='dateModified', blank=True, null=True) # Field name made lowercase. - parentid = models.IntegerField(db_column='parentID', blank=True, null=True) # Field name made lowercase. - contentid = models.IntegerField(db_column='contentID') # Field name made lowercase. + dateadded = models.DateTimeField(db_column='dateAdded', auto_now_add=True) + datemodified = models.DateTimeField(db_column='dateModified', blank=True, null=True, auto_now=True) + parentid = models.IntegerField(db_column='parentID', blank=True, null=True) + parent = models.ForeignKey('Comments', null=True, blank=True, related_name='comments', db_index=True, db_column='parentId') + content = models.ForeignKey('Content', null=True, blank=True, related_name='comments', db_index=True, db_column='contentID') ip = models.CharField(db_column='IP', max_length=50, blank=True, null=True) # Field name made lowercase. class Meta: @@ -71,16 +72,19 @@ class Content(models.Model): postedby = models.CharField(db_column='postedBy', max_length=50, blank=True, null=True) # Field name made lowercase. datestart = models.DateField(db_column='dateStart', blank=True, null=True) # Field name made lowercase. dateend = models.DateField(db_column='dateEnd', blank=True, null=True) # Field name made lowercase. - dateadded = models.DateTimeField(db_column='dateAdded') # Field name made lowercase. - datemodified = models.DateTimeField(db_column='dateModified', blank=True, null=True) # Field name made lowercase. - published = models.IntegerField() + dateadded = models.DateTimeField(db_column='dateAdded', auto_now_add=True) + datemodified = models.DateTimeField(db_column='dateModified', blank=True, null=True, auto_now=True) + + published = models.BooleanField() view = models.ForeignKey("Views", null=True, blank=True, db_column="view") place = models.CharField(max_length=255, null=True, blank=True) - parentid = models.ForeignKey("Content", null=True, db_column='parentID', blank=True, limit_choices_to={'type_id': 3}, related_name="please_run") # Field name made lowercase. - parents = models.ManyToManyField('Content', through='ContentContent', related_name= "children") + + parent = models.ForeignKey('Content', null=True, blank=True, related_name='children', db_index=True, db_column='parentId') + + resources = models.ManyToManyField('Resources', through='ContentResource', related_name="content") def __unicode__(self): - return self.title + return self.title or 'Untitled' class Meta: managed = True @@ -92,12 +96,13 @@ class Content(models.Model): return settings.IMAGE_PREFIX + self.image def get_absolute_url(self): - return reverse('content', kwargs={'shortname': self.shortname}) - + if self.shortname: + return reverse('content', kwargs={'shortname': self.shortname}) +''' class ContentContent(models.Model): - contentid1 = models.ForeignKey("content", db_column='contentID1', related_name="child") # Field name made lowercase. - contentid2 = models.ForeignKey("content", db_column='contentID2', related_name="parent") # Field name made lowercase. + contentid1 = models.ForeignKey('Content', db_column='contentID1', related_name='child') + contentid2 = models.ForeignKey('Content', db_column='contentID2', related_name='parent') def __unicode__(self): return "%s is child of %s" % (self.contentid1.title, self.contentid2.title,) @@ -105,11 +110,11 @@ class ContentContent(models.Model): class Meta: managed = False db_table = 'content_content' - +''' class ContentKeyword(models.Model): - contentid = models.IntegerField(db_column='contentID') # Field name made lowercase. - keywordid = models.IntegerField(db_column='keywordID') # Field name made lowercase. + contentid = models.ForeignKey('Content', db_column='contentID') + resourceid = models.ForeignKey('Keywords', db_column='keywordID') class Meta: managed = False @@ -117,8 +122,11 @@ class ContentKeyword(models.Model): class ContentResource(models.Model): - contentid = models.IntegerField(db_column='contentID') # Field name made lowercase. - resourceid = models.IntegerField(db_column='resourceID') # Field name made lowercase. + content = models.ForeignKey('Content', db_column='contentID') + resource = models.ForeignKey('Resources', db_column='resourceID') + + def __unicode__(self): + return self.resource.href class Meta: managed = False @@ -157,14 +165,17 @@ class People(models.Model): #not used bio = models.TextField(blank=True, null=True) type = models.IntegerField() + resources = models.ManyToManyField('Resources', through='PersonResource', related_name='people') + content = models.ManyToManyField('Content', through='PersonContent', related_name='people') + class Meta: managed = False db_table = 'people' class PersonContent(models.Model): - personid = models.IntegerField(db_column='personID') # Field name made lowercase. - contentid = models.IntegerField(db_column='contentID') # Field name made lowercase. + personid = models.ForeignKey("people", db_column="personID") + contentid = models.ForeignKey("content", db_column="contentID") level = models.IntegerField() class Meta: @@ -173,8 +184,8 @@ class PersonContent(models.Model): class PersonResource(models.Model): - personid = models.IntegerField(db_column='personID') # Field name made lowercase. - resourceid = models.IntegerField(db_column='resourceID') # Field name made lowercase. + personid = models.ForeignKey("people", db_column="personID") + resourceid = models.ForeignKey("resources", db_column="resourceID") class Meta: managed = False @@ -192,6 +203,32 @@ class Resources(models.Model): dateadded = models.DateTimeField(db_column='dateAdded') # Field name made lowercase. orderno = models.IntegerField(db_column='orderNo', blank=True, null=True) # Field name made lowercase. + def __unicode__(self): + return self.href + + def get_absolute_url(self): + href = self.href + if not href.startswith('http') and not href.startswith('/'): + href = '/' + href + if href.startswith('/'): + href = 'https://studio.camp' + href + return href + + @property + def is_image(self): + if self.mime: + return self.mime.lower() in ('gif', 'jpeg', 'jpg', 'png') + + @property + def is_audio(self): + if self.mime: + return self.mime.lower() in ('mp3', 'ogg') + + @property + def is_video(self): + if self.mime: + return self.mime.lower() in ('ogv', 'mp4') + class Meta: managed = False db_table = 'resources' diff --git a/content/static/css/main.css b/content/static/css/main.css index cf667d2..40a66d2 100644 --- a/content/static/css/main.css +++ b/content/static/css/main.css @@ -81,6 +81,8 @@ body, h1, h2, h3, h4, h5, h6 { box-shadow: none !important; } - - - +.admin-menu { + position: absolute; + top: 45px; + right: 25px; +} diff --git a/content/templates/detail.html b/content/templates/detail.html index c8c6398..2e63d47 100644 --- a/content/templates/detail.html +++ b/content/templates/detail.html @@ -1,8 +1,11 @@ - - {% extends 'base.html' %} {% block content %} +{% if request.user.is_staff %} +
+ Edit +
+{% endif %}
@@ -10,5 +13,49 @@

{{ content.header|safe }}

{{ content.body|safe }}

+ +{% if content.children.exists %} +Child Nodes:
+ +{% endif %} + +{% if content.parent and content.parent.title %} + Part of:
+ {{content.parent.title}} +{% endif %} + +{% if content.resources.exists %} + +
+ Resources:
+ {% for res in content.resources.all %} +
+ {% if res.is_image %} + + {% elif res.is_audio %} + + {% elif res.is_video %} + + {% else %} + {{res.description|default:res.href}} + {% endif %} +
+ {% endfor %} +
+
+{% endif %}
{% endblock %}