diff options
author | Evgeny Fadeev <evgeny.fadeev@gmail.com> | 2009-12-09 19:57:49 -0500 |
---|---|---|
committer | Evgeny Fadeev <evgeny.fadeev@gmail.com> | 2009-12-09 20:02:36 -0500 |
commit | fdac57838a27f3837b0ef187a9a3f86f293d29b1 (patch) | |
tree | ba9975b0e22fa02c97608f030395b4c26c31e972 /forum | |
parent | 4be0491e6adef6bf5817545ec9e050555066352f (diff) | |
download | askbot-fdac57838a27f3837b0ef187a9a3f86f293d29b1.tar.gz askbot-fdac57838a27f3837b0ef187a9a3f86f293d29b1.tar.bz2 askbot-fdac57838a27f3837b0ef187a9a3f86f293d29b1.zip |
added sitemap at /sitemap.xml and fixed per tag subscription (hopefully)
Diffstat (limited to 'forum')
-rw-r--r-- | forum/const.py | 2 | ||||
-rw-r--r-- | forum/forms.py | 8 | ||||
-rw-r--r-- | forum/management/commands/send_email_alerts.py | 145 | ||||
-rw-r--r-- | forum/models.py | 5 | ||||
-rw-r--r-- | forum/sitemap.py | 11 | ||||
-rw-r--r-- | forum/urls.py | 5 | ||||
-rw-r--r-- | forum/views.py | 21 |
7 files changed, 113 insertions, 84 deletions
diff --git a/forum/const.py b/forum/const.py index 91b6f5e5..76fd4a24 100644 --- a/forum/const.py +++ b/forum/const.py @@ -89,4 +89,4 @@ CONST = { } #how to filter questions by tags in email digests? -TAG_EMAIL_FILTER_CHOICES = (('ignored', _('exclude ignored tags')),('interesting',_('allow only interesting tags'))) +TAG_EMAIL_FILTER_CHOICES = (('ignored', _('exclude ignored tags')),('interesting',_('allow only selected tags'))) diff --git a/forum/forms.py b/forum/forms.py index ecdd9c95..ad2c5bac 100644 --- a/forum/forms.py +++ b/forum/forms.py @@ -241,6 +241,14 @@ class TagFilterSelectionForm(forms.ModelForm): model = User fields = ('tag_filter_setting',) + def save(self): + before = self.instance.tag_filter_setting + super(TagFilterSelectionForm, self).save() + after = self.instance.tag_filter_setting #User.objects.get(pk=self.instance.id).tag_filter_setting + if before != after: + return True + return False + class EditUserEmailFeedsForm(forms.Form): WN = (('w',_('weekly')),('n',_('no email'))) DWN = (('d',_('daily')),('w',_('weekly')),('n',_('no email'))) diff --git a/forum/management/commands/send_email_alerts.py b/forum/management/commands/send_email_alerts.py index 4a636e87..777381ec 100644 --- a/forum/management/commands/send_email_alerts.py +++ b/forum/management/commands/send_email_alerts.py @@ -38,84 +38,79 @@ class Command(NoArgsCommand): ).exclude( closed=True ) - #todo: still need to add back individually selected and other questions.... - #these may be filtered out by tags - if user.tag_filter_setting == 'ignored': - ignored_tags = Tag.objects.filter(user_selections___reason='bad',user_selections__user=user) - Q_set1 = Q_set1.exclude( tags__in=ignored_tags ) - logging.debug('removed ignored tags') - else: - selected_tags = Tag.objects.filter(user_selections___reason='good',user_selections__user=user) - Q_set1 = Q_set1.filter( tags__in=selected_tags ) - logging.debug('filtered for only selected tags') - - user_feeds = EmailFeedSetting.objects.filter(subscriber=user).exclude(frequency='n') - for feed in user_feeds: - cutoff_time = now - EmailFeedSetting.DELTA_TABLE[feed.frequency] - if feed.reported_at == None or feed.reported_at <= cutoff_time: - Q_set = Q_set1.exclude(last_activity_at__gt=cutoff_time)#report these excluded later - feed.reported_at = now - feed.save()#may not actually report anything, depending on filters below - if feed.feed_type == 'q_sel': - q_sel = Q_set.filter(followed_by=user) - q_sel.cutoff_time = cutoff_time #store cutoff time per query set - elif feed.feed_type == 'q_ask': - q_ask = Q_set.filter(author=user) - q_ask.cutoff_time = cutoff_time - elif feed.feed_type == 'q_ans': - q_ans = Q_set.filter(answers__author=user) - q_ans.cutoff_time = cutoff_time - elif feed.feed_type == 'q_all': - q_all = Q_set - q_all.cutoff_time = cutoff_time - #build list in this order - q_list = OrderedDict() - def extend_question_list(src, dst): - """src is a query set with questions - or an empty list - dst - is an ordered dictionary - """ - if src is None: - return #will not do anything if subscription of this type is not used - cutoff_time = src.cutoff_time - for q in src: - if q in dst: - if cutoff_time < dst[q]['cutoff_time']: - dst[q]['cutoff_time'] = cutoff_time - else: - #initialise a questions metadata dictionary to use for email reporting - dst[q] = {'cutoff_time':cutoff_time} + + user_feeds = EmailFeedSetting.objects.filter(subscriber=user).exclude(frequency='n') + for feed in user_feeds: + cutoff_time = now - EmailFeedSetting.DELTA_TABLE[feed.frequency] + if feed.reported_at == None or feed.reported_at <= cutoff_time: + Q_set = Q_set1.exclude(last_activity_at__gt=cutoff_time)#report these excluded later + feed.reported_at = now + feed.save()#may not actually report anything, depending on filters below + if feed.feed_type == 'q_sel': + q_sel = Q_set.filter(followed_by=user) + q_sel.cutoff_time = cutoff_time #store cutoff time per query set + elif feed.feed_type == 'q_ask': + q_ask = Q_set.filter(author=user) + q_ask.cutoff_time = cutoff_time + elif feed.feed_type == 'q_ans': + q_ans = Q_set.filter(answers__author=user) + q_ans.cutoff_time = cutoff_time + elif feed.feed_type == 'q_all': + if user.tag_filter_setting == 'ignored': + ignored_tags = Tag.objects.filter(user_selections___reason='bad',user_selections__user=user) + q_all = Q_set.exclude( tags__in=ignored_tags ) + else: + selected_tags = Tag.objects.filter(user_selections___reason='good',user_selections__user=user) + q_all = Q_set.filter( tags__in=selected_tags ) + q_all.cutoff_time = cutoff_time + #build list in this order + q_list = OrderedDict() + def extend_question_list(src, dst): + """src is a query set with questions + or an empty list + dst - is an ordered dictionary + """ + if src is None: + return #will not do anything if subscription of this type is not used + cutoff_time = src.cutoff_time + for q in src: + if q in dst: + if cutoff_time < dst[q]['cutoff_time']: + dst[q]['cutoff_time'] = cutoff_time + else: + #initialise a questions metadata dictionary to use for email reporting + dst[q] = {'cutoff_time':cutoff_time} - extend_question_list(q_sel, q_list) - extend_question_list(q_ask, q_list) - extend_question_list(q_ans, q_list) - extend_question_list(q_all, q_list) + extend_question_list(q_sel, q_list) + extend_question_list(q_ask, q_list) + extend_question_list(q_ans, q_list) + extend_question_list(q_all, q_list) - ctype = ContentType.objects.get_for_model(Question) - EMAIL_UPDATE_ACTIVITY = const.TYPE_ACTIVITY_QUESTION_EMAIL_UPDATE_SENT - for q, meta_data in q_list.items(): - #todo use Activity, but first start keeping more Activity records - #act = Activity.objects.filter(content_type=ctype, object_id=q.id) - #because currently activity is not fully recorded to through - #revision records to see what kind modifications were done on - #the questions and answers - try: - update_info = Activity.objects.get(content_type=ctype, - object_id=q.id, - activity_type=EMAIL_UPDATE_ACTIVITY) - emailed_at = update_info.active_at - except Activity.DoesNotExist: - update_info = Activity(user=user, content_object=q, activity_type=EMAIL_UPDATE_ACTIVITY) - emailed_at = datetime.datetime(1970,1,1)#long time ago - except Activity.MultipleObjectsReturned: - raise Exception('server error - multiple question email activities found per user-question pair') + ctype = ContentType.objects.get_for_model(Question) + EMAIL_UPDATE_ACTIVITY = const.TYPE_ACTIVITY_QUESTION_EMAIL_UPDATE_SENT + for q, meta_data in q_list.items(): + #todo use Activity, but first start keeping more Activity records + #act = Activity.objects.filter(content_type=ctype, object_id=q.id) + #because currently activity is not fully recorded to through + #revision records to see what kind modifications were done on + #the questions and answers + try: + update_info = Activity.objects.get(content_type=ctype, + object_id=q.id, + activity_type=EMAIL_UPDATE_ACTIVITY) + emailed_at = update_info.active_at + except Activity.DoesNotExist: + update_info = Activity(user=user, content_object=q, activity_type=EMAIL_UPDATE_ACTIVITY) + emailed_at = datetime.datetime(1970,1,1)#long time ago + except Activity.MultipleObjectsReturned: + raise Exception('server error - multiple question email activities found per user-question pair') - q_rev = QuestionRevision.objects.filter(question=q,\ - revised_at__lt=cutoff_time,\ - revised_at__gt=emailed_at) - q_rev = q_rev.exclude(author=user) - meta_data['q_rev'] = len(q_rev) - if len(q_rev) > 0 and q.added_at == q_rev[0].revised_at: + q_rev = QuestionRevision.objects.filter(question=q,\ + revised_at__lt=cutoff_time,\ + revised_at__gt=emailed_at) + q_rev = q_rev.exclude(author=user) + meta_data['q_rev'] = len(q_rev) + if len(q_rev) > 0 and q.added_at == q_rev[0].revised_at: meta_data['q_rev'] = 0 meta_data['new_q'] = True else: diff --git a/forum/models.py b/forum/models.py index b031b0c8..eb3650f8 100644 --- a/forum/models.py +++ b/forum/models.py @@ -13,6 +13,7 @@ from django.template.defaultfilters import slugify from django.db.models.signals import post_delete, post_save, pre_save from django.utils.translation import ugettext as _ from django.utils.safestring import mark_safe +from django.contrib.sitemaps import ping_google import django.dispatch import settings import logging @@ -208,6 +209,10 @@ class Question(models.Model): """ initial_addition = (self.id is None) super(Question, self).save(**kwargs) + try: + ping_google() + except Exception: + logging.debug('problem pinging google did you register you sitemap with google?') if initial_addition: tags = Tag.objects.get_or_create_multiple(self.tagname_list(), self.author) diff --git a/forum/sitemap.py b/forum/sitemap.py new file mode 100644 index 00000000..dc97a009 --- /dev/null +++ b/forum/sitemap.py @@ -0,0 +1,11 @@ +from django.contrib.sitemaps import Sitemap +from forum.models import Question + +class QuestionsSitemap(Sitemap): + changefreq = 'daily' + priority = 0.5 + def items(self): + return Question.objects.exclude(deleted=True) + + def lastmod(self, obj): + return obj.last_activity_at diff --git a/forum/urls.py b/forum/urls.py index 927e149c..62e70161 100644 --- a/forum/urls.py +++ b/forum/urls.py @@ -3,16 +3,21 @@ from django.conf.urls.defaults import * from django.contrib import admin from forum import views as app from forum.feed import RssLastestQuestionsFeed +from forum.sitemap import QuestionsSitemap from django.utils.translation import ugettext as _ admin.autodiscover() feeds = { 'rss': RssLastestQuestionsFeed } +sitemaps = { + 'questions': QuestionsSitemap +} APP_PATH = os.path.dirname(os.path.dirname(__file__)) urlpatterns = patterns('', url(r'^$', app.index, name='index'), + url(r'^sitemap.xml$', 'django.contrib.sitemaps.views.sitemap', {'sitemaps': sitemaps}), (r'^favicon\.ico$', 'django.views.generic.simple.redirect_to', {'url': '/content/images/favicon.ico'}), (r'^favicon\.gif$', 'django.views.generic.simple.redirect_to', {'url': '/content/images/favicon.gif'}), (r'^content/(?P<path>.*)$', 'django.views.static.serve', diff --git a/forum/views.py b/forum/views.py index c76abf3b..20ca2f6a 100644 --- a/forum/views.py +++ b/forum/views.py @@ -876,7 +876,8 @@ def tags(request): 'previous': tags.previous_page_number(), 'next': tags.next_page_number(), 'base_url' : reverse('tags') + '?sort=%s&' % sortby - }}) + } + }, context_instance=RequestContext(request)) def tag(request, tag): return questions(request, tagname=tag) @@ -1886,12 +1887,14 @@ def user_reputation(request, user_id, user_view): params=[user.id] ).values('positive', 'negative', 'question_id', 'title', 'reputed_at', 'reputation') reputation.query.group_by = ['question_id'] + rep_list = [] for rep in Repute.objects.filter(user=user).order_by('reputed_at'): dic = '[%s,%s]' % (calendar.timegm(rep.reputed_at.timetuple()) * 1000, rep.reputation) rep_list.append(dic) reps = ','.join(rep_list) reps = '[%s]' % reps + return render_to_response(user_view.template_file, { "tab_name": user_view.id, "tab_description": user_view.tab_description, @@ -1957,19 +1960,21 @@ def user_email_subscriptions(request, user_id, user_view): email_feeds_form = EditUserEmailFeedsForm(request.POST) tag_filter_form = TagFilterSelectionForm(request.POST, instance=user) if email_feeds_form.is_valid() and tag_filter_form.is_valid(): - tag_filter_form.save() + + action_status = None + tag_filter_saved = tag_filter_form.save() + if tag_filter_saved: + action_status = _('changes saved') if 'save' in request.POST: - saved = email_feeds_form.save(user) - if saved: + feeds_saved = email_feeds_form.save(user) + if feeds_saved: action_status = _('changes saved') elif 'stop_email' in request.POST: - saved = email_feeds_form.reset().save(user) + email_stopped = email_feeds_form.reset().save(user) initial_values = EditUserEmailFeedsForm.NO_EMAIL_INITIAL email_feeds_form = EditUserEmailFeedsForm(initial=initial_values) - if saved: + if email_stopped: action_status = _('email updates canceled') - if not saved: - action_status = None else: email_feeds_form = EditUserEmailFeedsForm() email_feeds_form.set_initial_values(user) |