|
1 #!/usr/bin/python2.5 |
|
2 # |
|
3 # Copyright 2008 the Melange authors. |
|
4 # |
|
5 # Licensed under the Apache License, Version 2.0 (the "License"); |
|
6 # you may not use this file except in compliance with the License. |
|
7 # You may obtain a copy of the License at |
|
8 # |
|
9 # http://www.apache.org/licenses/LICENSE-2.0 |
|
10 # |
|
11 # Unless required by applicable law or agreed to in writing, software |
|
12 # distributed under the License is distributed on an "AS IS" BASIS, |
|
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
14 # See the License for the specific language governing permissions and |
|
15 # limitations under the License. |
|
16 |
|
17 """Generic cleaning methods |
|
18 """ |
|
19 |
|
20 __authors__ = [ |
|
21 '"Todd Larsen" <tlarsen@google.com>', |
|
22 '"Sverre Rabbelier" <sverre@rabbelier.nl>', |
|
23 ] |
|
24 |
|
25 |
|
26 from django import forms |
|
27 |
|
28 from soc.logic import validate |
|
29 |
|
30 |
|
31 def clean_new_link_id(logic): |
|
32 """Clean method for new link_id's |
|
33 |
|
34 Returns a clean method that checks if the specified link_id is |
|
35 in the proper format, and verifies that the link_id is not already in |
|
36 in use by another entity of the same type. |
|
37 """ |
|
38 |
|
39 def wrapped(self): |
|
40 link_id = self.cleaned_data.get('link_id') |
|
41 if not validate.isLinkIdFormatValid(link_id): |
|
42 raise forms.ValidationError("This link ID is in wrong format.") |
|
43 if logic.getFromFields(link_id=link_id): |
|
44 raise forms.ValidationError("This link ID is already in use.") |
|
45 return link_id |
|
46 |
|
47 return wrapped |
|
48 |
|
49 |
|
50 def clean_link_id(self): |
|
51 link_id = self.cleaned_data.get('link_id') |
|
52 if not validate.isLinkIdFormatValid(link_id): |
|
53 raise forms.ValidationError("This link ID is in wrong format.") |
|
54 return link_id |
|
55 |
|
56 |
|
57 def clean_feed_url(self): |
|
58 feed_url = self.cleaned_data.get('feed_url') |
|
59 |
|
60 if feed_url == '': |
|
61 # feed url not supplied (which is OK), so do not try to validate it |
|
62 return None |
|
63 |
|
64 if not validate.isFeedURLValid(feed_url): |
|
65 raise forms.ValidationError('This URL is not a valid ATOM or RSS feed.') |
|
66 |
|
67 return feed_url |