WIP: glue @ 80%, needs generators
This commit is contained in:
parent
46f9d19def
commit
df10e56234
2
forms.py
2
forms.py
|
@ -8,7 +8,7 @@ class BlogForm(ModelForm):
|
|||
class ArticleForm(ModelForm):
|
||||
class Meta:
|
||||
model = Article
|
||||
exclude = ('creation_date', 'author', 'blog')
|
||||
exclude = ('creation_date', 'author', 'blog', 'tags')
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super(ArticleForm, self).__init__(*args, **kwargs)
|
||||
|
|
113
models.py
113
models.py
|
@ -1,23 +1,113 @@
|
|||
import os
|
||||
import shutil
|
||||
import hashlib
|
||||
from django.db import models
|
||||
from django.contrib.auth.models import User
|
||||
from django.db.models.signals import post_init, post_delete, post_save
|
||||
from django.dispatch import receiver
|
||||
from dynastie.generators import *
|
||||
|
||||
class Blog(models.Model):
|
||||
name = models.CharField(max_length=255)
|
||||
name = models.CharField(max_length=255, unique=True)
|
||||
title = models.CharField(max_length=255)
|
||||
description = models.TextField(max_length=255, blank=True)
|
||||
keywords = models.TextField(blank=True)
|
||||
writers = models.ManyToManyField(User)
|
||||
|
||||
generators = []
|
||||
|
||||
def create(self):
|
||||
src = 'sites/' + self.name
|
||||
output = 'sites/' + self.name + '_output'
|
||||
|
||||
if not os.path.exists('sites'):
|
||||
os.mkdir('sites')
|
||||
|
||||
self.remove()
|
||||
|
||||
os.mkdir(src)
|
||||
os.mkdir(output)
|
||||
|
||||
def remove(self):
|
||||
src = 'sites/' + self.name
|
||||
output = 'sites/' + self.name + '_output'
|
||||
|
||||
if os.path.exists(src):
|
||||
shutil.rmtree(src)
|
||||
if os.path.exists(output):
|
||||
shutil.rmtree(output)
|
||||
|
||||
def load_generators(self):
|
||||
src = 'sites/' + self.name
|
||||
if os.path.exists(src + '/_generators'):
|
||||
f = open(src + '/_generators', 'r')
|
||||
for line in f:
|
||||
generator.append(globals()[line])
|
||||
f.close()
|
||||
else:
|
||||
generator.append(globals()['dynastie.generators.index'])
|
||||
generator.append(globals()['dynastie.generators.categories'])
|
||||
generator.append(globals()['dynastie.generators.archives'])
|
||||
|
||||
def copytree(self, src, dst):
|
||||
names = os.listdir(src)
|
||||
|
||||
if not os.path.exists(dstname):
|
||||
os.makedirs(dst)
|
||||
|
||||
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)
|
||||
if os.path.isdir(srcname) and not os.path.exists(dstname):
|
||||
self.copytree(srcname, dstname)
|
||||
else:
|
||||
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():
|
||||
copy2(srcname, dstname)
|
||||
|
||||
# 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
|
||||
except Error, err:
|
||||
errors.extend(err.args[0])
|
||||
if errors:
|
||||
raise Error(errors)
|
||||
|
||||
def generate(self):
|
||||
src = 'sites/' + self.name
|
||||
output = 'sites/' + self.name + '_output'
|
||||
|
||||
self.load_generators()
|
||||
self.copytree(src, output)
|
||||
|
||||
class Editor(models.Model):
|
||||
name = models.CharField(max_length=255)
|
||||
name = models.CharField(max_length=255, unique=True)
|
||||
|
||||
class Category(models.Model):
|
||||
name = models.CharField(max_length=255)
|
||||
name = models.CharField(max_length=255, unique=True)
|
||||
parent = models.ForeignKey('self', blank=True, null=True)
|
||||
description = models.TextField(max_length=255, blank=True)
|
||||
|
||||
class Tag(models.Model):
|
||||
name = models.CharField(max_length=255)
|
||||
name = models.CharField(max_length=255, unique=True)
|
||||
|
||||
class Article(models.Model):
|
||||
title = models.CharField(max_length=255)
|
||||
|
@ -39,5 +129,18 @@ class Comment(models.Model):
|
|||
email = models.EmailField(max_length=255)
|
||||
the_comment = models.TextField(max_length=255)
|
||||
|
||||
@receiver(post_init, sender=Blog)
|
||||
def create_blog_signal(sender, **kwargs):
|
||||
sender.create()
|
||||
|
||||
|
||||
@receiver(post_delete, sender=Blog)
|
||||
def delete_blog_signal(sender, **kwargs):
|
||||
sender.remove()
|
||||
|
||||
@receiver(post_delete, sender=Article)
|
||||
def delete_article_signal(sender, **kwargs):
|
||||
sender.blog.generate()
|
||||
|
||||
@receiver(post_save, sender=Article)
|
||||
def save_article_signal(sender, **kwargs):
|
||||
sender.blog.generate()
|
||||
|
|
12
views.py
12
views.py
|
@ -174,9 +174,7 @@ def add_blog(request):
|
|||
form = BlogForm(request.POST) # A form bound to the POST data
|
||||
if form.is_valid(): # All validation rules pass
|
||||
form = form.save()
|
||||
# Process the data in form.cleaned_data
|
||||
# ...
|
||||
return HttpResponseRedirect('/blog/' + str(form.id)) # Redirect after POST
|
||||
return HttpResponseRedirect('/blog') # Redirect after POST
|
||||
else:
|
||||
return HttpResponseRedirect('/blog') # Redirect after POST
|
||||
else:
|
||||
|
@ -196,9 +194,10 @@ def view_blog(request, blog_id):
|
|||
if b is None:
|
||||
raise Http404
|
||||
|
||||
articles = Article.objects.filter(blog=blog_id)
|
||||
articles = Article.objects.filter(blog=b)
|
||||
b = Blog.objects.get(pk=blog_id)
|
||||
form = BlogForm(instance=b)
|
||||
|
||||
|
||||
c = {'blog' : b, 'articles' : articles, 'form' : form}
|
||||
|
||||
return render(request, 'templates/view_blog.html', c);
|
||||
|
@ -220,7 +219,8 @@ def edit_blog(request, blog_id):
|
|||
form.save()
|
||||
else:
|
||||
if 'delete' in request.POST:
|
||||
Blog.objects.get(pk=blog_id).delete()
|
||||
b = Blog.objects.get(pk=blog_id)
|
||||
b.delete()
|
||||
return HttpResponseRedirect('/blog')
|
||||
else:
|
||||
form = BlogForm(instance=b) # An unbound form
|
||||
|
|
Loading…
Reference in New Issue
Block a user