def comment_page_callback(request, page_model, page_app_label, page_pk, scope_model, scope_app_label, scope_pk): """ callback used when replying by email to an activity""" log.debug("replies.views.comment_page_callback") api_key = request.POST.get('api-key') if not api_key == settings.INTERNAL_API_KEY: log.error('Invalid API KEY used for internal API!') return http.HttpResponseForbidden() from_user = request.POST.get('from') reply_text = request.POST.get('text') reply_text = utils.extract_reply(reply_text) user = None try: user = UserProfile.objects.get(username=from_user) except UserProfile.DoesNotExist: log.error("Invalid user attempted reply: {0}".format(from_user)) page_object = None try: page_ct_cls = ContentType.objects.get( model=page_model, app_label=page_app_label).model_class() page_object = page_ct_cls.objects.get(pk=page_pk) except: log.error("could not find page object") scope_object = None try: scope_ct_cls = ContentType.objects.get( model=scope_model, app_label=scope_app_label).model_class() scope_object = get_object_or_404(scope_ct_cls, pk=scope_pk) except: log.error("could not find scope object") if user and user.can_post() and page_object and page_object.can_comment( user.user) and scope_object and reply_text: comment = PageComment(content=reply_text) comment.page_object = page_object comment.scope_object = scope_object comment.author = user comment.sent_by_email = True comment.save() return http.HttpResponse(status=200)
def test_repy(self): comment = PageComment() comment.page_object = self.page comment.scope_object = self.project comment.author = self.user comment.content = "blah blah" comment.save() self.client.login(username=self.test_username, password=self.test_password) # post reply data = {'content': 'This is a reply'} reply_url = '/{0}/comments/{1}/reply/'.format(self.locale, comment.id) response = self.client.post(reply_url, data) comments = PageComment.objects.all() self.assertEquals(comments.count(), 2)
def test_comment_reply_api_key(self): comment = PageComment() comment.page_object = self.page comment.scope_object = self.project comment.author = self.user comment.content = "blah blah" comment.save() data = { u'api-key': 'notthecorrectkey', u'from': self.test_username, u'text': u'Some stealthy reply that won\'t make it in!\n', } response = self.client.post( '/{0}/comments/{1}/email_reply/'.format(self.locale, comment.id), data) self.assertEqual(response.status_code, 403)
def accept(self, as_organizer=False, reviewer=None): if not reviewer: reviewer = self.sign_up.author is_organizing = self.project.organizers().filter( user=self.author).exists() is_participating = self.project.participants().filter( user=self.author).exists() if not is_organizing and not is_participating: participation = Participation(project=self.project, user=self.author, organizing=as_organizer) participation.save() accept_content = render_to_string( "signups/accept_sign_up_comment.html", {'as_organizer': as_organizer}) accept_comment = PageComment(content=accept_content, author=reviewer, page_object=self, scope_object=self.project) accept_comment.save() self.accepted = True self.save()
def test_reply_by_email(self): # post a comment comment = PageComment() comment.page_object = self.page comment.scope_object = self.project comment.author = self.user comment.content = "blah blah" comment.save() data = { u'api-key': settings.INTERNAL_API_KEY, u'from': self.test_username, u'text': u'Maybe this time\n', } comment_count = PageComment.objects.filter(sent_by_email=True).count() response = self.client.post( '/{0}/comments/{1}/email_reply/'.format(self.locale, comment.id), data) self.assertEqual(response.status_code, 200) comments = PageComment.objects.filter(sent_by_email=True) self.assertEquals(comments.count(), comment_count + 1)
def setUp(self): self.client = Client() self.locale = 'en' django_user = User( username=self.test_username, email=self.test_email, ) self.user = create_profile(django_user) self.user.set_password(self.test_password) self.user.save() self.project = Project( name='Reply Project', short_description='This project is to test replies', long_description='No really, its good', ) self.project.save() participation = Participation(project=self.project, user=self.user, organizing=True) participation.save() self.page = Page(author=self.user, project=self.project, title='task title', sub_header='Tagline', content='Content', index=2) self.page.save() self.comment = PageComment() self.comment.page_object = self.page self.comment.scope_object = self.project self.comment.author = self.user self.comment.content = "blah blah" self.comment.save()
def task_toggle_completion(request, page, ignore_post_data=False): project = page.project total_count = Page.objects.filter(project__slug=project.slug, listed=True, deleted=False).count() ajax_data = { 'upon_completion_redirect': page.project.get_absolute_url(), 'total_count': total_count, } progressbar_value = 0 task_completion = None next_badge = None is_last_badge = True task_link_submit_form = None task_badge_apply_form = None badges_to_apply = list(page.badges_to_apply.order_by('id')) if request.user.is_authenticated(): profile = request.user.get_profile() ajax_data['completed_count'] = PerUserTaskCompletion.objects.filter( page__project__slug=project.slug, page__deleted=False, unchecked_on__isnull=True, user=profile).count() if total_count: progressbar_value = (ajax_data['completed_count'] * 100 / total_count) try: task_completion = PerUserTaskCompletion.objects.filter( user=profile, page=page, unchecked_on__isnull=True)[0] except IndexError: pass if badges_to_apply and task_completion: next_badge, is_last_badge = page.get_next_badge_can_apply(profile) if not task_completion.url: if not ignore_post_data and request.method == 'POST': task_link_submit_form = TaskLinkSubmitForm( bool(next_badge), request.POST, instance=task_completion) if task_link_submit_form.is_valid(): task_completion = task_link_submit_form.save() if task_link_submit_form.cleaned_data['post_link']: link_comment_content = render_to_string( "content/_post_link_comment.html", {'url': task_completion.url}) link_comment = PageComment( content=link_comment_content, author=profile, page_object=page, scope_object=page.project) link_comment.save() ajax_data[ 'posted_link_comment_html'] = render_to_string( 'replies/_comment_threads.html', { 'comments': [link_comment], 'is_challenge': True, 'user': request.user }).strip() if task_link_submit_form.cleaned_data.get( 'apply_for_badges', False) and next_badge: task_badge_apply_form = TaskBadgeApplyForm( initial={'badge_slug': next_badge.slug}) task_link_submit_form = None else: task_link_submit_form = TaskLinkSubmitForm( show_badge_apply_option=bool(next_badge)) elif not ignore_post_data and next_badge and request.method == 'POST': task_badge_apply_form = TaskBadgeApplyForm(request.POST) if task_badge_apply_form.is_valid(): try: badge = page.badges_to_apply.get( slug=task_badge_apply_form. cleaned_data['badge_slug']) submission = task_badge_apply_form.save(commit=False) submission.url = task_completion.url submission.badge = badge submission.author = profile submission.save() next_badge, is_last_badge = page.get_next_badge_can_apply( profile) if next_badge: task_badge_apply_form = TaskBadgeApplyForm( initial={'badge_slug': next_badge.slug}) else: task_badge_apply_form = None except Badge.DoesNotExist: task_badge_apply_form = None for badge in badges_to_apply: try: badge.awarded = Award.objects.filter(user=profile, badge=badge)[0] except IndexError: badge.awarded = False try: badge.applied = Submission.objects.filter(author=profile, badge=badge)[0] except IndexError: badge.applied = False elegible = badge.is_eligible(request.user) badge.show_apply = (not badge.awarded and not badge.applied and elegible) ajax_data.update({ 'stay_on_page': bool(task_link_submit_form or task_badge_apply_form), 'progressbar_value': progressbar_value, }) context = { 'page': page, 'request': request, 'can_comment': page.can_comment(request.user), 'next_page': page.get_next_page(), 'task_completion': task_completion, 'next_badge': next_badge, 'is_last_badge': is_last_badge, 'task_link_submit_form': task_link_submit_form, 'task_badge_apply_form': task_badge_apply_form, 'badges_to_apply': badges_to_apply, 'ajax_data': ajax_data, } return context