From 0864edd921cb3eb66bbcbf4bb518502dc027d972 Mon Sep 17 00:00:00 2001 From: Jeremy Carbaugh Date: Fri, 29 Apr 2011 11:02:32 -0400 Subject: [PATCH] allow null editors --- markupwiki/models.py | 24 +++++++++++++++++++----- markupwiki/views.py | 8 +++++--- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/markupwiki/models.py b/markupwiki/models.py index 1b2d8ab..8ff4517 100644 --- a/markupwiki/models.py +++ b/markupwiki/models.py @@ -34,7 +34,7 @@ ARTICLE_STATUSES = ( class Article(models.Model): 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) redirect_to = models.ForeignKey('self', null=True) @@ -50,6 +50,11 @@ class Article(models.Model): if '/' in self.title: 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): return reverse('view_article', args=[self.title]) @@ -68,21 +73,25 @@ class Article(models.Model): else: 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 lock = cache.get(cache_key) if lock: if release: cache.delete(cache_key) - return lock == user.id + return lock == lock_id if not release: - cache.set(cache_key, user.id, WRITE_LOCK_SECONDS) + cache.set(cache_key, lock_id, WRITE_LOCK_SECONDS) return True class ArticleVersion(models.Model): 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() body = MarkupField(default_markup_type=DEFAULT_MARKUP_TYPE, markup_choices=WIKI_MARKUP_TYPES, @@ -98,6 +107,11 @@ class ArticleVersion(models.Model): def __unicode__(self): 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): return reverse('article_version', args=[self.article.title, self.number]) diff --git a/markupwiki/views.py b/markupwiki/views.py index 57eb722..5e226d7 100644 --- a/markupwiki/views.py +++ b/markupwiki/views.py @@ -115,11 +115,13 @@ def edit_article(request, title): form = ArticleForm() elif request.method == 'POST': form = ArticleForm(request.POST) + user = None if request.user.is_anonymous() else request.user + if form.is_valid(): if not article: # if article doesn't exist create it and start num at 0 article = Article.objects.create(title=title, - creator=request.user) + creator=user) num = 0 else: 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 version = form.save(False) version.article = article - version.author = request.user + version.author = user version.number = num 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 return redirect(article)