def create(request, section, **kwargs): model = get_model(section) name = get_name(section) tags = [] t = model.tags.all() for tag in t: tags.append(tag.name) tags = sorted(tags, key=lambda s: s.lower()) exclude_fields = get_exclude_fields(model) form = get_create_form(request, exclude_fields, model, kwargs) if request.method == "POST": state = set_state(request, form) if form.is_valid(): instance = form.save(commit=False) slug = get_slug(request, model, instance.title, kwargs) user = get_user(request) instance.state = state instance.slug = slug instance.save() instance.authors.add(user) form.save_m2m() automoderate(instance, user) return render(request, 'complete.html', { 'section': section, 'slug': slug, 'state': state, 'name': name }) return render(request, 'creation.html', { 'form': form, 'name': name, 'tags': tags })
def test_can_update_objects_with_bypass_enabled(self): obj = ModelWithVisibilityField.objects.create(test='initial') obj.save() # It's never been approved before, so it's now invisible self.assertEqual( [], list(ModelWithVisibilityField.objects.all()), "The ModelWithVisibilityField has never been approved and is now " "pending, so it should be hidden") # So approve it obj.moderated_object.approve(by=self.user, reason='test') # Now it should be visible, with the new description obj = ModelWithVisibilityField.objects.get() self.assertEqual('initial', obj.test) # Now change it again. Because bypass_moderation_after_approval is # True, it should still be visible and we shouldn't need to approve it # again. obj.test = 'modified' obj.save() obj = ModelWithVisibilityField.objects.get() self.assertEqual('modified', obj.test) # Admin does this after saving an object. Check that it doesn't undo # our changes. automoderate(obj, self.user) obj = ModelWithVisibilityField.objects.get() self.assertEqual('modified', obj.test)
def create(request,section,**kwargs): model=get_model(section) name =get_name(section) tags=[] t=model.tags.all() for tag in t: tags.append(tag.name) tags= sorted(tags, key=lambda s: s.lower()) exclude_fields = get_exclude_fields(model) form = get_create_form(request,exclude_fields,model,kwargs) if request.method=="POST": state=set_state(request,form) if form.is_valid(): instance=form.save(commit=False) slug = get_slug(request,model,instance.title,kwargs) user=get_user(request) instance.state=state instance.slug=slug instance.save() instance.authors.add(user) form.save_m2m() automoderate(instance,user) try: if instance.state=="submitted": subject="New AstroPython Post !" message="A new post to AstroPython" html_m='<hr /><h2 style="text-align:center"><code><tt><span style="font-family:trebuchet ms,helvetica,sans-serif"><strong>AstroPython - Python for Astronomers</strong></span></tt></code></h2><hr /><p>A Post "'+instance.title+'" has been added. It is waiting for moderation !</p><p>Access it <a href="http://www.astropython.org/admin/moderation/moderatedobject/">here</a> !</p><p>Thank you,</p><p><strong>AstroPython Team</strong></p><hr /><p style="text-align:right"><span style="font-size:10px">Currently , you cannot unsubscribe to these emails</span></p>' from_email="*****@*****.**" send_mail(subject,message,from_email,["*****@*****.**","*****@*****.**"], fail_silently=False,html_message=html_m) except: print "No Email Provided" return render(request,'complete.html',{'section':section,'slug':slug,'state':state,'name':name}) return render(request,'creation.html',{'form':form,'name':name,'tags': tags})
def single(request,section,slug,**kwargs): model=get_model(section) name=get_name(section) tags=model.tags.all() obj=model.objects.get(slug=slug) mode="display" if request.method=="GET" and 'edit' in request.GET: edit=request.GET['edit'] edit_field=edit.split(',') request.session['edit_field']=edit_field request.session.modified = True form= PostForm(model,edit_field,'edit',instance=obj) mode="edit" elif request.method=="POST": form= PostForm(model,request.session['edit_field'],'edit',request.POST,instance=obj) mode="edit" if form.is_valid(): instance=form.save(commit=False) user=get_user(request) instance.save() form.save_m2m() automoderate(instance,user) return HttpResponseRedirect(reverse('single',kwargs={'section':section,'slug':obj.slug})) else: form=None recent=model.objects.all().filter(state="submitted").order_by('-published')[:5] return render(request,'single.html',{'obj':obj,'section':section,'full_url':request.build_absolute_uri(),"mode":mode,"form":form,"tags":tags,'page':'single','recent':recent})
def edit_gov_annual_attr(request): attr_id = request.POST.get('attr_id') new_val = request.POST.get('new_val') is_from_source = request.POST.get('is_from_source') if is_from_source == '1': is_from_source = True elif is_from_source == '0': is_from_source = False else: is_from_source = None try: obj = GovernmentAnnualAttribute.objects.get(id=attr_id) obj.attribute_value = new_val obj.is_from_source = is_from_source # disconnect signal becuase of moderation post_save.disconnect(recalculate, sender=GovernmentAnnualAttribute) moderation.pre_save_handler(sender=GovernmentAnnualAttribute, instance=obj) obj.save() moderation.post_save_handler(sender=GovernmentAnnualAttribute, instance=obj, created=False) post_save.connect(recalculate, sender=GovernmentAnnualAttribute) automoderate(obj, request.user) return JsonResponse({'result': 'success'}) except GovernmentAnnualAttribute.DoesNotExist: return JsonResponse({ 'result': 'fail', 'msg': 'There is no matching record.' })
def test_can_update_objects_with_bypass_enabled(self): obj = ModelWithVisibilityField.objects.create(test='initial') obj.save() # It's never been approved before, so it's now invisible self.assertEqual( [], list(ModelWithVisibilityField.objects.all()), "The ModelWithVisibilityField has never been approved and is now " "pending, so it should be hidden") # So approve it obj.moderated_object.approve(moderated_by=self.user, reason='test') # Now it should be visible, with the new description obj = ModelWithVisibilityField.objects.get() self.assertEqual('initial', obj.test) # Now change it again. Because bypass_moderation_after_approval is # True, it should still be visible and we shouldn't need to approve it # again. obj.test = 'modified' obj.save() obj = ModelWithVisibilityField.objects.get() self.assertEqual('modified', obj.test) # Admin does this after saving an object. Check that it doesn't undo # our changes. automoderate(obj, self.user) obj = ModelWithVisibilityField.objects.get() self.assertEqual('modified', obj.test)
def single(request,section,slug,**kwargs): model=get_model(section) name=get_name(section) tags=model.tags.all() obj=model.objects.get(slug=slug) mode="display" if request.method=="GET" and 'edit' in request.GET: edit=request.GET['edit'] edit_field=edit.split(',') request.session['edit_field']=edit_field request.session.modified = True form= PostForm(model,edit_field,'edit',instance=obj) mode="edit" elif request.method=="POST": form= PostForm(model,request.session['edit_field'],'edit',request.POST,instance=obj) mode="edit" if form.is_valid(): instance=form.save(commit=False) user=get_user(request) instance.save() form.save_m2m() automoderate(instance,user) return HttpResponseRedirect(reverse('single',kwargs={'section':section,'slug':obj.slug})) else: form=None recent=model.objects.all().filter(state="submitted").order_by('-created')[:5] return render(request,'single.html',{'obj':obj,'section':section,'full_url':request.build_absolute_uri(),"mode":mode,"form":form,"tags":tags,'page':'single','recent':recent})
def create(request,section,**kwargs): model=get_model(section) name =get_name(section) tags=[] t=model.tags.all() for tag in t: tags.append(tag.name) tags= sorted(tags, key=lambda s: s.lower()) exclude_fields = get_exclude_fields(model) form = get_create_form(request,exclude_fields,model,kwargs) if request.method=="POST": state=set_state(request,form) if form.is_valid(): instance=form.save(commit=False) slug = get_slug(request,model,instance.title,kwargs) user=get_user(request) instance.state=state instance.slug=slug instance.save() instance.authors.add(user) form.save_m2m() automoderate(instance,user) try: if instance.state=="submitted": subject="New AstroPython Post !" message="A new post to AstroPython" html_m='<hr /><h2 style="text-align:center"><code><tt><span style="font-family:trebuchet ms,helvetica,sans-serif"><strong>AstroPython - Python for Astronomers</strong></span></tt></code></h2><hr /><p>A Post "'+instance.title+'" has been added. It is waiting for moderation !</p><p>Access it <a href="http://www.astropython.org'+instance.get_absolute_url()+'">here</a> !</p><p>Thank you,</p><p><strong>AstroPython Team</strong></p><hr /><p style="text-align:right"><span style="font-size:10px">Currently , you cannot unsubscribe to these emails</span></p>' from_email="*****@*****.**" send_mail(subject,message,from_email,["*****@*****.**"], fail_silently=False,html_message=html_m) except: print "No Email Provided" return render(request,'complete.html',{'section':section,'slug':slug,'state':state,'name':name}) return render(request,'creation.html',{'form':form,'name':name,'tags': tags})
def add_plan_annual_attr(request): attr_id = request.POST.get('attr_id') plan_id = request.POST.get('plan_id') year = request.POST.get('year') value = request.POST.get('value', '0') is_from_source = request.POST.get('is_from_source') if is_from_source == '1': is_from_source = True elif is_from_source == '0': is_from_source = False else: is_from_source = None try: plan_attr_obj = PlanAttribute.objects.get(id=attr_id) plan_obj = Plan.objects.get(id=plan_id) try: # check if already exists old_obj = PlanAnnualAttribute.objects.get( plan=plan_obj, year=year, plan_attribute=plan_attr_obj) return JsonResponse({'result': 'fail', 'msg': 'Already exists.'}) except PlanAnnualAttribute.DoesNotExist: pass except PlanAnnualAttribute.MultipleObjectsReturned: return JsonResponse({'result': 'fail', 'msg': 'Already exists.'}) new_plan_annual_attr_obj = PlanAnnualAttribute( plan=plan_obj, year=year, plan_attribute=plan_attr_obj, attribute_value=value, is_from_source=is_from_source) # disconnect signal because of moderation post_save.disconnect(recalculate, sender=PlanAnnualAttribute) moderation.pre_save_handler(sender=PlanAnnualAttribute, instance=new_plan_annual_attr_obj) new_plan_annual_attr_obj.save() moderation.post_save_handler(sender=PlanAnnualAttribute, instance=new_plan_annual_attr_obj, created=True, view_able=True) post_save.connect(recalculate, sender=PlanAnnualAttribute) automoderate(new_plan_annual_attr_obj, request.user) return JsonResponse({ 'result': 'success', 'id': new_plan_annual_attr_obj.id }) except Exception as e: traceback.print_exc() return JsonResponse({'result': 'fail', 'msg': 'Something went wrong.'})
def test_RuntimeError(self): from moderation.helpers import automoderate profile = UserProfile.objects.get(user__username='******') profile.description = 'Change description' profile.save() profile.moderated_object.changed_by = self.user profile.moderated_object.save() automoderate(profile, self.user) profile.moderated_object.save()
def add_gov_annual_attr(request): attr_id = request.POST.get('attr_id') gov_id = request.POST.get('gov_id') year = request.POST.get('year') value = request.POST.get('value', '0') is_from_source = request.POST.get('is_from_source') if is_from_source == '1': is_from_source = True elif is_from_source == '0': is_from_source = False else: is_from_source = None try: gov_attr_obj = GovernmentAttribute.objects.get(id=attr_id) gov_obj = Government.objects.get(id=gov_id) try: # check if already exists old_obj = GovernmentAnnualAttribute.objects.get( government=gov_obj, year=year, government_attribute=gov_attr_obj) return JsonResponse({'result': 'fail', 'msg': 'Already exists.'}) except GovernmentAnnualAttribute.DoesNotExist: pass except GovernmentAnnualAttribute.MultipleObjectsReturned: return JsonResponse({'result': 'fail', 'msg': 'Already exists.'}) new_gov_annual_attr_obj = GovernmentAnnualAttribute( government=gov_obj, year=year, government_attribute=gov_attr_obj, attribute_value=value, is_from_source=is_from_source) # disconnect signal becuase of moderation post_save.disconnect(recalculate, sender=PlanAnnualAttribute) moderation.pre_save_handler(sender=PlanAnnualAttribute, instance=new_gov_annual_attr_obj) new_gov_annual_attr_obj.save() moderation.post_save_handler(sender=PlanAnnualAttribute, instance=new_gov_annual_attr_obj, created=True) post_save.connect(recalculate, sender=PlanAnnualAttribute) automoderate(new_gov_annual_attr_obj, request.user) return JsonResponse({'result': 'success'}) except Exception as e: return JsonResponse({'result': 'fail', 'msg': 'Something went wrong.'})
def save_model(self, request, obj, form, change): # This is because django-model translations monkey patches # the base manager object causing the base manager to inherit # from a user defined manager. # This breaks object saving because when determining to do # an INSERT or an UPDATE, it does a test to see if the object # is in the _base_manager queryset. In our case it won't be if: # # The object hasn't been approved yet as the _base_manager query # will be searching for an object that's in a published state obj.__class__._base_manager.__class__ = models.Manager obj.save() automoderate(obj, request.user)
def test_auto_approve_helper_status_approved(self): self.profile.description = 'New description' self.profile.save() status = automoderate(self.profile, self.user) self.assertEqual(status, MODERATION_STATUS_APPROVED) profile = UserProfile.objects.get(user__username='******') self.assertEqual(profile.description, 'New description')
def test_auto_approve_helper_status_rejected(self): group = Group(name='banned') group.save() self.user.groups.add(group) self.user.save() self.profile.description = 'New description' self.profile.save() status = automoderate(self.profile, self.user) profile = UserProfile.objects.get(user__username='******') self.assertEqual(status, MODERATION_STATUS_REJECTED) self.assertEqual(profile.description, u'Old description')
def create(request,section,**kwargs): model=get_model(section) name =get_name(section) tags=[] t=model.tags.all() for tag in t: tags.append(tag.name) tags= sorted(tags, key=lambda s: s.lower()) exclude_fields = get_exclude_fields(model) form = get_create_form(request,exclude_fields,model,kwargs) if request.method=="POST": state=set_state(request,form) if form.is_valid(): instance=form.save(commit=False) slug = get_slug(request,model,instance.title,kwargs) user=get_user(request) instance.state=state instance.slug=slug instance.save() instance.authors.add(user) form.save_m2m() automoderate(instance,user) return render(request,'complete.html',{'section':section,'slug':slug,'state':state,'name':name}) return render(request,'creation.html',{'form':form,'name':name,'tags': tags})
def test_auto_approve_helper_status_rejected(self): group = Group(name="banned") group.save() self.user.groups.add(group) self.user.save() self.profile.description = "New description" self.profile.save() status = automoderate(self.profile, self.user) profile = UserProfile.objects.get(user__username="******") self.assertEqual(status, MODERATION_STATUS_REJECTED) self.assertEqual(profile.description, u"Old description")
def save_model(self, request, obj, form, change): obj.save() automoderate(obj, request.user)