From e811e9d5b152c01428a954a44bbab348fbe374e8 Mon Sep 17 00:00:00 2001 From: Evgeny Fadeev Date: Sat, 20 Nov 2010 22:37:44 -0500 Subject: fixed a bug preventing installation onto clean environment --- askbot/__init__.py | 73 ------------------------- askbot/conf/__init__.py | 6 ++ askbot/management/commands/send_email_alerts.py | 11 ++-- askbot/models/__init__.py | 15 ++--- askbot/models/question.py | 24 +------- askbot/search/state_manager.py | 3 +- askbot/tests/email_alert_tests.py | 4 +- askbot/utils/mail.py | 66 ++++++++++++++++++++++ askbot/views/meta.py | 3 +- askbot/views/readers.py | 5 +- askbot/views/users.py | 15 ++--- 11 files changed, 105 insertions(+), 120 deletions(-) create mode 100644 askbot/utils/mail.py diff --git a/askbot/__init__.py b/askbot/__init__.py index 125dd0be..823fec97 100644 --- a/askbot/__init__.py +++ b/askbot/__init__.py @@ -7,9 +7,6 @@ basic actions on behalf of the forum application import os import smtplib import logging -from django.core import mail -from django.conf import settings as django_settings -from askbot import exceptions def get_install_directory(): """returns path to directory @@ -23,73 +20,3 @@ def get_version(): this version is meaningful for pypi only """ return '0.6.34' - -#todo: maybe send_mail functions belong to models -#or the future API -def send_mail( - subject_line = None, - body_text = None, - recipient_list = None, - activity_type = None, - related_object = None, - headers = None, - raise_on_failure = False, - ): - """sends email message - logs email sending activity - and any errors are reported as critical - in the main log file - - related_object is not mandatory, other arguments - are. related_object (if given, will be saved in - the activity record) - - if raise_on_failure is True, exceptions.EmailNotSent is raised - """ - from askbot.conf import settings as askbot_settings - prefix = askbot_settings.EMAIL_SUBJECT_PREFIX.strip() + ' ' - try: - assert(subject_line is not None) - subject_line = prefix + subject_line - msg = mail.EmailMessage( - subject_line, - body_text, - django_settings.DEFAULT_FROM_EMAIL, - recipient_list, - headers = headers - ) - msg.content_subtype = 'html' - msg.send() - if related_object is not None: - assert(activity_type is not None) - except Exception, e: - logging.critical(unicode(e)) - if raise_on_failure == True: - raise exceptions.EmailNotSent(unicode(e)) - -def mail_moderators(subject_line, body_text): - """sends email to forum moderators and admins - """ - from django.db.models import Q - from askbot.models import User - recipient_list = User.objects.filter( - Q(status='m') | Q(is_superuser=True) - ).values_list('email', flat=True) - recipient_list = set(recipient_list) - - from_email = '' - if hasattr(django_settings, 'DEFAULT_FROM_EMAIL'): - from_email = django_settings.DEFAULT_FROM_EMAIL - - try: - mail.send_mail(subject_line, body_text, from_email, recipient_list) - except smtplib.SMPTException, e: - logging.critical(unicode(e)) - if raise_on_failure == True: - raise exceptions.EmailNotSent(unicode(e)) - -def should_show_sort_by_relevance(): - """True if configuration support sorting - questions by search relevance - """ - return (django_settings.DATABASE_ENGINE == 'postgresql_psycopg2') diff --git a/askbot/conf/__init__.py b/askbot/conf/__init__.py index ac286861..e36cb290 100644 --- a/askbot/conf/__init__.py +++ b/askbot/conf/__init__.py @@ -16,3 +16,9 @@ import askbot.conf.social_sharing #import main settings object from askbot.conf.settings_wrapper import settings +from django.conf import settings as django_settings +def should_show_sort_by_relevance(): + """True if configuration support sorting + questions by search relevance + """ + return (django_settings.DATABASE_ENGINE == 'postgresql_psycopg2') diff --git a/askbot/management/commands/send_email_alerts.py b/askbot/management/commands/send_email_alerts.py index 10aec596..e3c9482f 100644 --- a/askbot/management/commands/send_email_alerts.py +++ b/askbot/management/commands/send_email_alerts.py @@ -14,6 +14,7 @@ from askbot.conf import settings as askbot_settings from django.utils.datastructures import SortedDict from django.contrib.contenttypes.models import ContentType from askbot import const +from askbot.utils.mail import send_mail DEBUG_THIS_COMMAND = False @@ -533,8 +534,8 @@ class Command(NoArgsCommand): else: recipient_email = user.email - askbot.send_mail( - subject_line = subject_line, - body_text = text, - recipient_list = [recipient_email] - ) + send_mail( + subject_line = subject_line, + body_text = text, + recipient_list = [recipient_email] + ) diff --git a/askbot/models/__init__.py b/askbot/models/__init__.py index 4092ac2d..7c84cf4a 100644 --- a/askbot/models/__init__.py +++ b/askbot/models/__init__.py @@ -32,6 +32,7 @@ from askbot import auth from askbot.utils.decorators import auto_now_timestamp from askbot.utils.slug import slugify from askbot.utils.diff import textDiff as htmldiff +from askbot.utils.mail import send_mail from askbot.startup_tests import run_startup_tests run_startup_tests() @@ -1619,13 +1620,13 @@ def send_instant_notifications_about_activity_in_post( ) #todo: this could be packaged as an "action" - a bundle #of executive function with the activity log recording - askbot.send_mail( - subject_line = subject_line, - body_text = body_text, - recipient_list = [user.email], - related_object = origin_post, - activity_type = const.TYPE_ACTIVITY_EMAIL_UPDATE_SENT - ) + send_mail( + subject_line = subject_line, + body_text = body_text, + recipient_list = [user.email], + related_object = origin_post, + activity_type = const.TYPE_ACTIVITY_EMAIL_UPDATE_SENT + ) #todo: move to utils diff --git a/askbot/models/question.py b/askbot/models/question.py index 2dff5694..9ab91688 100644 --- a/askbot/models/question.py +++ b/askbot/models/question.py @@ -10,8 +10,8 @@ from django.core.urlresolvers import reverse from django.contrib.sitemaps import ping_google from django.utils.translation import ugettext as _ import askbot +import askbot.conf from askbot.models.tag import Tag, MarkedTag -from askbot.models import signals from askbot.models.base import AnonymousContent, DeletableContent, ContentRevision from askbot.models.base import parse_post_text, parse_and_save_post from askbot.models import content @@ -120,7 +120,7 @@ class QuestionManager(models.Manager): 'where': ['text_search_vector @@ to_tsquery(%s)'], 'params': ["'" + search_query + "'"] } - if askbot.should_show_sort_by_relevance(): + if askbot.conf.should_show_sort_by_relevance(): if sort_method == 'relevance-desc': extra_kwargs['order_by'] = ['-relevance',] @@ -592,26 +592,6 @@ class Question(content.Content, DeletableContent): text = text ) -# def save(self, **kwargs): -# """ -# Overridden to manually manage addition of tags when the object -# is first saved. -# -# This is required as we're using ``tagnames`` as the sole means of -# adding and editing tags. -# """ -# initial_addition = (self.pk is None) -# -# super(Question, self).save(**kwargs) -# -# if initial_addition: -# tags = Tag.objects.get_or_create_multiple( -# self.get_tag_names(), -# self.author -# ) -# self.tags.add(*tags) -# Tag.objects.update_use_counts(tags) - def get_tag_names(self): """Creates a list of Tag names from the ``tagnames`` attribute.""" return self.tagnames.split(u' ') diff --git a/askbot/search/state_manager.py b/askbot/search/state_manager.py index 53eb22bc..b50a0405 100644 --- a/askbot/search/state_manager.py +++ b/askbot/search/state_manager.py @@ -2,6 +2,7 @@ #that lives in the session and takes care of the state #persistece during the search session import askbot +import askbot.conf from askbot import const from askbot.conf import settings as askbot_settings import logging @@ -152,7 +153,7 @@ class SearchState(object): self.update_value('sort', input_dict) #todo: plug - mysql has no relevance sort - if not askbot.should_show_sort_by_relevance(): + if not askbot.conf.should_show_sort_by_relevance(): if self.sort == 'relevance-desc': self.reset_sort() diff --git a/askbot/tests/email_alert_tests.py b/askbot/tests/email_alert_tests.py index 416cab06..56142805 100644 --- a/askbot/tests/email_alert_tests.py +++ b/askbot/tests/email_alert_tests.py @@ -9,6 +9,7 @@ from django.test import TestCase from django.test.client import Client from askbot.tests import utils from askbot import models +from askbot.utils.mail import mail_moderators def email_alert_test(test_func): """decorator for test methods in EmailAlertTests @@ -710,7 +711,6 @@ class FeedbackTests(utils.AskbotTestCase): def test_mail_moderators(self): """tests askbot.mail_moderators() """ - import askbot - askbot.mail_moderators('subject', 'text') + mail_moderators('subject', 'text') self.assert_feedback_works() diff --git a/askbot/utils/mail.py b/askbot/utils/mail.py new file mode 100644 index 00000000..9dc32d16 --- /dev/null +++ b/askbot/utils/mail.py @@ -0,0 +1,66 @@ +from django.core import mail +from django.conf import settings as django_settings +from askbot.conf import settings as askbot_settings +from askbot import exceptions +#todo: maybe send_mail functions belong to models +#or the future API +def send_mail( + subject_line = None, + body_text = None, + recipient_list = None, + activity_type = None, + related_object = None, + headers = None, + raise_on_failure = False, + ): + """sends email message + logs email sending activity + and any errors are reported as critical + in the main log file + + related_object is not mandatory, other arguments + are. related_object (if given, will be saved in + the activity record) + + if raise_on_failure is True, exceptions.EmailNotSent is raised + """ + prefix = askbot_settings.EMAIL_SUBJECT_PREFIX.strip() + ' ' + try: + assert(subject_line is not None) + subject_line = prefix + subject_line + msg = mail.EmailMessage( + subject_line, + body_text, + django_settings.DEFAULT_FROM_EMAIL, + recipient_list, + headers = headers + ) + msg.content_subtype = 'html' + msg.send() + if related_object is not None: + assert(activity_type is not None) + except Exception, e: + logging.critical(unicode(e)) + if raise_on_failure == True: + raise exceptions.EmailNotSent(unicode(e)) + +def mail_moderators(subject_line, body_text): + """sends email to forum moderators and admins + """ + from django.db.models import Q + from askbot.models import User + recipient_list = User.objects.filter( + Q(status='m') | Q(is_superuser=True) + ).values_list('email', flat=True) + recipient_list = set(recipient_list) + + from_email = '' + if hasattr(django_settings, 'DEFAULT_FROM_EMAIL'): + from_email = django_settings.DEFAULT_FROM_EMAIL + + try: + mail.send_mail(subject_line, body_text, from_email, recipient_list) + except smtplib.SMPTException, e: + logging.critical(unicode(e)) + if raise_on_failure == True: + raise exceptions.EmailNotSent(unicode(e)) diff --git a/askbot/views/meta.py b/askbot/views/meta.py index d7ce7f4e..0884d3e8 100644 --- a/askbot/views/meta.py +++ b/askbot/views/meta.py @@ -12,6 +12,7 @@ from django.utils.translation import ugettext as _ from django.views import static from askbot.forms import FeedbackForm from askbot.utils.forms import get_next_url +from askbot.utils.mail import mail_moderators from askbot.models import Badge, Award from askbot.skins.loaders import ENV from askbot import skins @@ -54,7 +55,7 @@ def feedback(request): data['message'] = form.cleaned_data['message'] data['name'] = form.cleaned_data.get('name',None) message = render_to_response('feedback_email.txt',data,context_instance=RequestContext(request)) - askbot.mail_moderators(_('Q&A forum feedback'), message) + mail_moderators(_('Q&A forum feedback'), message) msg = _('Thanks for the feedback!') request.user.message_set.create(message=msg) return HttpResponseRedirect(get_next_url(request)) diff --git a/askbot/views/readers.py b/askbot/views/readers.py index 7877cf38..32dc2dde 100644 --- a/askbot/views/readers.py +++ b/askbot/views/readers.py @@ -29,7 +29,7 @@ import askbot from askbot.utils.html import sanitize_html #from lxml.html.diff import htmldiff from askbot.utils.diff import textDiff as htmldiff -from askbot.forms import * +from askbot.forms import AdvancedSearchForm, AnswerForm from askbot.models import * from askbot import const from askbot import auth @@ -40,6 +40,7 @@ from askbot.utils.decorators import profile from askbot.search.state_manager import SearchState from askbot.templatetags import extra_tags from askbot.templatetags import extra_filters +import askbot.conf from askbot.conf import settings as askbot_settings from askbot.skins.loaders import ENV #jinja2 template loading enviroment @@ -301,7 +302,7 @@ def questions(request): 'interesting_tag_names': meta_data.get('interesting_tag_names',None), 'ignored_tag_names': meta_data.get('ignored_tag_names',None), 'sort': search_state.sort, - 'show_sort_by_relevance': askbot.should_show_sort_by_relevance(), + 'show_sort_by_relevance': askbot.conf.should_show_sort_by_relevance(), 'scope': search_state.scope, 'context' : paginator_context, }) diff --git a/askbot/views/users.py b/askbot/views/users.py index c2109cf8..466b2697 100644 --- a/askbot/views/users.py +++ b/askbot/views/users.py @@ -25,6 +25,7 @@ from django.conf import settings as django_settings import askbot from askbot.utils.slug import slugify from askbot.utils.html import sanitize_html +from askbot.utils.mail import send_mail from askbot import auth from askbot import forms from askbot import const @@ -165,13 +166,13 @@ def user_moderate(request, subject): body_text = send_message_form.cleaned_data['body_text'] try: - askbot.send_mail( - subject_line = subject_line, - body_text = body_text, - recipient_list = [subject.email], - headers={'Reply-to':moderator.email}, - raise_on_failure = True - ) + send_mail( + subject_line = subject_line, + body_text = body_text, + recipient_list = [subject.email], + headers={'Reply-to':moderator.email}, + raise_on_failure = True + ) message_sent = True except exceptions.EmailNotSent, e: email_error_message = unicode(e) -- cgit v1.2.3-1-g7c22