allow null editors
This commit is contained in:
parent
e41f77fa72
commit
0864edd921
@ -34,7 +34,7 @@ ARTICLE_STATUSES = (
|
|||||||
|
|
||||||
class Article(models.Model):
|
class Article(models.Model):
|
||||||
title = models.CharField(max_length=200)
|
title = models.CharField(max_length=200)
|
||||||
creator = models.ForeignKey(User, related_name='wiki_articles')
|
creator = models.ForeignKey(User, related_name='wiki_articles', blank=True, null=True)
|
||||||
status = models.IntegerField(choices=ARTICLE_STATUSES, default=PUBLIC)
|
status = models.IntegerField(choices=ARTICLE_STATUSES, default=PUBLIC)
|
||||||
redirect_to = models.ForeignKey('self', null=True)
|
redirect_to = models.ForeignKey('self', null=True)
|
||||||
|
|
||||||
@ -50,6 +50,11 @@ class Article(models.Model):
|
|||||||
if '/' in self.title:
|
if '/' in self.title:
|
||||||
return self.title.rsplit('/',1)[0]
|
return self.title.rsplit('/',1)[0]
|
||||||
|
|
||||||
|
# def save(self, **kwargs):
|
||||||
|
# if self.creator is not None and self.creator.is_anonymous():
|
||||||
|
# self.creator = None
|
||||||
|
# super(Article, self).save(**kwargs)
|
||||||
|
|
||||||
def get_absolute_url(self):
|
def get_absolute_url(self):
|
||||||
return reverse('view_article', args=[self.title])
|
return reverse('view_article', args=[self.title])
|
||||||
|
|
||||||
@ -68,21 +73,25 @@ class Article(models.Model):
|
|||||||
else:
|
else:
|
||||||
return EDITOR_TEST_FUNC(user)
|
return EDITOR_TEST_FUNC(user)
|
||||||
|
|
||||||
def get_write_lock(self, user, release=False):
|
def get_write_lock(self, user_or_request, release=False):
|
||||||
|
if hasattr(user_or_request, 'session'):
|
||||||
|
lock_id = user_or_request.session.session_key
|
||||||
|
else:
|
||||||
|
lock_id = user_or_request.id
|
||||||
cache_key = 'markupwiki_articlelock_%s' % self.id
|
cache_key = 'markupwiki_articlelock_%s' % self.id
|
||||||
lock = cache.get(cache_key)
|
lock = cache.get(cache_key)
|
||||||
if lock:
|
if lock:
|
||||||
if release:
|
if release:
|
||||||
cache.delete(cache_key)
|
cache.delete(cache_key)
|
||||||
return lock == user.id
|
return lock == lock_id
|
||||||
|
|
||||||
if not release:
|
if not release:
|
||||||
cache.set(cache_key, user.id, WRITE_LOCK_SECONDS)
|
cache.set(cache_key, lock_id, WRITE_LOCK_SECONDS)
|
||||||
return True
|
return True
|
||||||
|
|
||||||
class ArticleVersion(models.Model):
|
class ArticleVersion(models.Model):
|
||||||
article = models.ForeignKey(Article, related_name='versions')
|
article = models.ForeignKey(Article, related_name='versions')
|
||||||
author = models.ForeignKey(User, related_name='article_versions')
|
author = models.ForeignKey(User, related_name='article_versions', blank=True, null=True)
|
||||||
number = models.PositiveIntegerField()
|
number = models.PositiveIntegerField()
|
||||||
body = MarkupField(default_markup_type=DEFAULT_MARKUP_TYPE,
|
body = MarkupField(default_markup_type=DEFAULT_MARKUP_TYPE,
|
||||||
markup_choices=WIKI_MARKUP_TYPES,
|
markup_choices=WIKI_MARKUP_TYPES,
|
||||||
@ -99,5 +108,10 @@ class ArticleVersion(models.Model):
|
|||||||
def __unicode__(self):
|
def __unicode__(self):
|
||||||
return '%s rev #%s' % (self.article, self.number)
|
return '%s rev #%s' % (self.article, self.number)
|
||||||
|
|
||||||
|
# def save(self, **kwargs):
|
||||||
|
# if self.author is not None and self.author.is_anonymous():
|
||||||
|
# self.author = None
|
||||||
|
# super(ArticleVersion, self).save(**kwargs)
|
||||||
|
|
||||||
def get_absolute_url(self):
|
def get_absolute_url(self):
|
||||||
return reverse('article_version', args=[self.article.title, self.number])
|
return reverse('article_version', args=[self.article.title, self.number])
|
||||||
|
@ -115,11 +115,13 @@ def edit_article(request, title):
|
|||||||
form = ArticleForm()
|
form = ArticleForm()
|
||||||
elif request.method == 'POST':
|
elif request.method == 'POST':
|
||||||
form = ArticleForm(request.POST)
|
form = ArticleForm(request.POST)
|
||||||
|
user = None if request.user.is_anonymous() else request.user
|
||||||
|
|
||||||
if form.is_valid():
|
if form.is_valid():
|
||||||
if not article:
|
if not article:
|
||||||
# if article doesn't exist create it and start num at 0
|
# if article doesn't exist create it and start num at 0
|
||||||
article = Article.objects.create(title=title,
|
article = Article.objects.create(title=title,
|
||||||
creator=request.user)
|
creator=user)
|
||||||
num = 0
|
num = 0
|
||||||
else:
|
else:
|
||||||
if not article.get_write_lock(request.user):
|
if not article.get_write_lock(request.user):
|
||||||
@ -133,11 +135,11 @@ def edit_article(request, title):
|
|||||||
# create a new version attached to article specified in name
|
# create a new version attached to article specified in name
|
||||||
version = form.save(False)
|
version = form.save(False)
|
||||||
version.article = article
|
version.article = article
|
||||||
version.author = request.user
|
version.author = user
|
||||||
version.number = num
|
version.number = num
|
||||||
version.save()
|
version.save()
|
||||||
|
|
||||||
article.get_write_lock(request.user, release=True)
|
article.get_write_lock(user or request, release=True)
|
||||||
|
|
||||||
# redirect to view article on save
|
# redirect to view article on save
|
||||||
return redirect(article)
|
return redirect(article)
|
||||||
|
Loading…
Reference in New Issue
Block a user