diff options
-rw-r--r-- | askbot/context.py | 1 | ||||
-rw-r--r-- | askbot/doc/source/conf.py | 2 | ||||
-rw-r--r-- | askbot/management/commands/fix_answer_counts.py | 5 | ||||
-rw-r--r-- | askbot/models/__init__.py | 17 | ||||
-rw-r--r-- | askbot/models/question.py | 88 | ||||
-rw-r--r-- | askbot/setup_templates/settings.py | 1 | ||||
-rw-r--r-- | askbot/skins/default/templates/question.html | 28 | ||||
-rw-r--r-- | askbot/skins/default/templates/user_stats.html | 2 | ||||
-rw-r--r-- | askbot/tests/db_api_tests.py | 8 | ||||
-rw-r--r-- | askbot/tests/email_alert_tests.py | 2 | ||||
-rw-r--r-- | askbot/views/readers.py | 2 | ||||
-rw-r--r-- | askbot/views/writers.py | 1 |
12 files changed, 85 insertions, 72 deletions
diff --git a/askbot/context.py b/askbot/context.py index 4a090309..447f46dc 100644 --- a/askbot/context.py +++ b/askbot/context.py @@ -4,7 +4,6 @@ def application_settings(context): my_settings = askbot_settings.as_dict() my_settings['LANGUAGE_CODE'] = settings.LANGUAGE_CODE my_settings['ASKBOT_URL'] = settings.ASKBOT_URL - #print '\n'.join(sorted(my_settings.keys())) return {'settings':my_settings} def auth_processor(request): diff --git a/askbot/doc/source/conf.py b/askbot/doc/source/conf.py index 0062228b..0a02d298 100644 --- a/askbot/doc/source/conf.py +++ b/askbot/doc/source/conf.py @@ -37,8 +37,6 @@ sys.path.append(PROJECT_DIR) sys.path.append(parent_dir(__file__, 4)) sys.path.append(parent_dir(__file__, 2)) -print sys.path - # -- General configuration ----------------------------------------------------- # If your documentation needs a minimal Sphinx version, state it here. diff --git a/askbot/management/commands/fix_answer_counts.py b/askbot/management/commands/fix_answer_counts.py index 3bb7f7d4..3e438ae7 100644 --- a/askbot/management/commands/fix_answer_counts.py +++ b/askbot/management/commands/fix_answer_counts.py @@ -25,7 +25,4 @@ class Command(NoArgsCommand): self.remove_save_signals() questions = models.Question.objects.all() for question in questions: - #todo: must be simply - #question.update_answer_count() - models.Question.objects.update_answer_count(question) - question.save() + question.update_answer_count() diff --git a/askbot/models/__init__.py b/askbot/models/__init__.py index 9eaea23e..b5121af1 100644 --- a/askbot/models/__init__.py +++ b/askbot/models/__init__.py @@ -684,9 +684,6 @@ def user_post_comment( comment = body_text, added_at = timestamp, ) - #print comment - #print 'comment id is %s' % comment.id - #print len(Comment.objects.all()) return comment @auto_now_timestamp @@ -743,7 +740,7 @@ def user_delete_answer( answer.deleted_at = timestamp answer.save() - Question.objects.update_answer_count(answer.question) + answer.question.update_answer_count() logging.debug('updated answer count to %d' % answer.question.answer_count) signals.delete_question_or_answer.send( @@ -838,7 +835,7 @@ def user_restore_post( post.deleted_at = None post.save() if isinstance(post, Answer): - Question.objects.update_answer_count(post.question) + post.question.update_answer_count() elif isinstance(post, Question): #todo: make sure that these tags actually exist #some may have since been deleted for good @@ -1132,7 +1129,6 @@ def user_get_status_display(self, soft = False): elif self.is_approved(): return _('Approved User') else: - print 'vot blin' raise ValueError('Unknown user status') @@ -1149,16 +1145,13 @@ def user_can_moderate_user(self, other): def user_get_q_sel_email_feed_frequency(self): - #print 'looking for frequency for user %s' % self try: feed_setting = EmailFeedSetting.objects.get( subscriber=self, feed_type='q_sel' ) except Exception, e: - #print 'have error %s' % e.message raise e - #print 'have freq=%s' % feed_setting.frequency return feed_setting.frequency def get_messages(self): @@ -1454,9 +1447,7 @@ def format_instant_notification_body( 'origin_post_title': origin_post.title, 'user_subscriptions_url': user_subscriptions_url, } - output = template.render(Context(update_data)) - #print output - return output + return template.render(Context(update_data)) #todo: action def send_instant_notifications_about_activity_in_post( @@ -1650,7 +1641,7 @@ def update_last_seen(instance, created, **kwargs): if instance.activity_type == const.TYPE_ACTIVITY_EMAIL_UPDATE_SENT: return user = instance.user - user.last_seen = datetime.datetime.now() + user.last_seen = instance.active_at user.save() diff --git a/askbot/models/question.py b/askbot/models/question.py index f194e599..1be112e7 100644 --- a/askbot/models/question.py +++ b/askbot/models/question.py @@ -207,16 +207,6 @@ class QuestionManager(models.Manager): authors.update(question.get_author_list(**kwargs)) return list(authors) - #todo: why not make this into a method of Question class? - # also it is actually strange - why do we need the answer_count - # field if the count depends on who is requesting this? - def update_answer_count(self, question): - """ - Executes an UPDATE query to update denormalised data with the - number of answers the given question has. - """ - question.answer_count = question.get_answers().count() - def update_view_count(self, question): """ update counter+1 when user browse question page @@ -229,31 +219,6 @@ class QuestionManager(models.Manager): """ self.filter(id=question.id).update(favourite_count = FavoriteQuestion.objects.filter(question=question).count()) - def get_similar_questions(self, question): - """ - Get 10 similar questions for given one. - This will search the same tag list for give question(by exactly same string) first. - Questions with the individual tags will be added to list if above questions are not full. - """ - #print datetime.datetime.now() - - manager = self - - def get_data(): - questions = list(manager.filter(tagnames = question.tagnames, deleted=False).all()) - - tags_list = question.tags.all() - for tag in tags_list: - extend_questions = manager.filter(tags__id = tag.id, deleted=False)[:50] - for item in extend_questions: - if item not in questions and len(questions) < 10: - questions.append(item) - - #print datetime.datetime.now() - return questions - - return LazyList(get_data) - class Question(content.Content, DeletableContent): title = models.CharField(max_length=300) tags = models.ManyToManyField('Tag', related_name='questions') @@ -287,6 +252,59 @@ class Question(content.Content, DeletableContent): parse = parse_post_text parse_and_save = parse_and_save_post + def update_answer_count(self, save = True): + """updates the denormalized field 'answer_count' + on the question + """ + self.answer_count = self.get_answers().count() + if save: + self.save() + + + def get_similar_questions(self): + """ + Get 10 similar questions for given one. + Questions with the individual tags will be added to list if above questions are not full. + + This function has a limitation that it will + retrieve only 100 records then select 10 most similar + from that list as querying entire database may + be very expensive - this function will benefit from + some sort of optimization + """ + #print datetime.datetime.now() + + def get_data(): + + tags_list = self.tags.all() + similar_questions = self.__class__.objects.filter( + tags__in = self.tags.all() + ).exclude( + id = self.id + )[:100] + similar_questions = list(similar_questions) + output = list() + for question in similar_questions: + question.similarity = self.get_similarity( + other_question = question + ) + #sort in reverse order - x and y are interchanged in cmp() call + similar_questions.sort(lambda x,y: cmp(y.similarity, x.similarity)) + if len(similar_questions) > 10: + return similar_questions[:10] + else: + return similar_questions + + return LazyList(get_data) + + def get_similarity(self, other_question = None): + """return number of tags in the other question + that overlap with the current question (self) + """ + my_tags = set(self.tagnames.split(' ')) + others_tags = set(other_question.tagnames.split(' ')) + return len(my_tags & others_tags) + def update_tags(self, tagnames, user): """ Updates Tag associations for a question to match the given diff --git a/askbot/setup_templates/settings.py b/askbot/setup_templates/settings.py index 4117880e..86ddbdad 100644 --- a/askbot/setup_templates/settings.py +++ b/askbot/setup_templates/settings.py @@ -33,6 +33,7 @@ EMAIL_SUBJECT_PREFIX = '' EMAIL_HOST='' EMAIL_PORT='' EMAIL_USE_TLS=False +EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend' # Local time zone for this installation. Choices can be found here: # http://en.wikipedia.org/wiki/List_of_tz_zones_by_name diff --git a/askbot/skins/default/templates/question.html b/askbot/skins/default/templates/question.html index 0525bcf0..881a36f0 100644 --- a/askbot/skins/default/templates/question.html +++ b/askbot/skins/default/templates/question.html @@ -133,7 +133,7 @@ {% comment %}todo: here we have important case to reset search state{% endcomment %} <div id="question-tags" class="tags"> {% for tag in question.tagname_list %} - <a href="{% url questions %}?tags={{tag|urlencode}}&start_over=true" + <a href="{% url questions %}?tags={{tag|urlencode}}&start_over=true" class="post-tag" title="{% blocktrans %}see questions tagged '{{tag}}'{% endblocktrans %}" rel="tag">{{ tag }}</a> {% endfor %} @@ -472,7 +472,7 @@ </p> <p class="tags" > {% for tag in tags %} - <a href="{% url questions %}?tags={{tag.name|urlencode}}&start_over=true" + <a href="{% url questions %}?tags={{tag.name|urlencode}}&start_over=true" title="{% blocktrans with tag.name as tag_name %}see questions tagged '{{tag_name}}'{% endblocktrans %}" rel="tag">{{ tag.name }}</a> <span class="tag-number">×{{ tag.used_count|intcomma }}</span><br/> @@ -489,20 +489,22 @@ </p> </div> {% endcache %} -{% cache 1800 related_questions question.id LANGUAGE_CODE %} -<div class="boxC"> - <h3 class="subtitle">{% trans "Related questions" %}</h3> - <div class="questions-related"> +{% if similar_questions.data %} + {% cache 1800 related_questions question.id LANGUAGE_CODE %} + <div class="boxC"> + <h3 class="subtitle">{% trans "Related questions" %}</h3> + <div class="questions-related"> - {% for question in similar_questions.data %} - <p> - <a href="{{ question.get_absolute_url }}">{{ question.get_question_title }}</a> - </p> - {% endfor %} + {% for question in similar_questions.data %} + <p> + <a href="{{ question.get_absolute_url }}">{{ question.get_question_title }}</a> + </p> + {% endfor %} + </div> </div> -</div> -{% endcache %} + {% endcache %} +{% endif %} {% endblock %} diff --git a/askbot/skins/default/templates/user_stats.html b/askbot/skins/default/templates/user_stats.html index 05a629cf..23d8ddb8 100644 --- a/askbot/skins/default/templates/user_stats.html +++ b/askbot/skins/default/templates/user_stats.html @@ -70,7 +70,7 @@ {% for tag in user_tags%} <a rel="tag" title="{% blocktrans with tag.name as tag_name %}see other questions with {{view_user}}'s contributions tagged '{{ tag_name }}' {% endblocktrans %}" - href="{% url questions %}?tags={{tag|urlencode}}&author={{view_user.id}}&start_over=true">{{tag.name}}</a> + href="{% url questions %}?tags={{tag|urlencode}}&author={{view_user.id}}&start_over=true">{{tag.name}}</a> <span class="tag-number">× {{ tag.user_tag_usage_count|intcomma }}</span><br/> {% if forloop.counter|divisibleby:"10" %} </td> diff --git a/askbot/tests/db_api_tests.py b/askbot/tests/db_api_tests.py index e17c55f8..96296ec3 100644 --- a/askbot/tests/db_api_tests.py +++ b/askbot/tests/db_api_tests.py @@ -73,6 +73,11 @@ class DBApiTests(AskbotTestCase): self.post_answer(question = self.question) self.user.delete_answer(self.answer) self.assert_post_is_deleted(self.answer) + saved_question = models.Question.objects.get(id = self.question.id) + self.assertEquals( + saved_question.answer_count, + 0 + ) def test_restore_answer(self): self.post_answer() @@ -91,4 +96,5 @@ class DBApiTests(AskbotTestCase): answer = self.question.answers.all()[0] self.assert_post_is_not_deleted(answer) self.assertTrue(answer_count == 1) - self.assertTrue(self.question.answer_count == 1) + saved_question = models.Question.objects.get(id = self.question.id) + self.assertTrue(saved_question.answer_count == 1) diff --git a/askbot/tests/email_alert_tests.py b/askbot/tests/email_alert_tests.py index 48e32240..8b17b5aa 100644 --- a/askbot/tests/email_alert_tests.py +++ b/askbot/tests/email_alert_tests.py @@ -262,6 +262,8 @@ class EmailAlertTests(TestCase): self.assertEqual(len(outbox), expected['message_count'], error_message) if expected['message_count'] > 0: if len(outbox) > 0: + error_message = 'expected recipient %s found %s' % \ + (self.target_user.email, outbox[0].recipients()[0]) self.assertEqual( outbox[0].recipients()[0], self.target_user.email, diff --git a/askbot/views/readers.py b/askbot/views/readers.py index f903d3db..274aa172 100644 --- a/askbot/views/readers.py +++ b/askbot/views/readers.py @@ -373,7 +373,7 @@ def question(request, id):#refactor - long subroutine. display question body, an 'tags' : question.tags.all(), 'tab_id' : view_id, 'favorited' : favorited, - 'similar_questions' : Question.objects.get_similar_questions(question), + 'similar_questions' : question.get_similar_questions(), 'context' : { 'is_paginated' : True, 'pages': objects_list.num_pages, diff --git a/askbot/views/writers.py b/askbot/views/writers.py index fcadbe90..c1156170 100644 --- a/askbot/views/writers.py +++ b/askbot/views/writers.py @@ -216,7 +216,6 @@ def edit_question(request, id): try: request.user.assert_can_edit_question(question) if request.method == 'POST': - print request.POST if 'select_revision' in request.POST:#revert-type edit # user has changed revistion number revision_form = forms.RevisionForm(question, latest_revision, request.POST) |