def flagvote(request): if not request.user.is_authenticated() or not request.user.is_active: #needs to popup registration dialog instead return HttpResponse(simplejson.dumps({'FAIL': True}), #need a better non-auth error here, interferes with view mimetype='application/json') if request.method == 'POST': vote = int(request.POST[u'vote']) pk = int(request.POST[u'pk']) flag_type = str(request.POST['flag_type']) user_pk = int(request.POST['user']) if request.user.pk != user_pk: return HttpResponse(simplejson.dumps({'FAIL': True}), mimetype='application/json') user = User(pk=user_pk) flag = Flag.objects.get(object_pk=pk, flag_type=flag_type) uflag, created = UserFlag.objects.get_or_create(user=user, flag=flag) cons = Consensus.objects.get(object_pk=pk) count = 0 v = bool(vote) votestr = '' if uflag.mode == v and not created: if v == True: flag.votes = flag.votes - 1 count = flag.votes votestr = 'vote_up_flat' elif v == False: flag.counters = flag.counters - 1 count = flag.counters votestr = 'vote_down_flat' flag.save() aso_rep_delete.send(sender=user, event_score=1, user=user, initiator=user, dimension=ReputationDimension.objects.get('Flag'),related_object=uflag) # delete reputation is flag removed uflag.delete() elif created: uflag.mode = v if v == True: flag.votes = flag.votes + 1 count = flag.votes votestr = 'vote_up_acti' elif v == False: flag.counters = flag.counters + 1 count = flag.counters votestr = 'vote_down_acti' uflag.save() flag.save() check_badges(cons.content_object.user, Flag, pk) aso_rep_event.send(sender=user, event_score=1, user=flag.content_object.user, initiator=user, dimension=ReputationDimension.objects.get('Flag'),related_object=uflag) # register reputation for flag creation imgsrc = '/static/voteimgs/' + votestr + '.gif' results = {'FAIL':False, 'count':count, 'modes':str(vote == 0), 'imgsrc':imgsrc} if 'application/json' in request.META.get('HTTP_ACCEPT', ''): return HttpResponse(simplejson.dumps(results), mimetype='application/json')
def vote(request, pk, vote, votemodel, vote_type_str, register): consensus = Consensus.objects.get(object_pk=pk) cnt = ContentType.objects.get_for_model(User) user_cons, is_new = Consensus.objects.get_or_create(content_type=cnt, object_pk=consensus.content_object.user.pk, parent_pk=consensus.content_object.user.pk, vote_type=cnt) if is_new: user_cons.intiate_vote_distributions() user_cons.spectrum.get_list() if vote == -99: st = votemodel.objects.get(user=request.user, object_pk=pk) # register reputation for voting if register: user_cons.register_vote(st, 'delete', old_vote=st.vote) aso_rep_delete.send(sender=request.user, event_score=1, user=consensus.content_object.user, initiator=request.user, dimension=ReputationDimension.objects.get('Vote'), related_object=st, is_vote=True) consensus.register_vote(st, 'delete', old_vote=st.vote) update_agent.send(sender=user_cons, type="vote", params=[vote_type_str, st.pk]) st.delete() else: try: #TODO: there is an error happening here! old = votemodel.objects.get(user=request.user, object_pk=pk) if old.vote != vote: old_vote_pos = old.vote old.vote = vote old.save() if register: user_cons.register_vote(old, 'change', old_vote=old_vote_pos) vote_created_callback(sender=request.session.session_key, parent=consensus, vote_type=vote) consensus.register_vote(old, 'change', old_vote=old_vote_pos) except: st, is_new = votemodel.objects.get_or_create(user=request.user, parent=consensus, vote=vote, object_pk=pk, parent_pk=consensus.parent_pk) st.save() check_badges(consensus.content_object.user, votemodel, pk) # register reputation for voting vote_created_callback(sender=request.session.session_key, parent=consensus, vote_type=vote) if register: aso_rep_event.send(sender=request.user, event_score=1, user=consensus.content_object.user, initiator=request.user, dimension=ReputationDimension.objects.get('Vote'), related_object=st, is_vote=True) user_cons.register_vote(st, 'register') consensus.register_vote(st, 'register') update_agent.send(sender=user_cons, type="vote", params=[vote_type_str, st.pk])
def vote(request, pk, vote, votemodel, vote_type_str, register): consensus = Consensus.objects.get(object_pk=pk) cnt = ContentType.objects.get_for_model(User) user_cons, is_new = Consensus.objects.get_or_create( content_type=cnt, object_pk=consensus.content_object.user.pk, parent_pk=consensus.content_object.user.pk, vote_type=cnt) if is_new: user_cons.intiate_vote_distributions() user_cons.spectrum.get_list() if vote == -99: st = votemodel.objects.get(user=request.user, object_pk=pk) # register reputation for voting if register: user_cons.register_vote(st, 'delete', old_vote=st.vote) aso_rep_delete.send( sender=request.user, event_score=1, user=consensus.content_object.user, initiator=request.user, dimension=ReputationDimension.objects.get('Vote'), related_object=st, is_vote=True) consensus.register_vote(st, 'delete', old_vote=st.vote) update_agent.send(sender=user_cons, type="vote", params=[vote_type_str, st.pk]) st.delete() else: try: #TODO: there is an error happening here! old = votemodel.objects.get(user=request.user, object_pk=pk) if old.vote != vote: old_vote_pos = old.vote old.vote = vote old.save() if register: user_cons.register_vote(old, 'change', old_vote=old_vote_pos) vote_created_callback(sender=request.session.session_key, parent=consensus, vote_type=vote) consensus.register_vote(old, 'change', old_vote=old_vote_pos) except: st, is_new = votemodel.objects.get_or_create( user=request.user, parent=consensus, vote=vote, object_pk=pk, parent_pk=consensus.parent_pk) st.save() check_badges(consensus.content_object.user, votemodel, pk) # register reputation for voting vote_created_callback(sender=request.session.session_key, parent=consensus, vote_type=vote) if register: aso_rep_event.send( sender=request.user, event_score=1, user=consensus.content_object.user, initiator=request.user, dimension=ReputationDimension.objects.get('Vote'), related_object=st, is_vote=True) user_cons.register_vote(st, 'register') consensus.register_vote(st, 'register') update_agent.send(sender=user_cons, type="vote", params=[vote_type_str, st.pk])
def flagvote(request): if not request.user.is_authenticated() or not request.user.is_active: #needs to popup registration dialog instead return HttpResponse( simplejson.dumps({'FAIL': True}), #need a better non-auth error here, interferes with view mimetype='application/json') if request.method == 'POST': vote = int(request.POST[u'vote']) pk = int(request.POST[u'pk']) flag_type = str(request.POST['flag_type']) user_pk = int(request.POST['user']) if request.user.pk != user_pk: return HttpResponse(simplejson.dumps({'FAIL': True}), mimetype='application/json') user = User(pk=user_pk) flag = Flag.objects.get(object_pk=pk, flag_type=flag_type) uflag, created = UserFlag.objects.get_or_create(user=user, flag=flag) cons = Consensus.objects.get(object_pk=pk) count = 0 v = bool(vote) votestr = '' if uflag.mode == v and not created: if v == True: flag.votes = flag.votes - 1 count = flag.votes votestr = 'vote_up_flat' elif v == False: flag.counters = flag.counters - 1 count = flag.counters votestr = 'vote_down_flat' flag.save() aso_rep_delete.send( sender=user, event_score=1, user=user, initiator=user, dimension=ReputationDimension.objects.get('Flag'), related_object=uflag) # delete reputation is flag removed uflag.delete() elif created: uflag.mode = v if v == True: flag.votes = flag.votes + 1 count = flag.votes votestr = 'vote_up_acti' elif v == False: flag.counters = flag.counters + 1 count = flag.counters votestr = 'vote_down_acti' uflag.save() flag.save() check_badges(cons.content_object.user, Flag, pk) aso_rep_event.send( sender=user, event_score=1, user=flag.content_object.user, initiator=user, dimension=ReputationDimension.objects.get('Flag'), related_object=uflag) # register reputation for flag creation imgsrc = '/static/voteimgs/' + votestr + '.gif' results = { 'FAIL': False, 'count': count, 'modes': str(vote == 0), 'imgsrc': imgsrc } if 'application/json' in request.META.get('HTTP_ACCEPT', ''): return HttpResponse(simplejson.dumps(results), mimetype='application/json')
def flagvote(request): if not request.user.is_authenticated() or not request.user.is_active: # needs to popup registration dialog instead return HttpResponse( simplejson.dumps({"FAIL": True}), # need a better non-auth error here, interferes with view mimetype="application/json", ) if request.method == "POST": vote = int(request.POST[u"vote"]) pk = int(request.POST[u"pk"]) flag_type = str(request.POST["flag_type"]) user_pk = int(request.POST["user"]) if request.user.pk != user_pk: return HttpResponse(simplejson.dumps({"FAIL": True}), mimetype="application/json") user = User(pk=user_pk) flag = Flag.objects.get(object_pk=pk, flag_type=flag_type) uflag, created = UserFlag.objects.get_or_create(user=user, flag=flag) cons = Consensus.objects.get(object_pk=pk) count = 0 v = bool(vote) votestr = "" if uflag.mode == v and not created: if v == True: flag.votes = flag.votes - 1 count = flag.votes votestr = "vote_up_flat" elif v == False: flag.counters = flag.counters - 1 count = flag.counters votestr = "vote_down_flat" flag.save() aso_rep_delete.send( sender=user, event_score=1, user=user, initiator=user, dimension=ReputationDimension.objects.get("Flag"), related_object=uflag, ) # delete reputation is flag removed uflag.delete() elif created: uflag.mode = v if v == True: flag.votes = flag.votes + 1 count = flag.votes votestr = "vote_up_acti" elif v == False: flag.counters = flag.counters + 1 count = flag.counters votestr = "vote_down_acti" uflag.save() flag.save() check_badges(cons.content_object.user, Flag, pk) aso_rep_event.send( sender=user, event_score=1, user=flag.content_object.user, initiator=user, dimension=ReputationDimension.objects.get("Flag"), related_object=uflag, ) # register reputation for flag creation imgsrc = "/static/voteimgs/" + votestr + ".gif" results = {"FAIL": False, "count": count, "modes": str(vote == 0), "imgsrc": imgsrc} if "application/json" in request.META.get("HTTP_ACCEPT", ""): return HttpResponse(simplejson.dumps(results), mimetype="application/json")
def pp_blob_form(context, nodelist, *args, **kwargs): '''form_id = forms.CharField(widget=forms.HiddenInput(), initial="pp-issue-form") This block tag can create or process forms either to create or to modify issues. Usage is as follows: {% pp_blob_form POST=request.POST path=request.path parent=request.object %} Do stuff with {{ pp-issue.form }}. {% endpp_blob_form %} This form has become a little overloaded. ''' context.push() namespace = get_namespace(context) POST = kwargs.get('POST', None) parent = kwargs.get('parent', None) user = kwargs.get('user', None) dimension = kwargs.get('dimension', None) obj = kwargs.get('object', None) edit = kwargs.get('edit', None) #subcontext imparted to form namespace['timezones'] = pytz.common_timezones #for editting objects if edit and obj is not None: blob_form, model, verbose_name = get_form(dimension) blob_form = BlobEditForm if POST: #save editted form form = blob_form(POST, auto_id='id_for_%s_' + str(obj.pk), prefix=user.username) #get info for diff old_text = obj.description if form.is_valid(): #blob = form.save(commit=False) obj.description = form.cleaned_data['description'] obj.summary = form.cleaned_data['summary'] #blob.description = urlize(blob.description, trim_url_limit=30, nofollow=True) obj.save() new_text = form.cleaned_data['description'] #create edit ctype = ContentType.objects.get_for_model(obj) #compute diff using diff_match_patch from google create_edit.apply_async(args=[obj.pk, user.pk, ctype.pk, new_text, old_text]) namespace['form_complete'] = True namespace['path'] = obj namespace['form'] = form else: namespace['errors'] = form.errors namespace['form'] = form else: form = blob_form(initial={'summary': obj.summary, 'description': obj.description}, prefix=user.username, auto_id='id_for_%s_' + str(obj.pk)) namespace['form'] = form elif dimension is not None: if isinstance(parent, unicode): parent = None elif parent is not None: if ContentType.objects.get_for_model(parent) == ContentType.objects.get_for_model(User): parent = None blob_form, model, verbose_name = get_form(dimension) fd = ForumDimension.objects.get(key=dimension) #get appropriate form if POST and user is not None: comboform = ComboFormFactory(TopicForm(POST), blob_form(POST)).ComboForm() namespace['form'] = comboform for form in comboform._forms: if form.is_valid(): #if this is a TopicForm, we must extract the parent if 'group' in form.cleaned_data: try: parent = Topic.objects.get(summary=form.cleaned_data['group']) cnt = MyGroup.objects.filter(topic=parent, user=user) if cnt == 0: namespace['grouperrors'] = 'You are not a member of that Group.' output = nodelist.render(context) context.pop() return output except: namespace['grouperrors'] = 'This Group does not exist.' output = nodelist.render(context) context.pop() return output else: #blob form if parent is not None: parent.children += 1 parent.save() blob = form.save(commit=False) blob.forumdimension = fd blob.user = user blob.description = form.cleaned_data['blobdescription'] #blob.description = urlize(blob.description, trim_url_limit=30, nofollow=True) blob.parent_pk = parent.pk blob.parent_type = ContentType.objects.get_for_model(parent) ctype = ContentType.objects.get_for_model(Topic) if blob.parent_type != ctype: parent.parent.solutions += 1 parent.parent.save() #####relevant to VOTING and TIME now = datetime.datetime.now() now = now.replace(tzinfo=pytz.utc) blob.save() contype = ContentType.objects.get_for_model(blob.__class__) #CONSENSUS RELATED DENORMALIZATION # All ForumBlobs have consensus objects attached try: parent_cons = Consensus.objects.get(object_pk=parent.pk) if parent_cons.child_vote_type != None: cvt = parent_cons.child_vote_type else: cvt = ContentType.objects.get_for_model(UpDownVote) except: cvt = ContentType.objects.get_for_model(UpDownVote) cons, is_new = Consensus.objects.get_or_create(content_type=contype, object_pk=blob.pk, vote_type=cvt, parent_pk=blob.parent_pk) if is_new: cons.intiate_vote_distributions() #deprecated but lets holdon to phases for now cons.phasename = "temp" cons.save() aso_rep_event.send(sender=user, event_score=1, user=user, initiator=user, dimension=ReputationDimension.objects.get(name=blob.get_verbose_name()), related_object=cons) update_agent.send(sender=blob, type="content", params=[ContentType.objects.get_for_model(blob.__class__).app_label, verbose_name.lower(), blob.pk]) #check badges for this model on creation check_badges(user, model, user) #update the dimensiontracker, used for sorting by content type #deferred.defer(defer_dimensiontracker_update, parent, dimension) #if is_new: #if this is a new issue/consensus, send signal for reputation #relationship_event.send(sender=issue,obj=issue,parent=issue.topic) namespace['path'] = blob namespace['form_complete'] = True #provide context with extension path #raise HttpRedirectException(HttpResponseRedirect(path)) else: namespace['errors'] = form.errors output = nodelist.render(context) context.pop() return output else: blob_form, model, verbose_name = get_form(dimension) if parent is not None: form = ComboFormFactory(TopicForm(auto_id='id_for_%s_' + str(parent.pk), initial={'group': str(parent.summary)}), blob_form()).ComboForm() else: form = ComboFormFactory(TopicForm(), blob_form()).ComboForm() namespace['form'] = form namespace['POST'] = POST, parent #try to set ids for side effects try: namespace['object'] = cons namespace['object_pk'] = blob.pk ctype = ContentType.objects.get_for_model(blob) namespace['content_type'] = ctype.pk except: pass if parent is not None: if isinstance(parent, Topic): namespace['parent_summary'] = parent.summary if isinstance(parent, ForumBlob): namespace['parent_summary'] = parent.summary elif isinstance(parent, User): namespace['parent_summary'] = parent.username output = nodelist.render(context) context.pop() return output
def pp_blob_form(context, nodelist, *args, **kwargs): '''form_id = forms.CharField(widget=forms.HiddenInput(), initial="pp-issue-form") This block tag can create or process forms either to create or to modify issues. Usage is as follows: {% pp_blob_form POST=request.POST path=request.path parent=request.object %} Do stuff with {{ pp-issue.form }}. {% endpp_blob_form %} This form has become a little overloaded. ''' context.push() namespace = get_namespace(context) POST = kwargs.get('POST', None) parent = kwargs.get('parent', None) user = kwargs.get('user', None) dimension = kwargs.get('dimension', None) obj = kwargs.get('object', None) sub = kwargs.get('sub', None) edit = kwargs.get('edit', None) #subcontext imparted to form namespace['timezones'] = pytz.common_timezones if parent == 'submit': parent = None #for editting objects if edit and obj is not None: blob_form, model, verbose_name = get_form(dimension) blob_form = BlobEditForm if POST: #save editted form form = blob_form(POST, instance=obj, prefix=user.username) #get info for diff old_text = obj.description blob = form.save(commit=False) cleaned_data = form.clean() for k, v in cleaned_data.items(): setattr(blob, k, v) #blob.description = urlize(blob.description, trim_url_limit=30, nofollow=True) blob.save() new_text = blob.description #create edit ctype = ContentType.objects.get_for_model(blob) #compute diff using diff_match_patch from google create_edit.apply_async(args=[blob.pk, user.pk, ctype.pk, new_text, old_text]) content_type = ContentType.objects.get_for_model(obj.__class__) namespace['form_complete'] = True namespace['path'] = obj namespace['form'] = form else: form = blob_form(instance=obj) try: if obj.link is not None: form = blob_form(instance=obj, initial={'link': str(obj.link)}, prefix=user.username) except: pass namespace['form'] = form elif dimension is not None: if parent is not None: if ContentType.objects.get_for_model(parent) == ContentType.objects.get_for_model(User): parent = None blob_form, model, verbose_name = get_form(dimension) fd = ForumDimension.objects.get(key=dimension) #get appropriate form if POST and user is not None: comboform = ComboFormFactory(TopicForm(POST), blob_form(POST)).ComboForm() namespace['form'] = comboform for form in comboform._forms: if form.is_valid(): #if this is a TopicForm, we must extract the parent if 'group' in form.cleaned_data: try: parent = Topic.objects.get(summary=form.cleaned_data['group']) cnt = MyGroup.objects.filter(topic=parent, user=user) if cnt == 0: namespace['grouperrors'] = 'You are not a member of that Group.' output = nodelist.render(context) context.pop() return output except: namespace['grouperrors'] = 'This Group does not exist.' output = nodelist.render(context) context.pop() return output else: #blob form if parent is not None: parent.children += 1 parent.save() blob = form.save(commit=False) blob.forumdimension = fd blob.user = user blob.description = form.cleaned_data['blobdescription'] #blob.description = urlize(blob.description, trim_url_limit=30, nofollow=True) blob.parent_pk = parent.pk blob.parent_type = ContentType.objects.get_for_model(parent) ctype = ContentType.objects.get_for_model(Topic) if blob.parent_type != ctype: parent.parent.solutions += 1 parent.parent.save() #####relevant to VOTING and TIME now = datetime.datetime.now() now = now.replace(tzinfo=pytz.utc) blob.save() contype = ContentType.objects.get_for_model(blob.__class__) #CONSENSUS RELATED DENORMALIZATION # All ForumBlobs have consensus objects attached try: parent_cons = Consensus.objects.get(object_pk=parent.pk) if parent_cons.child_vote_type != None: cvt = parent_cons.child_vote_type else: cvt = ContentType.objects.get_for_model(UpDownVote) except: cvt = ContentType.objects.get_for_model(UpDownVote) cons, is_new = Consensus.objects.get_or_create(content_type=contype, object_pk=blob.pk, vote_type=cvt, parent_pk=blob.parent_pk) if is_new: cons.intiate_vote_distributions() #deprecated but lets holdon to phases for now cons.phasename = "temp" cons.save() aso_rep_event.send(sender=user, event_score=1, user=user, initiator=user, dimension=ReputationDimension.objects.get(name=blob.get_verbose_name()), related_object=cons) update_agent.send(sender=blob, type="content", params=[ContentType.objects.get_for_model(blob.__class__).app_label, verbose_name.lower(), blob.pk]) #check badges for this model on creation check_badges(user, model, user) #update the dimensiontracker, used for sorting by content type #deferred.defer(defer_dimensiontracker_update, parent, dimension) #if is_new: #if this is a new issue/consensus, send signal for reputation #relationship_event.send(sender=issue,obj=issue,parent=issue.topic) namespace['path'] = blob namespace['form_complete'] = True #provide context with extension path #raise HttpRedirectException(HttpResponseRedirect(path)) else: namespace['errors'] = form.errors output = nodelist.render(context) context.pop() return output else: blob_form, model, verbose_name = get_form(dimension) form = ComboFormFactory(TopicForm(), blob_form()).ComboForm() namespace['form'] = form namespace['POST'] = POST, parent #try to set ids for side effects try: namespace['object'] = cons namespace['object_pk'] = blob.pk ctype = ContentType.objects.get_for_model(blob) namespace['content_type'] = ctype.pk except: pass if parent is not None: if isinstance(parent, Topic): namespace['parent_summary'] = parent.summary if isinstance(parent, ForumBlob): namespace['parent_summary'] = parent.summary elif isinstance(parent, User): namespace['parent_summary'] = parent.username output = nodelist.render(context) context.pop() return output