def get_status_display(self): """ Return the displayable value for the current status (replace the original get_FIELD_display for this field which act as a field with choices) """ statuses = dict(flag_settings.get_for_model(self.content_object, "STATUSES")) return force_unicode(statuses[self.status], strings_only=True)
def get_status_display(self): """ Return the displayable value for the current status (replace the original get_FIELD_display for this field which act as a field with choices) """ statuses = dict( flag_settings.get_for_model(self.content_object, 'STATUSES')) return force_unicode(statuses[self.status], strings_only=True)
def test_create_form(self): """ Test the creation of the form """ flag_settings.ALLOW_COMMENTS = False form = get_default_form(self.model_without_author) # check the used form self.assertTrue(isinstance(form, FlagForm)) self.assertFalse(isinstance(form, FlagFormWithCreator)) # check data self.assertEqual(form['object_pk'].value(), str(self.model_without_author.id)) self.assertEqual(form['content_type'].value(), '%s.%s' % ( self.model_without_author._meta.app_label, self.model_without_author._meta.module_name)) # check security self.assertTrue('timestamp' in form.fields) self.assertTrue('security_hash' in form.fields) # check with_author form = get_default_form(self.model_with_author, 'author') self.assertTrue(isinstance(form, FlagFormWithCreator)) self.assertEqual(form['creator_field'].value(), 'author') # check with_status form = get_default_form(self.model_without_author, with_status=True) self.assertTrue(isinstance(form, FlagFormWithStatus)) self.assertEqual(form['status'].field.choices, flag_settings.get_for_model( self.model_without_author, 'STATUSES')) # check with_status and with_author form = get_default_form(self.model_with_author, 'author', with_status=True) self.assertTrue(isinstance(form, FlagFormWithCreatorAndStatus)) self.assertEqual(form['creator_field'].value(), 'author') self.assertEqual(form['status'].field.choices, flag_settings.get_for_model( self.model_with_author, 'STATUSES'))
def test_flag_settings(self): """ Test settings """ model = ModelWithAuthor model_name = 'tests.modelwithauthor' self.assertEqual('.'.join(get_content_type_tuple(model)), model_name) # no settings for this model flag_settings.MODELS_SETTINGS = {} flag_settings.SEND_MAILS = True self.assertEqual(flag_settings.SEND_MAILS, flag_settings.get_for_model(model_name, 'SEND_MAILS')) self.assertEqual(flag_settings.SEND_MAILS, flag_settings.get_for_model(model, 'SEND_MAILS')) # a setting for this model flag_settings.MODELS_SETTINGS[model_name] = {} flag_settings.MODELS_SETTINGS[model_name]['SEND_MAILS'] = False self.assertNotEqual(flag_settings.SEND_MAILS, flag_settings.get_for_model(model_name, 'SEND_MAILS')) self.assertNotEqual(flag_settings.SEND_MAILS, flag_settings.get_for_model(model, 'SEND_MAILS')) # bad model self.assertEqual(flag_settings.SEND_MAILS, flag_settings.get_for_model('bad-model', 'SEND_MAILS')) # forbidden setting flag_settings.MODELS = (model_name,) flag_settings.MODELS_SETTINGS = {} self.assertEqual(flag_settings.MODELS, flag_settings.get_for_model(model_name, 'MODELS')) flag_settings.MODELS_SETTINGS[model_name] = {} flag_settings.MODELS_SETTINGS[model_name]['MODELS'] = ( 'tests.modelwithoutauthor',) self.assertEqual(flag_settings.MODELS, flag_settings.get_for_model(model_name, 'MODELS')) # inexistint setting self.assertRaises(AttributeError, flag_settings.get_for_model, model_name, 'INEXISTING_SETTINGS')
def clean(self): """ Manage the `ALLOW_COMMENTS` settings """ cleaned_data = super(FlagForm, self).clean() content_type = cleaned_data.get('content_type', None) if content_type is not None: allow_comments = flag_settings.get_for_model( content_type, 'ALLOW_COMMENTS') comment = cleaned_data.get('comment', None) if allow_comments and not comment: self._errors['comment'] = self.error_class( [_('You must add a comment')]) elif not allow_comments and comment: del cleaned_data['comment'] raise forms.ValidationError( _('You are not allowed to add a comment')) return cleaned_data
def clean(self): """ Manage the `ALLOW_COMMENTS` settings """ cleaned_data = super(FlagForm, self).clean() content_type = cleaned_data.get('content_type', None) if content_type is not None: allow_comments = flag_settings.get_for_model(content_type, 'ALLOW_COMMENTS') comment = cleaned_data.get('comment', None) if allow_comments and not comment: self._errors['comment'] = self.error_class( [_('You must add a comment')]) elif not allow_comments and comment: del cleaned_data['comment'] raise forms.ValidationError( _('You are not allowed to add a comment')) return cleaned_data
def content_settings(self, name): """ Return the settings `name` for the current content object """ return flag_settings.get_for_model(self.content_object, name)
def flag(request): """ Validate the form and create the flag. In all cases, redirect to the `next` parameter. """ if request.method == 'POST': post_data = request.POST.copy() # only staff can update status with_status = 'status' in post_data if with_status: try: assert_user_can_change_status(request.user) except OnlyStaffCanUpdateStatus, e: return FlagBadRequest(str(e)) # the object to flag object_pk = post_data.get('object_pk') content_object = get_content_object(post_data.get("content_type"), object_pk) if (isinstance(content_object, HttpResponseBadRequest)): return content_object content_type = ContentType.objects.get_for_model(content_object) # get the form class regrding if we have a creator_field form_class = FlagForm if 'creator_field' in post_data: form_class = FlagFormWithCreator if with_status: form_class = FlagFormWithCreatorAndStatus elif with_status: form_class = FlagFormWithStatus form = form_class(target_object=content_object, data=post_data) if form.security_errors(): return FlagBadRequest( "The flag form failed security verification: %s" % \ escape(str(form.security_errors()))) if form.is_valid(): # manage creator creator = None if form_class == FlagFormWithCreator: creator_field = form.cleaned_data['creator_field'] if creator_field: creator = getattr(content_object, creator_field, None) # manage comment if flag_settings.get_for_model(content_object, 'ALLOW_COMMENTS'): comment = form.cleaned_data['comment'] else: comment = None # manage status status = form.cleaned_data.get( 'status', flag_settings.DEFAULT_STATUS) or flag_settings.DEFAULT_STATUS # add the flag, but check the user can do it try: FlagInstance.objects.add(request.user, content_object, creator, comment, status, send_signal=True, send_mails=True) except FlagException, e: messages.error(request, unicode(e)) else: messages.success( request, _("You have added a flag. A " "moderator will review your submission shortly."))
def __init__status_choices__(self): """ Initialize the status choices """ self.fields['status'].choices = flag_settings.get_for_model( self.target_object, 'STATUSES')
def flag(request): """ Validate the form and create the flag. In all cases, redirect to the `next` parameter. """ if request.method == "POST": post_data = request.POST.copy() # only staff can update status with_status = "status" in post_data if with_status: try: assert_user_can_change_status(request.user) except OnlyStaffCanUpdateStatus, e: return FlagBadRequest(str(e)) # the object to flag object_pk = post_data.get("object_pk") content_object = get_content_object(post_data.get("content_type"), object_pk) if isinstance(content_object, HttpResponseBadRequest): return content_object content_type = ContentType.objects.get_for_model(content_object) # get the form class regrding if we have a creator_field form_class = FlagForm if "creator_field" in post_data: form_class = FlagFormWithCreator if with_status: form_class = FlagFormWithCreatorAndStatus elif with_status: form_class = FlagFormWithStatus form = form_class(target_object=content_object, data=post_data) if form.security_errors(): return FlagBadRequest( "The flag form failed security verification: %s" % escape(str(form.security_errors())) ) if form.is_valid(): # manage creator creator = None if form_class == FlagFormWithCreator: creator_field = form.cleaned_data["creator_field"] if creator_field: creator = getattr(content_object, creator_field, None) # manage comment if flag_settings.get_for_model(content_object, "ALLOW_COMMENTS"): comment = form.cleaned_data["comment"] else: comment = None # manage status status = form.cleaned_data.get("status", flag_settings.DEFAULT_STATUS) or flag_settings.DEFAULT_STATUS # add the flag, but check the user can do it try: FlagInstance.objects.add( request.user, content_object, creator, comment, status, send_signal=True, send_mails=True ) except FlagException, e: messages.error(request, unicode(e)) else: messages.success( request, _("You have added a flag. A " "moderator will review your submission shortly.") )
def flag(request): """ Validate the form and create the flag. In all cases, redirect to the `next` parameter. """ if request.method == 'POST': post_data = request.POST.copy() # only staff can update status with_status = 'status' in post_data # the object to flag object_pk = post_data.get('object_pk') content_object = get_content_object(post_data.get("content_type"), object_pk) if not can_be_flagged_by(content_object, request.user): return HttpResponseBadRequest() if isinstance(content_object, HttpResponseBadRequest): return content_object content_type = ContentType.objects.get_for_model(content_object) # get the form class regrding if we have a creator_field form_class = FlagForm if 'creator_field' in post_data: form_class = FlagFormWithCreator if with_status: form_class = FlagFormWithCreatorAndStatus elif with_status: form_class = FlagFormWithStatus form = form_class(target_object=content_object, data=post_data) if form.security_errors(): return FlagBadRequest( "The flag form failed security verification: %s" % \ escape(str(form.security_errors()))) if form.is_valid(): # manage creator creator = None if form_class == FlagFormWithCreator: creator_field = form.cleaned_data['creator_field'] if creator_field: creator = getattr(content_object, creator_field, None) # manage comment if flag_settings.get_for_model(content_object, 'ALLOW_COMMENTS'): comment = form.cleaned_data['comment'] else: comment = None # manage status status = form.cleaned_data.get('status', flag_settings.DEFAULT_STATUS) or flag_settings.DEFAULT_STATUS # add the flag, but check the user can do it try: FlagInstance.objects.add(request.user, content_object, creator, comment, status, send_signal=True, send_mails=True) except FlagException, e: if request.is_ajax(): return HttpResponseBadRequest() else: messages.error(request, unicode(e)) else: if request.is_ajax(): return HttpResponse() else: messages.success( request, _("You have added a flag." " A moderator will review your submission shortly.") ) else: if request.is_ajax(): return HttpResponseBadRequest() else: # form not valid, we return to the confirm page return confirm(request, app_label=content_type.app_label, object_name=content_type.model, object_id=object_pk, form=form)
def __init__(self, *args, **kwargs): super(FlagForm, self).__init__(*args, **kwargs) content_type = kwargs.get('initial', {}).get('content_type') if not flag_settings.get_for_model(content_type, 'ALLOW_COMMENTS'): self.fields['comment'].widget = forms.HiddenInput()