+ generate_dc = forms.BooleanField(required=False, \
+ initial=True, label=u"Generate DublinCore template")
+
+
+ def clean(self):
+ clean_data = self.cleaned_data
+
+ ocr_file = clean_data['ocr_file']
+ ocr_data = clean_data['ocr_data']
+
+ if not ocr_file and not ocr_data:
+ raise forms.ValidationError(
+ "You must either provide file descriptor or raw data." )
+
+ return clean_data
+
+PRQ_USER_RE = re.compile(r"^\$prq-(\d{1,10})$", re.UNICODE)
+
+class DocumentRetrieveForm(forms.Form):
+ revision = forms.RegexField(regex=r'latest|[0-9a-z]{40}', required=False)
+ user = forms.CharField(required=False)
+
+ def clean_user(self):
+ # why, oh why does django doesn't implement this!!!
+ # value = super(DocumentRetrieveForm, self).clean_user()
+ value = self.cleaned_data['user']
+
+ if value.startswith('$'):
+ # library user (... maybe later)
+ if value == '$library':
+ raise forms.ValidationError("Invalid user name '%s'" % value)
+
+ m = PRQ_USER_RE.match(value)
+
+ if m:
+ try:
+ return value
+ except:
+ raise forms.ValidationError("User doesn't exist.")
+ raise forms.ValidationError("Invalid user name '%s'" % value)
+ try:
+ return value
+ except:
+ raise forms.ValidationError("User doesn't exist.")
+
+
+class TextRetrieveForm(DocumentRetrieveForm):
+ part = forms.CharField(required=False)
+
+class TextUpdateForm(DocumentRetrieveForm):
+ message = forms.CharField(required=False)
+ contents = forms.CharField(required=False)
+ chunks = forms.CharField(required=False)
+
+ def clean_message(self):
+ value = self.cleaned_data['message']
+
+ if value:
+ return u"$USER$ " + value
+ else:
+ return u"$AUTO$ XML content update."
+
+ def clean_chunks(self):
+ value = self.cleaned_data['chunks']
+
+ try:
+ return json.loads(value)
+ except Exception, e:
+ forms.ValidationError("Invalid JSON: " + e.message)
+
+
+ def clean(self):
+ if self.cleaned_data['contents'] and self.cleaned_data['chunks']:
+ raise forms.ValidationError("Pass either contents or chunks - not both ")
+
+ if not self.cleaned_data['contents'] and not self.cleaned_data['chunks']:
+ raise forms.ValidationError("You must pass contents or chunks.")