Esempio n. 1
0
    def clean(self):
        cleaned_data = self.cleaned_data
        content_type_id = cleaned_data.get("content_type", None)
        object_id = cleaned_data.get("object_id", None)
        if content_type_id is None or object_id is None:
            raise forms.ValidationError(_("Missing Parameter"))
        try:
            ct = ContentType.objects.get(id=content_type_id)
        except ContentType.DoesNotExist:
            raise forms.ValidationError(_("Bogus content"))
        try:
            obj = ct.get_object_for_this_type(id=object_id)
        except ct.model_class().DoesNotExist:
            raise forms.ValidationError(_("Bogus object"))
        cleaned_data["content_type"] = ct
        # Connected Model must have attribute annotatable set to True, 
        # so not every model can be annotated, don't know if this is cool in practice
        if not getattr(obj, "annotatable", False):
            raise forms.ValidationError(_("Invalid object"))
        if not hasattr(obj,Annotation.field_name):
            raise forms.ValidationError(_("Bogus object"))
        text = getattr(obj, Annotation.field_name)
        if len(text) != cleaned_data["lengthcheck"]:
            raise forms.ValidationError(_("Text changed"))
        if not "selection_start" in cleaned_data or not "selection_end" in cleaned_data:
            raise forms.ValidationError(_("Missing Parameter for selection"))
        if not Annotation.validate_selection(text, start=cleaned_data["selection_start"], end=cleaned_data["selection_end"]):
            raise forms.ValidationError(_("Bogus selection"))
        # disallow footnotes? uncomment following
#        if cleaned_data["selection_end"] == cleaned_data["selection_start"]:
#            raise forms.ValidationError(_("No Selection"))
        if "flags" not in cleaned_data or "selection_start" not in cleaned_data:
            raise forms.ValidationError(_("Missing parameters"))
        return cleaned_data
Esempio n. 2
0
def post_annotation(request):
    if request.method != "POST":
        return HttpResponseNotAllowed(["POST"])
    if request.user.is_anonymous():
        return HttpResponseForbidden(_("Sorry, only logged users can annotate."))
    form = NewAnnotationForm(request.POST)
    if form.is_valid():
        new_annotation = Annotation(content_type=form.cleaned_data["content_type"],
                    object_id=form.cleaned_data["object_id"],
                    user=request.user,
                    selection_start=form.cleaned_data["selection_start"],
                    selection_end=form.cleaned_data["selection_end"],
                    comment=form.cleaned_data["comment"],
                    flags=form.cleaned_data["flags"],
                    color=form.cleaned_data["color"])
        new_annotation.save()
        return HttpResponseRedirect(new_annotation.get_absolute_url())
    else:
        return HttpResponseBadRequest()