Dynastie/models.py

266 lines
9.1 KiB
Python
Raw Normal View History

2012-07-08 20:41:16 +02:00
import os
import shutil
import hashlib
2012-07-15 18:21:26 +02:00
import inspect
2012-07-22 10:47:24 +02:00
from unicodedata import normalize
from re import sub
2012-07-20 21:54:43 +02:00
from datetime import datetime
2012-07-08 16:23:39 +02:00
from django.db import models
from django.contrib.auth.models import User
2012-07-08 20:41:16 +02:00
from django.db.models.signals import post_init, post_delete, post_save
from django.dispatch import receiver
from dynastie.generators import *
2012-07-08 16:23:39 +02:00
class Blog(models.Model):
2012-07-08 20:41:16 +02:00
name = models.CharField(max_length=255, unique=True)
2012-07-08 16:23:39 +02:00
title = models.CharField(max_length=255)
description = models.TextField(max_length=255, blank=True)
keywords = models.TextField(blank=True)
writers = models.ManyToManyField(User)
2012-07-08 20:41:16 +02:00
2012-07-15 18:21:26 +02:00
engines = list()
2012-07-08 20:41:16 +02:00
2012-07-15 18:21:26 +02:00
src_path = ''
output_path = ''
2012-07-20 21:54:43 +02:00
report = ''
2012-07-08 20:41:16 +02:00
2012-07-15 18:21:26 +02:00
def create_paths(self):
self.src_path = 'sites/' + self.name
self.output_path = 'sites/' + self.name + '_output'
def create(self):
2012-07-22 10:47:24 +02:00
self.create_paths()
2012-07-08 20:41:16 +02:00
if not os.path.exists('sites'):
os.mkdir('sites')
2012-07-22 10:47:24 +02:00
if not os.path.exists(self.src_path):
os.mkdir(self.src_path)
2012-07-08 20:41:16 +02:00
2012-07-22 10:47:24 +02:00
if not os.path.exists(self.output_path):
os.mkdir(self.output_path)
2012-07-08 20:41:16 +02:00
def remove(self):
2012-07-15 18:21:26 +02:00
if os.path.exists(self.src_path):
shutil.rmtree(self.src_path)
if os.path.exists(self.output_path):
shutil.rmtree(self.output_path)
2012-07-08 20:41:16 +02:00
def load_generators(self):
2012-07-15 18:21:26 +02:00
if os.path.exists(self.src_path + '/_generators'):
f = open(self.src_path + '/_generators', 'r')
2012-07-08 20:41:16 +02:00
for line in f:
2012-07-22 20:49:11 +02:00
if line.startswith("#"):
continue
2012-07-15 18:21:26 +02:00
self.engines.append(globals()[line.strip()])
2012-07-08 20:41:16 +02:00
f.close()
else:
2012-07-22 20:49:11 +02:00
self.engines.append(globals()['article'])
2012-07-15 18:21:26 +02:00
self.engines.append(globals()['index'])
2012-07-22 20:49:11 +02:00
self.engines.append(globals()['category'])
self.engines.append(globals()['archive'])
2012-07-08 20:41:16 +02:00
def copytree(self, src, dst):
names = os.listdir(src)
errors = []
for name in names:
if name.startswith('_') or name.endswith('~'):
continue
srcname = os.path.join(src, name)
dstname = os.path.join(dst, name)
try:
if os.path.islink(srcname) and not os.path.exists(dstname):
linkto = os.readlink(srcname)
os.symlink(linkto, dstname)
2012-07-15 18:21:26 +02:00
if os.path.isdir(srcname):
if not os.path.exists(dstname):
os.makedirs(dstname)
2012-07-08 20:41:16 +02:00
self.copytree(srcname, dstname)
else:
2012-07-15 18:21:26 +02:00
if os.path.exists(dstname):
src_md5 = hashlib.md5()
f = open(srcname,'rb')
src_md5.update(f.read())
f.close()
dst_md5 = hashlib.md5()
f = open(dstname,'rb')
dst_md5.update(f.read())
f.close()
if src_md5.digest() != dst_md5.digest():
2012-07-20 21:54:43 +02:00
self.report = self.report + 'Update ' + dstname + '<br/>\n'
2012-07-15 18:21:26 +02:00
shutil.copy2(srcname, dstname)
else:
2012-07-20 21:54:43 +02:00
self.report = self.report + 'Add ' + dstname + '<br/>\n'
2012-07-15 18:21:26 +02:00
shutil.copy2(srcname, dstname)
2012-07-08 20:41:16 +02:00
# XXX What about devices, sockets etc.?
except (IOError, os.error), why:
errors.append((srcname, dstname, str(why)))
# catch the Error from the recursive copytree so that we can
# continue with other files
2012-07-15 18:21:26 +02:00
except Exception, err:
2012-07-08 20:41:16 +02:00
errors.extend(err.args[0])
if errors:
2012-07-15 18:21:26 +02:00
raise Exception(errors)
2012-07-08 20:41:16 +02:00
def generate(self):
2012-07-20 21:54:43 +02:00
self.report = '<br/><br/>Generation of ' + datetime.now().strftime("%d/%m/%Y at %H:%M:%S") + '<br/>\n'
2012-07-08 20:41:16 +02:00
self.load_generators()
2012-07-15 18:21:26 +02:00
self.copytree(self.src_path, self.output_path)
2012-07-22 20:49:11 +02:00
generated = []
2012-07-15 18:21:26 +02:00
for engine in self.engines:
2012-07-22 20:49:11 +02:00
if not inspect.ismodule(engine):
continue
2012-07-15 18:21:26 +02:00
for name, obj in inspect.getmembers(engine):
2012-07-22 20:49:11 +02:00
if inspect.isclass(obj) and obj.__module__.startswith("dynastie"):
if obj.__module__ in generated: continue
2012-07-15 18:21:26 +02:00
e = obj()
2012-07-18 11:30:54 +02:00
r = e.generate(self, self.src_path, self.output_path)
2012-07-22 20:49:11 +02:00
generated.append(obj.__module__)
2012-07-18 11:30:54 +02:00
if not r is None:
2012-07-20 21:54:43 +02:00
self.report = self.report + '<br/>\n' + r
2012-07-22 10:47:24 +02:00
2012-07-20 21:54:43 +02:00
return self.report
2012-07-08 16:23:39 +02:00
class Editor(models.Model):
2012-07-08 20:41:16 +02:00
name = models.CharField(max_length=255, unique=True)
2012-07-08 16:23:39 +02:00
class Category(models.Model):
2012-07-08 20:41:16 +02:00
name = models.CharField(max_length=255, unique=True)
name_slug = models.CharField(max_length=255)
2012-07-08 16:23:39 +02:00
parent = models.ForeignKey('self', blank=True, null=True)
description = models.TextField(max_length=255, blank=True)
def slugify(self):
name = normalize('NFKD', self.name).encode('ascii', 'ignore').replace(' ', '-').lower()
#remove `other` characters
name = sub('[^a-zA-Z0-9_-]', '', name)
#nomalize dashes
name = sub('-+', '-', name)
self.name_slug = name
def save(self):
self.slugify()
super(Category, self).save()
2012-07-22 20:49:11 +02:00
def remove(self, blog):
blog.create_paths()
output = blob.output_path
cur_cat = 0
while True:
found = False
filename = output + '/category/'
if cur_cat == 0:
filename = filename + self.name_slug + '.html'
else:
filename = filename + self.name_slug + str(cur_cat) + '.html'
if os.path.exists(filename):
os.unlink(filename)
found = True
filename = filename + '.gz'
if os.path.exists(filename):
os.unlink(filename)
found = True
if not found:
break
cur_cat = cur_cat + 1
filename = output + '/category/'
if len(os.listdir(filename)) == 0:
os.rmdir(filename)
2012-07-08 16:23:39 +02:00
class Tag(models.Model):
2012-07-08 20:41:16 +02:00
name = models.CharField(max_length=255, unique=True)
2012-07-08 16:23:39 +02:00
class Article(models.Model):
title = models.CharField(max_length=255)
2012-07-22 10:47:24 +02:00
title_slug = models.CharField(max_length=255)
2012-07-08 16:23:39 +02:00
category = models.ForeignKey(Category, blank=True, null=True, on_delete=models.SET_NULL)
published = models.BooleanField()
creation_date = models.DateField()
front_page = models.BooleanField()
author = models.ForeignKey(User, null=True, on_delete=models.SET_NULL)
description = models.TextField(max_length=255, blank=True)
keywords = models.TextField(blank=True)
tags = models.ManyToManyField(Tag, blank=True, null=True)
blog = models.ForeignKey(Blog)
2012-07-22 10:47:24 +02:00
def slugify(self):
name = normalize('NFKD', self.title).encode('ascii', 'ignore').replace(' ', '-').lower()
#remove `other` characters
name = sub('[^a-zA-Z0-9_-]', '', name)
#nomalize dashes
name = sub('-+', '-', name)
self.title_slug = name
def save(self):
self.slugify()
super(Article, self).save()
def createArticle(self, content):
b = self.blog
b.create_paths()
output = b.src_path
if not os.path.exists(output + '/_articles'):
os.mkdir(output + '/_articles')
filename = output + '/_articles/' + str(self.pk)
if os.path.exists(filename):
os.unlink(filename)
f = open(filename, 'wb')
f.write(content)
f.close()
def remove(self):
b = self.blog
b.create_paths()
output = b.src_path
filename = output + '/_articles/' + str(self.pk)
if os.path.exists(filename):
os.unlink(filename)
output = b.output_path
filename = output + '/article/'
2012-07-22 17:28:17 +02:00
filename = filename + self.creation_date.strftime("%Y") + '/' + self.creation_date.strftime("%m") + '/'
filename = filename + self.title_slug + '.html'
2012-07-22 10:47:24 +02:00
if os.path.exists(filename):
os.unlink(filename)
filename = filename + '.gz'
if os.path.exists(filename):
os.unlink(filename)
2012-07-22 17:28:17 +02:00
filename = output + '/article/'
2012-07-22 17:28:17 +02:00
filename = filename + self.creation_date.strftime("%Y") + '/' + self.creation_date.strftime("%m") + '/'
if len(os.listdir(filename)) == 0:
os.rmdir(filename)
filename = output + '/article/'
2012-07-22 17:28:17 +02:00
filename = filename + self.creation_date.strftime("%Y") + '/'
if len(os.listdir(filename)) == 0:
os.rmdir(filename)
2012-07-22 10:47:24 +02:00
2012-07-08 16:23:39 +02:00
class Comment(models.Model):
article = models.ForeignKey(Article)
parent = models.ForeignKey('Comment')
date = models.DateField(max_length=255)
author = models.CharField(max_length=255)
email = models.EmailField(max_length=255)
the_comment = models.TextField(max_length=255)
2012-07-08 20:41:16 +02:00
@receiver(post_init, sender=Blog)
2012-07-15 18:21:26 +02:00
def delete_blog_signal(sender, **kwargs):
sender.create_paths()
2012-07-08 16:23:39 +02:00
2012-07-08 20:41:16 +02:00
@receiver(post_delete, sender=Blog)
def delete_blog_signal(sender, **kwargs):
sender.remove()
2012-07-22 10:47:24 +02:00
@receiver(post_delete, sender=Article)
def delete_article_signal(sender, **kwargs):
sender.remove()