fix #71 Permissions of feeds
This commit is contained in:
parent
ba4d4b177e
commit
ba25dd5c87
12 changed files with 60 additions and 132 deletions
djangobb/djangobb_forum
|
@ -1,8 +1,11 @@
|
|||
from django.contrib.syndication.feeds import Feed, FeedDoesNotExist
|
||||
from django.contrib.syndication.views import Feed, FeedDoesNotExist
|
||||
from django.core.exceptions import ObjectDoesNotExist
|
||||
from django.utils.feedgenerator import Atom1Feed
|
||||
from django.core.urlresolvers import reverse
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
from django.contrib.auth.models import User
|
||||
from django.db.models import Q
|
||||
from django.http import Http404
|
||||
|
||||
from djangobb_forum.models import Post, Topic, Forum, Category
|
||||
|
||||
|
@ -25,8 +28,17 @@ class LastPosts(ForumFeed):
|
|||
title_template = 'forum/feeds/posts_title.html'
|
||||
description_template = 'forum/feeds/posts_description.html'
|
||||
|
||||
def items(self):
|
||||
return Post.objects.order_by('-created')[:15]
|
||||
def get_object(self, request):
|
||||
user_groups = request.user.groups.all()
|
||||
if request.user.is_anonymous():
|
||||
user_groups = []
|
||||
allow_forums = Forum.objects.filter(
|
||||
Q(category__groups__in=user_groups) | \
|
||||
Q(category__groups__isnull=True))
|
||||
return allow_forums
|
||||
|
||||
def items(self, allow_forums):
|
||||
return Post.objects.filter(topic__forum__in=allow_forums).order_by('-created')[:15]
|
||||
|
||||
|
||||
class LastTopics(ForumFeed):
|
||||
|
@ -35,18 +47,30 @@ class LastTopics(ForumFeed):
|
|||
title_template = 'forum/feeds/topics_title.html'
|
||||
description_template = 'forum/feeds/topics_description.html'
|
||||
|
||||
def items(self):
|
||||
return Topic.objects.order_by('-created')[:15]
|
||||
def get_object(self, request):
|
||||
user_groups = request.user.groups.all()
|
||||
if request.user.is_anonymous():
|
||||
user_groups = []
|
||||
allow_forums = Forum.objects.filter(
|
||||
Q(category__groups__in=user_groups) | \
|
||||
Q(category__groups__isnull=True))
|
||||
return allow_forums
|
||||
|
||||
def items(self, allow_forums):
|
||||
return Topic.objects.filter(forum__in=allow_forums).order_by('-created')[:15]
|
||||
|
||||
|
||||
class LastPostsOnTopic(ForumFeed):
|
||||
title_template = 'forum/feeds/posts_title.html'
|
||||
description_template = 'forum/feeds/posts_description.html'
|
||||
|
||||
def get_object(self, topics):
|
||||
def get_object(self, request, topics):
|
||||
if len(topics) != 1:
|
||||
raise ObjectDoesNotExist
|
||||
return Topic.objects.get(id=topics[0])
|
||||
topic = Topic.objects.get(id=topics[0])
|
||||
if not topic.forum.category.has_access(request.user):
|
||||
raise Http404
|
||||
return topic
|
||||
|
||||
def title(self, obj):
|
||||
return _('Latest posts on %s topic' % obj.name)
|
||||
|
@ -67,10 +91,13 @@ class LastPostsOnForum(ForumFeed):
|
|||
title_template = 'forum/feeds/posts_title.html'
|
||||
description_template = 'forum/feeds/posts_description.html'
|
||||
|
||||
def get_object(self, forums):
|
||||
def get_object(self, request, forums):
|
||||
if len(forums) != 1:
|
||||
raise ObjectDoesNotExist
|
||||
return Forum.objects.get(id=forums[0])
|
||||
forum = Forum.objects.get(id=forums[0])
|
||||
if not forum.category.has_access(request.user):
|
||||
raise Http404
|
||||
return forum
|
||||
|
||||
def title(self, obj):
|
||||
return _('Latest posts on %s forum' % obj.name)
|
||||
|
@ -91,10 +118,13 @@ class LastPostsOnCategory(ForumFeed):
|
|||
title_template = 'forum/feeds/posts_title.html'
|
||||
description_template = 'forum/feeds/posts_description.html'
|
||||
|
||||
def get_object(self, categories):
|
||||
def get_object(self, request, categories):
|
||||
if len(categories) != 1:
|
||||
raise ObjectDoesNotExist
|
||||
return Category.objects.get(id=categories[0])
|
||||
category = Category.objects.get(id=categories[0])
|
||||
if not category.has_access(request.user):
|
||||
raise Http404
|
||||
return category
|
||||
|
||||
def title(self, obj):
|
||||
return _('Latest posts on %s category' % obj.name)
|
||||
|
|
|
@ -9,7 +9,7 @@ from django.db.models import Q
|
|||
from django.contrib.auth.models import User
|
||||
from django.utils.translation import ugettext as _
|
||||
|
||||
from djangobb_forum.models import Topic, Post, Profile, Reputation, Report, PrivateMessage,\
|
||||
from djangobb_forum.models import Topic, Post, Profile, Reputation, Report, \
|
||||
Forum, Attachment, TZ_CHOICES, PRIVACY_CHOICES
|
||||
from djangobb_forum.markups import bbmarkup
|
||||
from djangobb_forum import settings as forum_settings
|
||||
|
@ -360,32 +360,3 @@ class ReportForm(forms.ModelForm):
|
|||
if commit:
|
||||
report.save()
|
||||
return report
|
||||
|
||||
|
||||
class CreatePMForm(forms.ModelForm):
|
||||
recipient = forms.CharField(label=_('Recipient'))
|
||||
|
||||
class Meta:
|
||||
model = PrivateMessage
|
||||
fields = ['subject', 'body']
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
self.user = kwargs.pop('user', None)
|
||||
super(CreatePMForm, self).__init__(*args, **kwargs)
|
||||
self.fields.keyOrder = ['recipient', 'subject', 'body']
|
||||
self.fields['subject'].widget = widget=forms.TextInput(attrs={'size':'115'})
|
||||
self.fields['body'].widget = forms.Textarea(attrs={'class':'bbcode'})
|
||||
|
||||
def clean_recipient(self):
|
||||
name = self.cleaned_data['recipient']
|
||||
try:
|
||||
user = User.objects.get(username=name)
|
||||
except User.DoesNotExist:
|
||||
raise forms.ValidationError(_('User with login %s does not exist') % name)
|
||||
else:
|
||||
return user
|
||||
|
||||
def save(self):
|
||||
pm = PrivateMessage(src_user=self.user, dst_user=self.cleaned_data['recipient'])
|
||||
pm = forms.save_instance(self, pm)
|
||||
return pm
|
||||
|
|
|
@ -330,51 +330,6 @@ class Report(models.Model):
|
|||
def __unicode__(self):
|
||||
return u'%s %s' % (self.reported_by ,self.zapped)
|
||||
|
||||
|
||||
class PrivateMessage(models.Model):
|
||||
dst_user = models.ForeignKey(User, verbose_name=_('Recipient'), related_name='dst_users')
|
||||
src_user = models.ForeignKey(User, verbose_name=_('Author'), related_name='src_users')
|
||||
read = models.BooleanField(_('Read'), blank=True, default=False)
|
||||
created = models.DateTimeField(_('Created'), auto_now_add=True)
|
||||
markup = models.CharField(_('Markup'), max_length=15, default=forum_settings.DEFAULT_MARKUP, choices=MARKUP_CHOICES)
|
||||
subject = models.CharField(_('Subject'), max_length=255)
|
||||
body = models.TextField(_('Message'))
|
||||
body_html = models.TextField(_('HTML version'))
|
||||
body_text = models.TextField(_('Text version'))
|
||||
|
||||
class Meta:
|
||||
ordering = ['-created']
|
||||
verbose_name = _('Private message')
|
||||
verbose_name_plural = _('Private messages')
|
||||
|
||||
def __unicode__(self):
|
||||
return self.subject
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
if self.markup == 'bbcode':
|
||||
self.body_html = bbmarkup.bbcode(self.body)
|
||||
elif self.markup == 'markdown':
|
||||
self.body_html = unicode(Markdown(self.body, safe_mode='escape'))
|
||||
#self.body_html = markdown(self.body, 'safe')
|
||||
else:
|
||||
raise Exception('Invalid markup property: %s' % self.markup)
|
||||
self.body_html = urlize(self.body_html)
|
||||
if forum_settings.SMILES_SUPPORT:
|
||||
self.body_html = smiles(self.body_html)
|
||||
super(PrivateMessage, self).save(*args, **kwargs)
|
||||
|
||||
@models.permalink
|
||||
def get_absolute_url(self):
|
||||
return ('djangobb:forum_show_pm', [self.id])
|
||||
|
||||
# TODO: summary and part of the save method is the same as in the Post model
|
||||
# move to common functions
|
||||
def summary(self):
|
||||
LIMIT = 50
|
||||
tail = len(self.body) > LIMIT and '...' or ''
|
||||
return self.body[:LIMIT] + tail
|
||||
|
||||
|
||||
class Ban(models.Model):
|
||||
user = models.OneToOneField(User, verbose_name=_('Banned user'), related_name='ban_users')
|
||||
ban_start = models.DateTimeField(_('Ban start'), default=datetime.now)
|
||||
|
|
|
@ -2,8 +2,8 @@ from datetime import datetime
|
|||
|
||||
from django.db.models.signals import post_save, pre_save, post_delete
|
||||
|
||||
from djangobb_forum.subscription import notify_topic_subscribers, notify_pm_recipients
|
||||
from djangobb_forum.models import Topic, Post, PrivateMessage
|
||||
from djangobb_forum.subscription import notify_topic_subscribers
|
||||
from djangobb_forum.models import Topic, Post
|
||||
|
||||
|
||||
def post_saved(instance, **kwargs):
|
||||
|
@ -22,10 +22,6 @@ def post_saved(instance, **kwargs):
|
|||
topic.save(force_update=True)
|
||||
|
||||
|
||||
def pm_saved(instance, **kwargs):
|
||||
notify_pm_recipients(instance)
|
||||
|
||||
|
||||
def topic_saved(instance, **kwargs):
|
||||
created = kwargs.get('created')
|
||||
topic = instance
|
||||
|
@ -38,5 +34,4 @@ def topic_saved(instance, **kwargs):
|
|||
|
||||
|
||||
post_save.connect(post_saved, sender=Post)
|
||||
post_save.connect(pm_saved, sender=PrivateMessage)
|
||||
post_save.connect(topic_saved, sender=Topic)
|
||||
|
|
|
@ -14,12 +14,6 @@ TOPIC_SUBSCRIPTION_TEXT_TEMPLATE = (u"""New reply from %(username)s to topic tha
|
|||
See topic: %(post_url)s
|
||||
Unsubscribe %(unsubscribe_url)s""")
|
||||
|
||||
PM_RECIPIENT_TEXT_TEMPLATE = (u"""User %(username)s have sent your the new private message.
|
||||
---
|
||||
%(message)s
|
||||
---
|
||||
See message online: %(pm_url)s""")
|
||||
|
||||
|
||||
def send_mail(rec_list, subject, text, html=None):
|
||||
"""
|
||||
|
@ -59,16 +53,3 @@ def notify_topic_subscribers(post):
|
|||
}
|
||||
#html_content = html_version(post)
|
||||
send_mail([to_email], subject, text_content)
|
||||
|
||||
|
||||
def notify_pm_recipients(pm):
|
||||
if not pm.read:
|
||||
from djangobb_forum.models import PrivateMessage
|
||||
subject = (u'There are new messages')
|
||||
to_email = pm.dst_user.email
|
||||
text_content = PM_RECIPIENT_TEXT_TEMPLATE % {
|
||||
'username': pm.src_user.username,
|
||||
'message': pm.body_text,
|
||||
'pm_url': absolute_url(pm.get_absolute_url()),
|
||||
}
|
||||
send_mail([to_email], subject, text_content)
|
||||
|
|
|
@ -15,8 +15,8 @@
|
|||
<link rel="stylesheet" type="text/css" href="{{ MEDIA_URL }}forum/js/markitup/skins/markitup/style.css" />
|
||||
<link rel="stylesheet" type="text/css" href="{{ MEDIA_URL }}forum/js/markitup/sets/bbcode/style.css" />
|
||||
|
||||
<link rel="alternate" type="application/atom+xml" href="{% url djangobb:forum_feed "posts" %}" title="{% trans "Latest posts on forum" %}" />
|
||||
<link rel="alternate" type="application/atom+xml" href="{% url djangobb:forum_feed "topics" %}" title="{% trans "Latest topics on forum" %}" />
|
||||
<link rel="alternate" type="application/atom+xml" href="{% url djangobb:forum_posts_feed %}" title="{% trans "Latest posts on forum" %}" />
|
||||
<link rel="alternate" type="application/atom+xml" href="{% url djangobb:forum_topics_feed %}" title="{% trans "Latest topics on forum" %}" />
|
||||
|
||||
<link rel="shortcut icon" href="{{ MEDIA_URL }}forum/favicon.png" type="image/png" />
|
||||
<script type="text/javascript">
|
||||
|
|
|
@ -7,7 +7,7 @@
|
|||
<div class="inbox">
|
||||
<p class="pagelink conl">{% pagination %}</p>
|
||||
<ul><li><a href="{% url djangobb:index %}">{% trans "Root" %} </a></li><li>» {% link topic.forum %} </li><li>» {{ topic }}
|
||||
<a href="{% url djangobb:forum_feed "topic" %}{{ topic.id }}"><img src="{{ MEDIA_URL }}/forum/img/feed-icon-small.png" alt="[RSS Feed]" title="[RSS Feed]" style="vertical-align:middle;" /></a>
|
||||
<a href="{% url djangobb:forum_topic_feed %}{{ topic.id }}"><img src="{{ MEDIA_URL }}/forum/img/feed-icon-small.png" alt="[RSS Feed]" title="[RSS Feed]" style="vertical-align:middle;" /></a>
|
||||
</li></ul>
|
||||
<div class="clearer"></div>
|
||||
</div>
|
||||
|
|
|
@ -16,7 +16,7 @@
|
|||
</div>
|
||||
<div id="vf" class="blocktable">
|
||||
<h2>
|
||||
<a href="{% url djangobb:forum_feed "forum" %}{{ forum.id }}"><img src="{{ MEDIA_URL }}/forum/img/feed-icon-small.png" alt="[RSS Feed]" title="[RSS Feed]" class="rss" /></a>
|
||||
<a href="{% url djangobb:forum_forum_feed forum.id %}"><img src="{{ MEDIA_URL }}/forum/img/feed-icon-small.png" alt="[RSS Feed]" title="[RSS Feed]" class="rss" /></a>
|
||||
<b><span>{{ forum.name }}</span></b>
|
||||
</h2>
|
||||
<div class="box">
|
||||
|
|
|
@ -13,7 +13,7 @@
|
|||
<div class="nosize"><!-- --></div>
|
||||
</div>
|
||||
<div class="tclcon">
|
||||
<h3>{% link forum %} <a href="{% url djangobb:forum_feed "forum" %}{{ forum.id }}/"><img src="{{ MEDIA_URL }}forum/img/feed-icon-small.png" alt="[RSS Feed]" title="[RSS Feed]" class="rss" /></a></h3>
|
||||
<h3>{% link forum %} <a href="{% url djangobb:forum_forum_feed forum.id %}"><img src="{{ MEDIA_URL }}forum/img/feed-icon-small.png" alt="[RSS Feed]" title="[RSS Feed]" class="rss" /></a></h3>
|
||||
{{ forum.description|safe }}
|
||||
<p>
|
||||
{% cache 6000 moderators forum.id %}
|
||||
|
|
|
@ -13,7 +13,7 @@
|
|||
<ul>
|
||||
<li>
|
||||
<a href="{% url djangobb:index %}">{% trans "Root" %} </a></li><li>» {% link topic.forum %} </li><li>» {{ topic.name }}
|
||||
<a href="{% url djangobb:forum_feed "topic" %}{{ topic.id }}/"><img src="{{ MEDIA_URL }}forum/img/feed-icon-small.png" alt="[RSS Feed]" title="[RSS Feed]" class="breadcrumb_rss" /></a>
|
||||
<a href="{% url djangobb:forum_topic_feed topic.id %}"><img src="{{ MEDIA_URL }}forum/img/feed-icon-small.png" alt="[RSS Feed]" title="[RSS Feed]" class="breadcrumb_rss" /></a>
|
||||
</li>
|
||||
</ul>
|
||||
<div class="clearer"></div>
|
||||
|
@ -133,7 +133,7 @@
|
|||
<p class="pagelink conl">{% pagination %}</p>
|
||||
<p class="postlink conr"><a href="{% url djangobb:add_post topic.id %}">{% trans "Reply" %}</a></p>
|
||||
<ul><li><a href="{% url djangobb:index %}">{% trans "Root" %} </a></li><li>» {% link topic.forum %} </li><li>» {{ topic }}
|
||||
<a href="{% url djangobb:forum_feed "topic" %}{{ topic.id }}"><img src="{{ MEDIA_URL }}/forum/img/feed-icon-small.png" alt="[RSS Feed]" title="[RSS Feed]" class="breadcrumb_rss" /></a>
|
||||
<a href="{% url djangobb:forum_topic_feed topic.id %}"><img src="{{ MEDIA_URL }}/forum/img/feed-icon-small.png" alt="[RSS Feed]" title="[RSS Feed]" class="breadcrumb_rss" /></a>
|
||||
</li></ul>
|
||||
{% if user.is_authenticated %}
|
||||
{% if subscribed %}
|
||||
|
|
|
@ -4,14 +4,7 @@ from djangobb_forum import settings as forum_settings
|
|||
from djangobb_forum import views as forum_views
|
||||
from djangobb_forum.feeds import LastPosts, LastTopics, LastPostsOnForum,\
|
||||
LastPostsOnCategory, LastPostsOnTopic
|
||||
|
||||
feeds = {
|
||||
'posts': LastPosts,
|
||||
'topics': LastTopics,
|
||||
'topic': LastPostsOnTopic,
|
||||
'forum': LastPostsOnForum,
|
||||
'category': LastPostsOnCategory,
|
||||
}
|
||||
|
||||
|
||||
urlpatterns = patterns('',
|
||||
|
||||
|
@ -47,12 +40,15 @@ urlpatterns = patterns('',
|
|||
# Subscription
|
||||
url('^subscription/topic/(?P<topic_id>\d+)/delete/$', forum_views.delete_subscription, name='forum_delete_subscription'),
|
||||
url('^subscription/topic/(?P<topic_id>\d+)/add/$', forum_views.add_subscription, name='forum_add_subscription'),
|
||||
|
||||
# Feeds
|
||||
url(r'^feeds/(?P<url>.*)/$', 'django.contrib.syndication.views.feed',
|
||||
{'feed_dict': feeds}, name='forum_feed'),
|
||||
url(r'^feeds/posts/$', LastPosts(), name='forum_posts_feed'),
|
||||
url(r'^feeds/topics/$', LastTopics(), name='forum_topics_feed'),
|
||||
url(r'^feeds/topic/(?P<topics>\d+)/$', LastPostsOnTopic(), name='forum_topic_feed'),
|
||||
url(r'^feeds/forum/(?P<forums>\d+)/$', LastPostsOnForum(), name='forum_forum_feed'),
|
||||
url(r'^feeds/category/(?P<categories>\d+)/$', LastPostsOnCategory(), name='forum_category_feed'),
|
||||
)
|
||||
|
||||
|
||||
### EXTENSIONS ###
|
||||
|
||||
# LOFI Extension
|
||||
|
|
|
@ -19,11 +19,11 @@ from django.db import transaction
|
|||
|
||||
from djangobb_forum.util import render_to, paged, build_form, paginate, set_language
|
||||
from djangobb_forum.models import Category, Forum, Topic, Post, Profile, Reputation,\
|
||||
Report, PrivateMessage, Attachment, PostTracking
|
||||
Report, Attachment, PostTracking
|
||||
from djangobb_forum.forms import AddPostForm, EditPostForm, UserSearchForm,\
|
||||
PostSearchForm, ReputationForm, MailToForm, EssentialsProfileForm,\
|
||||
PersonalProfileForm, MessagingProfileForm, PersonalityProfileForm,\
|
||||
DisplayProfileForm, PrivacyProfileForm, ReportForm, UploadAvatarForm, CreatePMForm
|
||||
DisplayProfileForm, PrivacyProfileForm, ReportForm, UploadAvatarForm
|
||||
from djangobb_forum.markups import bbmarkup
|
||||
from djangobb_forum.templatetags import forum_extras
|
||||
from djangobb_forum import settings as forum_settings
|
||||
|
|
Reference in a new issue