def upvote_question(request, q_id): if request.user.is_anonymous(): messages.error( request, _('Sorry but only connected users can upvote questions')) return HttpResponseRedirect(settings.LOGIN_URL) if request.method == "POST": q = get_object_or_404(Question, id=q_id) user = request.user if q.entity != user.profile.locality: return HttpResponseForbidden( _('You may only support questions in your locality')) if q.author == user: return HttpResponseForbidden( _("You may not support your own question")) if user.upvotes.filter(question=q): return HttpResponseForbidden( _("You already upvoted this question")) else: upvote = QuestionUpvote.objects.create(question=q, user=user) #TODO: use signals so the next line won't be necesary new_count = change_rating(q, 1) follow(request.user, q) publish_upvote_to_facebook.delay(upvote) return HttpResponse(new_count) else: return HttpResponseForbidden(_("Use POST to upvote a question"))
def handle_annotation_save(sender, created, instance, **kwargs): if created: if not cm_ct: get_ct() if not Action.objects.filter( actor_object_id=instance.content_object.meeting.id, actor_content_type=cm_ct, verb='annotation-added', target_object_id=instance.id, target_content_type=annotation_ct).exists(): action.send(instance.content_object.meeting, verb='annotation-added', target=instance, description=unicode(instance.flag_value)) if not Action.objects.filter( actor_object_id=instance.user.id, actor_content_type=user_ct, verb='annotated', target_object_id=instance.id, target_content_type=annotation_ct).exists(): action.send(instance.user, verb='annotated', target=instance, description=unicode(instance.flag_value)) if not Follow.objects.filter(user=instance.user, object_id=instance.content_object.meeting.id, content_type=cm_ct).exists(): follow(instance.user, instance.content_object.meeting)
def handle_annotation_save(sender, created, instance, **kwargs): if created: if not cm_ct: get_ct() if Action.objects.filter( actor_object_id=instance.content_object.meeting.id, actor_content_type=cm_ct, verb='annotation-added', target_object_id=instance.id, target_content_type=annotation_ct).count() == 0: action.send(instance.content_object.meeting, verb='annotation-added', target=instance, description=unicode(instance.flag_value)) if Action.objects.filter( actor_object_id=instance.user.id, actor_content_type=user_ct, verb='annotated', target_object_id=instance.id, target_content_type=annotation_ct).count() == 0: action.send(instance.user, verb='annotated', target=instance, description=unicode(instance.flag_value)) if Follow.objects.filter(user=instance.user, object_id=instance.content_object.meeting.id, content_type=cm_ct)\ .count()==0: follow(instance.user, instance.content_object.meeting)
def post_question(request, entity_id=None, slug=None): if request.user.is_anonymous(): messages.error(request, _('Sorry but only connected users can post questions')) return HttpResponseRedirect(settings.LOGIN_URL) profile = request.user.profile if entity_id: entity = Entity.objects.get(pk=entity_id) if entity != profile.locality: messages.warning(request, _('Sorry, you may only post questions in your locality') + "\n" + _('Before posting a new question, please check if it already exists in this page')) return HttpResponseRedirect(reverse('local_home', kwargs={'entity_id': profile.locality.id,})) entity = profile.locality q = slug and get_object_or_404(Question, unislug=slug, entity=entity) if request.method == "POST": form = QuestionForm(request.user, request.POST, instance=q) if form.is_valid(): ''' carefull when changing a question's history ''' if not q: try: q = Question.objects.get(author=request.user, subject=form.cleaned_data['subject']) except: pass question = form.save(commit=False) if q: if q.author != request.user: return HttpResponseForibdden(_("You can only edit your own questions.")) if q.answers.count(): return HttpResponseForbidden(_("Question has been answered, editing disabled.")) question.id = q.id question.created_at = q.created_at question.author = request.user question.save() form.save_m2m() if form.cleaned_data.get('facebook_publish', False): publish_question_to_facebook.delay(question) follow(request.user, question) return HttpResponseRedirect(question.get_absolute_url()) else: if q: form = QuestionForm(request.user, instance=q) else: form = QuestionForm(request.user, initial={'entity': entity}) becoming_editor = not profile.is_editor and\ Profile.objects.need_editors(entity) context = RequestContext(request, {"form": form, "entity": entity, "max_length_q_subject": MAX_LENGTH_Q_SUBJECT, "slug": slug, "becoming_editor": becoming_editor, }) return render(request, "qa/post_question.html", context)
def handle_annotation_save(sender, created, instance, **kwargs): if created: action.send(instance.content_object.meeting, verb='annotation-added', target=instance, description=unicode(instance.flag_value)) action.send(instance.user, verb='annotated', target=instance, description=unicode(instance.flag_value)) follow(instance.user, instance.content_object.meeting)
def user_follow_unfollow(request): """Recieves POST parameters: verb - 'follow' or 'unfollow' what - string representing target object type ('member', 'agenda', ...) id - id of target object """ FOLLOW_TYPES = {"question": Question, "user": User} what = request.POST.get("what", None) target_id = request.POST.get("id", None) if not target_id: return HttpResponseBadRequest("need an id of an object to watch") verb = request.POST.get("verb", None) if verb not in ["follow", "unfollow"]: return HttpResponseBadRequest("verb parameter has to be one of: 'follow', 'unfollow'") logged_in = request.user.is_authenticated() content_type = ContentType.objects.get_for_model(FOLLOW_TYPES[what]) qs = Follow.objects.filter(object_id=target_id, content_type=content_type) if verb == "follow": try: obj = get_object_or_404(FOLLOW_TYPES[what], pk=target_id) follow(request.user, obj) except: return HttpResponseBadRequest("object not found") else: # unfollow Follow.objects.get(user=request.user, content_type=content_type, object_id=target_id).delete() res = {"can_watch": logged_in, "followers": qs.count(), "watched": logged_in and bool(qs.filter(user=request.user))} return HttpResponse(json.dumps(res), content_type="application/json")
def test_is_following(self): """Test the is-following query""" p = self.jacob.get_profile() loggedin = self.client.login(username='******', password='******') self.assertTrue(loggedin) follow(self.jacob, self.bill_1) response = self.client.post(reverse('user-is-following'), { 'id': self.bill_1.id, 'what': 'bill' }) self.assertEquals(response.status_code, 200) res_obj = json.loads(response.content) self.assertTrue(res_obj['watched']) unfollow(self.jacob, self.bill_1) response = self.client.post(reverse('user-is-following'), { 'id': self.bill_1.id, 'what': 'bill' }) self.assertEquals(response.status_code, 200) res_obj = json.loads(response.content) self.assertFalse(res_obj['watched']) self.client.logout()
def forwards(self, orm): "Write your forwards methods here." for profile in orm.UserProfile.objects.all(): user = User.objects.get(pk=profile.user.id) for mk in profile.followed_members.all(): follow(user, mk) for party in profile.followed_parties.all(): follow(user, party)
def testUnfollowMeeting(self): follow(self.jacob, self.meeting_1) p = self.jacob.get_profile() self.assertEquals(len(p.meetings), 1) loggedin = self.client.login(username="******", password="******") self.assertTrue(loggedin) response = self.client.post(reverse("user-unfollows"), {"what": "meeting", "unwatch": self.meeting_1.id}) self.assertEquals(len(p.members), 0)
def testUnfollowMeeting(self): follow(self.jacob, self.meeting_1) p = self.jacob.get_profile() self.assertEquals(len(p.meetings), 1) loggedin = self.client.login(username='******', password='******') self.assertTrue(loggedin) response = self.client.post(reverse('user-unfollows'), {'what': 'meeting', 'unwatch': self.meeting_1.id}) self.assertEquals(len(p.members), 0)
def testUnfollowMeeting(self): follow(self.jacob, self.meeting_1) p = self.jacob.get_profile() self.assertEquals(len(p.meetings), 1) loggedin = self.client.login(username='******', password='******') self.assertTrue(loggedin) response = self.client.post(reverse('user-follow-unfollow'), {'what': 'meeting', 'id': self.meeting_1.id, 'verb':'unfollow'}) self.assertEquals(len(p.members), 0)
def testMemberDetailsContext(self): # test anonymous user mk_1_url = self.mk_1.get_absolute_url() res = self.client.get(mk_1_url) self.assertFalse(res.context['watched_member']) # test autherized user self.assertTrue(self.client.login(username='******', password='******')) res = self.client.get(mk_1_url) self.assertFalse(res.context['watched_member']) # test autherized user that follows follow(self.jacob, self.mk_1) res = self.client.get(mk_1_url) self.assertTrue(res.context['watched_member'])
def upvote_question(request, q_id): if request.method == "POST": q = get_object_or_404(Question, id=q_id) user = request.user if q.author == user or user.upvotes.filter(question=q): return HttpResponseForbidden(_("You already upvoted this question")) else: upvote = QuestionUpvote.objects.create(question=q, user=user) #TODO: use signals so the next line won't be necesary new_count = increase_rating(q) follow(request.user, q) publish_upvote_to_facebook.delay(upvote) return HttpResponse(new_count) else: return HttpResponseForbidden(_("Use POST to upvote a question"))
def user_follow_unfollow(request): """Recieves POST parameters: verb - 'follow' or 'unfollow' what - string representing target object type ('member', 'agenda', ...) id - id of target object """ what = request.POST.get('what', None) if what not in FOLLOW_TYPES: return HttpResponseBadRequest('what parameter has to be one of: %s' % ','.join(FOLLOW_TYPES.keys())) if not request.user.is_authenticated(): return HttpResponseForbidden(reverse('login')) target_id = request.POST.get('id', None) if not target_id: return HttpResponseBadRequest('need an id of an object to watch') verb = request.POST.get('verb', None) if verb not in ['follow', 'unfollow']: return HttpResponseBadRequest( "verb parameter has to be one of: 'follow', 'unfollow'") logged_in = request.user.is_authenticated() content_type = ContentType.objects.get_for_model(FOLLOW_TYPES[what]) qs = Follow.objects.filter(object_id=target_id, content_type=content_type) if verb == 'follow': try: obj = get_object_or_404(FOLLOW_TYPES[what], pk=target_id) follow(request.user, obj) except: return HttpResponseBadRequest('object not found') else: # unfollow Follow.objects.get(user=request.user, content_type=content_type, object_id=target_id).delete() res = { 'can_watch': logged_in, 'followers': qs.count(), 'watched': logged_in and bool(qs.filter(user=request.user)) } return HttpResponse(json.dumps(res), content_type='application/json')
def user_follow_unfollow(request): """Recieves POST parameters: verb - 'follow' or 'unfollow' what - string representing target object type ('member', 'agenda', ...) id - id of target object """ what = request.POST.get('what', None) if what not in FOLLOW_TYPES: return HttpResponseBadRequest( 'what parameter has to be one of: %s' % ','.join(FOLLOW_TYPES.keys())) if not request.user.is_authenticated(): return HttpResponseForbidden(reverse('login')) target_id = request.POST.get('id', None) if not target_id: return HttpResponseBadRequest('need an id of an object to watch') verb = request.POST.get('verb', None) if verb not in ['follow', 'unfollow']: return HttpResponseBadRequest( "verb parameter has to be one of: 'follow', 'unfollow'") logged_in = request.user.is_authenticated() content_type = ContentType.objects.get_for_model(FOLLOW_TYPES[what]) qs = Follow.objects.filter(object_id=target_id, content_type=content_type) if verb == 'follow': try: obj = get_object_or_404(FOLLOW_TYPES[what], pk=target_id) follow(request.user, obj) except: return HttpResponseBadRequest('object not found') else: # unfollow Follow.objects.get( user=request.user, content_type=content_type, object_id=target_id).delete() res = { 'can_watch': logged_in, 'followers': qs.count(), 'watched': logged_in and bool(qs.filter(user=request.user)) } return HttpResponse(json.dumps(res), content_type='application/json')
def test_empty_email(self): cmd = notify.Command() email, email_html = cmd.get_email_for_user(self.jacob) self.assertEqual(email, []) follow(self.jacob, self.mk_1) action.send(self.mk_1, verb='farted on', target=self.agenda_1) email, email_html = cmd.get_email_for_user(self.jacob) text = "\n".join(email) self.assertIn(u'mk 1 farted on agenda 1', text) follow(self.jacob, self.agenda_1) action.send(self.mk_1, verb='voted for', target=self.agenda_1) action.send(self.agenda_1, verb='supports', target=self.mk_1) email, email_html = cmd.get_email_for_user(self.jacob) text = "\n".join(email) self.assertIn(u'mk 1 voted for agenda 1', text) self.assertIn(u'supports mk 1', text) email, email_html = cmd.get_email_for_user(self.jacob) self.assertEqual(email, [])
def test_empty_email(self): cmd = notify.Command() email, email_html = cmd.get_email_for_user(self.jacob) self.assertEqual(email, []) follow (self.jacob, self.mk_1) action.send(self.mk_1, verb='farted on', target=self.agenda_1) email, email_html = cmd.get_email_for_user(self.jacob) text = "\n".join(email) self.assertIn(u'mk 1 farted on agenda 1', text) follow (self.jacob, self.agenda_1) action.send(self.mk_1, verb='voted for', target=self.agenda_1) action.send(self.agenda_1, verb='supports', target=self.mk_1) email, email_html = cmd.get_email_for_user(self.jacob) text = "\n".join(email) self.assertIn(u'mk 1 voted for agenda 1', text) self.assertIn(u'supports mk 1', text) email, email_html = cmd.get_email_for_user(self.jacob) self.assertEqual(email, [])
def handle_noargs(self, **options): # TODO: VALID_EMAIL_GROUP should be project-wide valid_email = Group.objects.get(name='Valid Email') for member in Member.objects.all(): user = member.user if not user: user = User() user.email = member.email user.username = member.email.split('@')[0] self.stdout.write(u"filling user data for member #%d (%s)\n" % (member.id, user.username)) names = member.name.split(' ') user.first_name = ' '.join(names[:-1]) if user.first_name: user.last_name = names[-1] else: # just one name, better make it the first user.first_name = names[0] user.last_name = '' user.save() user.groups.add(valid_email) avatars = Avatar.objects.filter(user=user) avatar = avatars[0] if avatars else user.avatar_set.create() image_url = urllib2.urlopen(member.img_url) content = ContentFile(image_url.read()) avatar.avatar.save(user.username, content) try: profile = user.profiles.get() except: profile = user.profiles.create() profile.email_notification = 'D' if not profile.description: profile.description = member.get_role profile.gender = member.gender profile.save() follow(user, member) if not member.user: member.user = user member.save()
def handle_noargs(self, **options): # TODO: VALID_EMAIL_GROUP should be project-wide valid_email = Group.objects.get(name="Valid Email") for member in Member.objects.all(): user = member.user if not user: user = User() user.email = member.email user.username = member.email.split("@")[0] self.stdout.write(u"filling user data for member #%d (%s)\n" % (member.id, user.username)) names = member.name.split(" ") user.first_name = " ".join(names[:-1]) if user.first_name: user.last_name = names[-1] else: # just one name, better make it the first user.first_name = names[0] user.last_name = "" user.save() user.groups.add(valid_email) avatars = Avatar.objects.filter(user=user) avatar = avatars[0] if avatars else user.avatar_set.create() image_url = urllib2.urlopen(member.img_url) content = ContentFile(image_url.read()) avatar.avatar.save(user.username, content) try: profile = user.get_profile() except: profile = user.profiles.create() profile.email_notification = "D" if not profile.description: profile.description = member.get_role profile.gender = member.gender profile.save() follow(user, member) if not member.user: member.user = user member.save()
def test_empty_email(self): cmd = notify.Command() email, email_html = cmd.get_email_for_user(self.jacob) self.assertEqual(email, []) follow (self.jacob, self.mk_1) action.send(self.mk_1, verb='farted on', target=self.agenda_1) email, email_html = cmd.get_email_for_user(self.jacob) self.assertEqual(email, [u'\n\nfollowed MKs\n\n', u'Member mk 1\n\nmk 1 farted on agenda 1\n\n']) follow (self.jacob, self.agenda_1) action.send(self.mk_1, verb='voted for', target=self.agenda_1) action.send(self.agenda_1, verb='supports', target=self.mk_1) email, email_html = cmd.get_email_for_user(self.jacob) self.assertEqual(email, [u'\n\nfollowed MKs\n\n', u'Member mk 1\n\nmk 1 voted for agenda 1\n\n', u'Agendas', u'Agenda agenda 1\n\nagenda 1 supports mk 1\n\n']) email, email_html = cmd.get_email_for_user(self.jacob) self.assertEqual(email, [])
def post_answer(request, q_id): question = Question.objects.get(id=q_id) if not question.can_answer(request.user): return HttpResponseForbidden(_("You must be logged in as a candidate to post answers")) try: # If the user already answered, update his answer answer = question.answers.get(author=request.user) except question.answers.model.DoesNotExist: answer = Answer(author=request.user, question=question) follow(request.user, question) answer.content = request.POST.get("content") answer.save() publish_answer.delay(answer) return HttpResponseRedirect(question.get_absolute_url())
def follow_agendas(request): if not request.user.is_authenticated(): return HttpResponseForbidden(reverse('login')) if request.method == 'POST': unwatch_id = request.POST.get('unwatch', None) if unwatch_id: agenda = get_object_or_404(Agenda, pk=unwatch_id) unfollow(request.user, agenda, send_action=True) else: watch_id = request.POST.get('watch', None) if not watch_id: return HttpResponseServerError('neither "watch" nor "unwatch" arguments specified') try: agenda = get_object_or_404(Agenda, pk=watch_id) follow(request.user, agenda) except Agenda.DoesNotExist: return HttpResponseBadRequest('bad agenda id') return HttpResponse('OK') else: return HttpResponseNotAllowed(['POST'])
def follow_members(request): if not request.user.is_authenticated(): return HttpResponseForbidden(reverse("login")) if request.method == "POST": unwatch_id = request.POST.get("unwatch", None) if unwatch_id: member = get_object_or_404(Member, pk=unwatch_id) unfollow(request.user, member, send_action=True) else: watch_id = request.POST.get("watch", None) if not watch_id: return HttpResponseServerError('neither "watch" nor "unwatch" arguments specified') try: member = get_object_or_404(Member, pk=watch_id) follow(request.user, member) except Member.DoesNotExist: return HttpResponseBadRequest("bad member id") return HttpResponse("OK") else: return HttpResponseNotAllowed(["POST"])
def user_follow_unfollow(request): """Recieves POST parameters: verb - 'follow' or 'unfollow' what - string representing target object type ('member', 'agenda', ...) id - id of target object """ if not request.user.is_authenticated(): return HttpResponseForbidden(reverse('login')) if request.method != 'POST': return HttpResponseNotAllowed(['POST']) target_id = request.POST.get('id', None) if not target_id: return HttpResponseBadRequest('need an id of an object to watch') what = request.POST.get('what', None) what_types = { # these are the object types we allow following 'member': Member, 'meeting': CommitteeMeeting, 'agenda': Agenda, 'bill': Bill, 'topic': Topic, } if what not in what_types: return HttpResponseBadRequest('what parameter has to be one of: %s' % ','.join(what_types.keys())) verb = request.POST.get('verb', None) if verb not in ['follow', 'unfollow']: return HttpResponseBadRequest( "verb parameter has to be one of: 'follow', 'unfollow'") if verb == 'follow': try: obj = get_object_or_404(what_types[what], pk=target_id) follow(request.user, obj) except: return HttpResponseBadRequest('object not found') else: # unfollow content_type = ContentType.objects.get_for_model(what_types[what]) Follow.objects.get(user=request.user, content_type=content_type, object_id=target_id).delete() return HttpResponse('OK')
def follow_members(request): if not request.user.is_authenticated(): return HttpResponseForbidden(reverse('login')) p = request.user.get_profile() if request.method == 'POST': unwatch_id = request.POST.get('unwatch', None) if unwatch_id: member = get_object_or_404(Member, pk=unwatch_id) unfollow(request.user, member) else: watch_id = request.POST.get('watch', None) if not watch_id: return HttpResponseServerError('neither "watch" nor "unwatch" arguments specified') try: member = get_object_or_404(Member, pk=watch_id) follow(request.user, member) except Member.DoesNotExist: return HttpResponseBadRequest('bad member id') return HttpResponse('OK') else: return HttpResponseNotAllowed(['POST'])
def follow_agendas(request): if not request.user.is_authenticated(): return HttpResponseForbidden(reverse('login')) if request.method == 'POST': unwatch_id = request.POST.get('unwatch', None) if unwatch_id: agenda = get_object_or_404(Agenda, pk=unwatch_id) unfollow(request.user, agenda, send_action=True) else: watch_id = request.POST.get('watch', None) if not watch_id: return HttpResponseServerError( 'neither "watch" nor "unwatch" arguments specified') try: agenda = get_object_or_404(Agenda, pk=watch_id) follow(request.user, agenda) except Agenda.DoesNotExist: return HttpResponseBadRequest('bad agenda id') return HttpResponse('OK') else: return HttpResponseNotAllowed(['POST'])
def user_follow_unfollow(request): """Recieves POST parameters: verb - 'follow' or 'unfollow' what - string representing target object type ('member', 'agenda', ...) id - id of target object """ if not request.user.is_authenticated(): return HttpResponseForbidden(reverse('login')) if request.method != 'POST': return HttpResponseNotAllowed(['POST']) target_id = request.POST.get('id', None) if not target_id: return HttpResponseBadRequest('need an id of an object to watch') what = request.POST.get('what', None) what_types = { # these are the object types we allow following 'member': Member, 'meeting': CommitteeMeeting, 'agenda': Agenda, 'bill': Bill, 'topic': Topic, } if what not in what_types: return HttpResponseBadRequest( 'what parameter has to be one of: %s' % ','.join(what_types.keys())) verb = request.POST.get('verb', None) if verb not in ['follow','unfollow']: return HttpResponseBadRequest( "verb parameter has to be one of: 'follow', 'unfollow'") if verb == 'follow': try: obj = get_object_or_404(what_types[what], pk=target_id) follow(request.user, obj) except: return HttpResponseBadRequest('object not found') else: # unfollow content_type = ContentType.objects.get_for_model(what_types[what]) Follow.objects.get(user=request.user, content_type=content_type, object_id=target_id).delete() return HttpResponse('OK')
def test_is_following(self): """Test the is-following query""" p = self.jacob.get_profile() loggedin = self.client.login(username='******', password='******') self.assertTrue(loggedin) follow(self.jacob, self.bill_1) response = self.client.post(reverse('user-is-following'), {'id': self.bill_1.id, 'what': 'bill'}) self.assertEquals(response.status_code, 200) self.assertEquals(response.content, 'true') unfollow(self.jacob, self.bill_1) response = self.client.post(reverse('user-is-following'), {'id': self.bill_1.id, 'what': 'bill'}) self.assertEquals(response.status_code, 200) self.assertEquals(response.content, 'false') self.client.logout()
def post_answer(request, q_id): question = Question.objects.get(id=q_id) if not question.can_answer(request.user): return HttpResponseForbidden( _("You must be logged in as a candidate to post answers")) try: # If the user already answered, update his answer answer = question.answers.get(author=request.user) is_new_answer = False except question.answers.model.DoesNotExist: answer = Answer(author=request.user, question=question) is_new_answer = True follow(request.user, question) answer.content = request.POST.get("content") answer.save() publish_answer.delay(answer, send_email=is_new_answer) return HttpResponseRedirect(question.get_absolute_url())
def setUp(self): self.group = Group.objects.get_or_create(name='CoolGroup')[0] self.user1 = User.objects.get_or_create(username='******')[0] self.user1.set_password('admin') self.user1.is_superuser = self.user1.is_active = self.user1.is_staff = True self.user1.save() self.user2 = User.objects.get_or_create(username='******')[0] # User1 joins group self.user1.groups.add(self.group) self.yesterday = datetime.now() - timedelta(days=1) action.send(self.user1,verb='joined',target=self.group, description='hello group', timestamp=self.yesterday) # User1 follows User2 follow(self.user1, self.user2) # User2 joins group self.user2.groups.add(self.group) action.send(self.user2,verb='joined',target=self.group) # User2 follows group follow(self.user2, self.group) # User1 comments on group action.send(self.user1,verb='commented on',target=self.group) comment = Comment.objects.get_or_create( user = self.user1, content_type = ContentType.objects.get_for_model(self.group), object_pk = self.group.pk, comment = 'Sweet Group!', site = Site.objects.get_current() )[0] # Group responds to comment action.send(self.group,verb='responded to',target=comment) self.client = Client()
def test_is_following(self): """Test the is-following query""" p = self.jacob.profiles.get() loggedin = self.client.login(username='******', password='******') self.assertTrue(loggedin) follow(self.jacob, self.bill_1) response = self.client.get(reverse('user-is-following'), {'id': self.bill_1.id, 'what': 'bill'}) self.assertEquals(response.status_code, 200) res_obj = json.loads(response.content) self.assertTrue(res_obj['watched']) unfollow(self.jacob, self.bill_1) response = self.client.get(reverse('user-is-following'), {'id': self.bill_1.id, 'what': 'bill'}) self.assertEquals(response.status_code, 200) res_obj = json.loads(response.content) self.assertFalse(res_obj['watched']) self.client.logout()
def upvote_question(request, q_id): if request.user.is_anonymous(): messages.error(request, _('Sorry but only connected users can upvote questions')) return HttpResponseRedirect(settings.LOGIN_URL) if request.method == "POST": q = get_object_or_404(Question, id=q_id) user = request.user if q.entity != user.profile.locality: return HttpResponseForbidden(_('You may only support questions in your locality')) if q.author == user: return HttpResponseForbidden(_("You may not support your own question")) if user.upvotes.filter(question=q): return HttpResponseForbidden(_("You already upvoted this question")) else: upvote = QuestionUpvote.objects.create(question=q, user=user) #TODO: use signals so the next line won't be necesary new_count = change_rating(q, 1) follow(request.user, q) publish_upvote_to_facebook.delay(upvote) return HttpResponse(new_count) else: return HttpResponseForbidden(_("Use POST to upvote a question"))
def handle_comment_save(sender, comment, request, **kwargs): if comment.content_type.model_class() == Topic: action.send(comment.content_object, verb='comment-added', target=comment, description=comment.comment) follow(request.user, comment.content_object)
def handle_comment_save(sender, comment, request, **kwargs): action.send(comment.content_object, verb='comment-added', target=comment, description=comment.comment) follow(request.user, comment.content_object)
def post_question(request, entity_id=None, slug=None): if request.user.is_anonymous(): messages.error(request, _('Sorry but only connected users can post questions')) return HttpResponseRedirect(settings.LOGIN_URL) profile = request.user.profile if entity_id: entity = Entity.objects.get(pk=entity_id) if entity != profile.locality: messages.warning( request, _('Sorry, you may only post questions in your locality') + "\n" + _('Before posting a new question, please check if it already exists in this page' )) return HttpResponseRedirect( reverse('local_home', kwargs={ 'entity_id': profile.locality.id, })) entity = profile.locality q = slug and get_object_or_404(Question, unislug=slug, entity=entity) if request.method == "POST": form = QuestionForm(request.user, request.POST, instance=q) if form.is_valid(): ''' carefull when changing a question's history ''' if not q: try: q = Question.objects.get( author=request.user, subject=form.cleaned_data['subject']) except: pass question = form.save(commit=False) if q: if q.author != request.user: return HttpResponseForibdden( _("You can only edit your own questions.")) if q.answers.count(): return HttpResponseForbidden( _("Question has been answered, editing disabled.")) question.id = q.id question.created_at = q.created_at question.author = request.user question.save() form.save_m2m() if form.cleaned_data.get('facebook_publish', False): publish_question_to_facebook.delay(question) follow(request.user, question) return HttpResponseRedirect(question.get_absolute_url()) else: if q: form = QuestionForm(request.user, instance=q) else: form = QuestionForm(request.user, initial={'entity': entity}) becoming_editor = not profile.is_editor and\ Profile.objects.need_editors(entity) context = RequestContext( request, { "form": form, "entity": entity, "max_length_q_subject": MAX_LENGTH_Q_SUBJECT, "slug": slug, "becoming_editor": becoming_editor, }) return render(request, "qa/post_question.html", context)