- return render_to_response(user_view.template_file,{\r
- 'moderate_user_form': moderate_user_form,\r
- "tab_name" : user_view.id,\r
- "tab_description" : user_view.tab_description,\r
- "page_title" : user_view.page_title,\r
- "view_user" : user,\r
- "questions" : questions,\r
- "answered_questions" : answered_questions,\r
- "up_votes" : up_votes,\r
- "down_votes" : down_votes,\r
- "total_votes": up_votes + down_votes,\r
- "votes_today_left": votes_total-votes_today,\r
- "votes_total_per_day": votes_total,\r
- "user_tags" : user_tags[:50],\r
- "awards": awards,\r
- "total_awards" : total_awards,\r
- }, context_instance=RequestContext(request))\r
-\r
-def user_recent(request, user_id, user_view):\r
- user = get_object_or_404(User, id=user_id)\r
- def get_type_name(type_id):\r
- for item in TYPE_ACTIVITY:\r
- if type_id in item:\r
- return item[1]\r
-\r
- class Event:\r
- def __init__(self, time, type, title, summary, answer_id, question_id):\r
- self.time = time\r
- self.type = get_type_name(type)\r
- self.type_id = type\r
- self.title = title\r
- self.summary = summary\r
- slug_title = slugify(title)\r
- self.title_link = reverse('question', kwargs={'id':question_id, 'slug':slug_title})\r
- if int(answer_id) > 0:\r
- self.title_link += '#%s' % answer_id\r
-\r
- class AwardEvent:\r
- def __init__(self, time, type, id):\r
- self.time = time\r
- self.type = get_type_name(type)\r
- self.type_id = type\r
- self.badge = get_object_or_404(Badge, id=id)\r
-\r
- activities = []\r
- # ask questions\r
- questions = Activity.objects.extra(\r
- select={\r
- 'title' : 'question.title',\r
- 'question_id' : 'question.id',\r
- 'active_at' : 'activity.active_at',\r
- 'activity_type' : 'activity.activity_type'\r
- },\r
- tables=['activity', 'question'],\r
- where=['activity.content_type_id = %s AND activity.object_id = ' +\r
- 'question.id AND NOT question.deleted AND activity.user_id = %s AND activity.activity_type = %s'],\r
- params=[question_type_id, user_id, TYPE_ACTIVITY_ASK_QUESTION],\r
- order_by=['-activity.active_at']\r
- ).values(\r
- 'title',\r
- 'question_id',\r
- 'active_at',\r
- 'activity_type'\r
- )\r
- if len(questions) > 0:\r
- questions = [(Event(q['active_at'], q['activity_type'], q['title'], '', '0', \\r
- q['question_id'])) for q in questions]\r
- activities.extend(questions)\r
-\r
- # answers\r
- answers = Activity.objects.extra(\r
- select={\r
- 'title' : 'question.title',\r
- 'question_id' : 'question.id',\r
- 'answer_id' : 'answer.id',\r
- 'active_at' : 'activity.active_at',\r
- 'activity_type' : 'activity.activity_type'\r
- },\r
- tables=['activity', 'answer', 'question'],\r
- where=['activity.content_type_id = %s AND activity.object_id = answer.id AND ' + \r
- 'answer.question_id=question.id AND NOT answer.deleted AND activity.user_id=%s AND '+\r
- 'activity.activity_type=%s AND NOT question.deleted'],\r
- params=[answer_type_id, user_id, TYPE_ACTIVITY_ANSWER],\r
- order_by=['-activity.active_at']\r
- ).values(\r
- 'title',\r
- 'question_id',\r
- 'answer_id',\r
- 'active_at',\r
- 'activity_type'\r
- )\r
- if len(answers) > 0:\r
- answers = [(Event(q['active_at'], q['activity_type'], q['title'], '', q['answer_id'], \\r
- q['question_id'])) for q in answers]\r
- activities.extend(answers)\r
-\r
- # question comments\r
- comments = Activity.objects.extra(\r
- select={\r
- 'title' : 'question.title',\r
- 'question_id' : 'comment.object_id',\r
- 'added_at' : 'comment.added_at',\r
- 'activity_type' : 'activity.activity_type'\r
- },\r
- tables=['activity', 'question', 'comment'],\r
-\r
- where=['activity.content_type_id = %s AND activity.object_id = comment.id AND '+\r
- 'activity.user_id = comment.user_id AND comment.object_id=question.id AND '+\r
- 'comment.content_type_id=%s AND activity.user_id = %s AND activity.activity_type=%s AND ' +\r
- 'NOT question.deleted'],\r
- params=[comment_type_id, question_type_id, user_id, TYPE_ACTIVITY_COMMENT_QUESTION],\r
- order_by=['-comment.added_at']\r
- ).values(\r
- 'title',\r
- 'question_id',\r
- 'added_at',\r
- 'activity_type'\r
- )\r
-\r
- if len(comments) > 0:\r
- comments = [(Event(q['added_at'], q['activity_type'], q['title'], '', '0', \\r
- q['question_id'])) for q in comments]\r
- activities.extend(comments)\r
-\r
- # answer comments\r
- comments = Activity.objects.extra(\r
- select={\r
- 'title' : 'question.title',\r
- 'question_id' : 'question.id',\r
- 'answer_id' : 'answer.id',\r
- 'added_at' : 'comment.added_at',\r
- 'activity_type' : 'activity.activity_type'\r
- },\r
- tables=['activity', 'question', 'answer', 'comment'],\r
-\r
- where=['activity.content_type_id = %s AND activity.object_id = comment.id AND '+\r
- 'activity.user_id = comment.user_id AND comment.object_id=answer.id AND '+\r
- 'comment.content_type_id=%s AND question.id = answer.question_id AND '+\r
- 'activity.user_id = %s AND activity.activity_type=%s AND '+\r
- 'NOT answer.deleted AND NOT question.deleted'],\r
- params=[comment_type_id, answer_type_id, user_id, TYPE_ACTIVITY_COMMENT_ANSWER],\r
- order_by=['-comment.added_at']\r
- ).values(\r
- 'title',\r
- 'question_id',\r
- 'answer_id',\r
- 'added_at',\r
- 'activity_type'\r
- )\r
-\r
- if len(comments) > 0:\r
- comments = [(Event(q['added_at'], q['activity_type'], q['title'], '', q['answer_id'], \\r
- q['question_id'])) for q in comments]\r
- activities.extend(comments)\r
-\r
- # question revisions\r
- revisions = Activity.objects.extra(\r
- select={\r
- 'title' : 'question_revision.title',\r
- 'question_id' : 'question_revision.question_id',\r
- 'added_at' : 'activity.active_at',\r
- 'activity_type' : 'activity.activity_type',\r
- 'summary' : 'question_revision.summary'\r
- },\r
- tables=['activity', 'question_revision', 'question'],\r
- where=['activity.content_type_id = %s AND activity.object_id = question_revision.id AND '+\r
- 'question_revision.id=question.id AND NOT question.deleted AND '+\r
- 'activity.user_id = question_revision.author_id AND activity.user_id = %s AND '+\r
- 'activity.activity_type=%s'],\r
- params=[question_revision_type_id, user_id, TYPE_ACTIVITY_UPDATE_QUESTION],\r
- order_by=['-activity.active_at']\r
- ).values(\r
- 'title',\r
- 'question_id',\r
- 'added_at',\r
- 'activity_type',\r
- 'summary'\r
- )\r
-\r
- if len(revisions) > 0:\r
- revisions = [(Event(q['added_at'], q['activity_type'], q['title'], q['summary'], '0', \\r
- q['question_id'])) for q in revisions]\r
- activities.extend(revisions)\r
-\r
- # answer revisions\r
- revisions = Activity.objects.extra(\r
- select={\r
- 'title' : 'question.title',\r
- 'question_id' : 'question.id',\r
- 'answer_id' : 'answer.id',\r
- 'added_at' : 'activity.active_at',\r
- 'activity_type' : 'activity.activity_type',\r
- 'summary' : 'answer_revision.summary'\r
- },\r
- tables=['activity', 'answer_revision', 'question', 'answer'],\r
-\r
- where=['activity.content_type_id = %s AND activity.object_id = answer_revision.id AND '+\r
- 'activity.user_id = answer_revision.author_id AND activity.user_id = %s AND '+\r
- 'answer_revision.answer_id=answer.id AND answer.question_id = question.id AND '+\r
- 'NOT question.deleted AND NOT answer.deleted AND '+\r
- 'activity.activity_type=%s'],\r
- params=[answer_revision_type_id, user_id, TYPE_ACTIVITY_UPDATE_ANSWER],\r
- order_by=['-activity.active_at']\r
- ).values(\r
- 'title',\r
- 'question_id',\r
- 'added_at',\r
- 'answer_id',\r
- 'activity_type',\r
- 'summary'\r
- )\r
-\r
- if len(revisions) > 0:\r
- revisions = [(Event(q['added_at'], q['activity_type'], q['title'], q['summary'], \\r
- q['answer_id'], q['question_id'])) for q in revisions]\r
- activities.extend(revisions)\r
-\r
- # accepted answers\r
- accept_answers = Activity.objects.extra(\r
- select={\r
- 'title' : 'question.title',\r
- 'question_id' : 'question.id',\r
- 'added_at' : 'activity.active_at',\r
- 'activity_type' : 'activity.activity_type',\r
- },\r
- tables=['activity', 'answer', 'question'],\r
- where=['activity.content_type_id = %s AND activity.object_id = answer.id AND '+\r
- 'activity.user_id = question.author_id AND activity.user_id = %s AND '+\r
- 'NOT answer.deleted AND NOT question.deleted AND '+\r
- 'answer.question_id=question.id AND activity.activity_type=%s'],\r
- params=[answer_type_id, user_id, TYPE_ACTIVITY_MARK_ANSWER],\r
- order_by=['-activity.active_at']\r
- ).values(\r
- 'title',\r
- 'question_id',\r
- 'added_at',\r
- 'activity_type',\r
- )\r
- if len(accept_answers) > 0:\r
- accept_answers = [(Event(q['added_at'], q['activity_type'], q['title'], '', '0', \\r
- q['question_id'])) for q in accept_answers]\r
- activities.extend(accept_answers)\r
- #award history\r
- awards = Activity.objects.extra(\r
- select={\r
- 'badge_id' : 'badge.id',\r
- 'awarded_at': 'award.awarded_at',\r
- 'activity_type' : 'activity.activity_type'\r
- },\r
- tables=['activity', 'award', 'badge'],\r
- where=['activity.user_id = award.user_id AND activity.user_id = %s AND '+\r
- 'award.badge_id=badge.id AND activity.object_id=award.id AND activity.activity_type=%s'],\r
- params=[user_id, TYPE_ACTIVITY_PRIZE],\r
- order_by=['-activity.active_at']\r
- ).values(\r
- 'badge_id',\r
- 'awarded_at',\r
- 'activity_type'\r
- )\r
- if len(awards) > 0:\r
- awards = [(AwardEvent(q['awarded_at'], q['activity_type'], q['badge_id'])) for q in awards]\r
- activities.extend(awards)\r
-\r
- activities.sort(lambda x,y: cmp(y.time, x.time))\r
-\r
- return render_to_response(user_view.template_file,{\r
- "tab_name" : user_view.id,\r
- "tab_description" : user_view.tab_description,\r
- "page_title" : user_view.page_title,\r
- "view_user" : user,\r
- "activities" : activities[:user_view.data_size]\r
- }, context_instance=RequestContext(request))\r