Trees | Indices | Help |
---|
|
1 import re 2 import urlparse 3 4 import flask 5 6 from flask.ext import wtf 7 8 from coprs import constants 9 from coprs import helpers 10 from coprs import models 11 from coprs.logic import coprs_logic3515 if not message: 16 message = 'A list of URLs separated by whitespace characters is needed ("{0}" doesn\'t seem to be a URL).' 17 self.message = message1820 urls = field.data.split() 21 for u in urls: 22 if not self.is_url(u): 23 raise wtf.ValidationError(self.message.format(u))24424844 existing = coprs_logic.CoprsLogic.exists_for_user(flask.g.user, field.data).first() 45 46 if existing and str(existing.id) != form.id.data: 47 raise wtf.ValidationError(self.message.format(field.data))6052 if not value: 53 return '' 54 # Replace every whitespace string with one newline 55 # Formats ideally for html form filling, use replace('\n', ' ') 56 # to get space-separated values or split() to get list 57 result = value.strip() 58 regex = re.compile(r'\s+') 59 return regex.sub(lambda x: '\n', result)6663 if value: 64 return helpers.PermissionEnum('request') 65 return helpers.PermissionEnum('nothing')68 @staticmethod103 104 def validate_mock_chroots_not_empty(self): 105 have_any = False 106 for c in self.chroots_list: 107 if getattr(self, c).data: 108 have_any = True 109 return have_any 110 111 F.chroots_list = map(lambda x: x.chroot_name, models.MockChroot.query.filter(models.MockChroot.is_active==True).all()) 112 F.chroots_list.sort() 113 F.chroots_sets = {} # sets of chroots according to how we should print them in columns 114 for ch in F.chroots_list: 115 checkbox_default = False 116 if mock_chroots and ch in map(lambda x:x.chroot_name, mock_chroots): 117 checkbox_default = True 118 setattr(F, ch, wtf.BooleanField(ch, default=checkbox_default)) 119 if ch[0] in F.chroots_sets: 120 F.chroots_sets[ch[0]].append(ch) 121 else: 122 F.chroots_sets[ch[0]] = [ch] 123 124 return F 12570 class F(wtf.Form): 71 # also use id here, to be able to find out whether user is updating a copr 72 # if so, we don't want to shout that name already exists 73 id = wtf.HiddenField() 74 name = wtf.TextField('Name', 75 validators = [wtf.Required(), 76 wtf.Regexp(re.compile(r'^[\w.-]+$'), message='Name must contain only letters, digits, underscores, dashes and dots.'), 77 CoprUniqueNameValidator()]) 78 description = wtf.TextAreaField('Description') 79 instructions = wtf.TextAreaField('Instructions') 80 repos = wtf.TextAreaField('Repos', 81 validators = [UrlListValidator()], 82 filters = [StringListFilter()]) 83 initial_pkgs = wtf.TextAreaField('Initial packages to build', 84 validators = [UrlListValidator()], 85 filters = [StringListFilter()]) 86 87 @property 88 def selected_chroots(self): 89 selected = [] 90 for ch in self.chroots_list: 91 if getattr(self, ch).data: 92 selected.append(ch) 93 return selected94 95 def validate(self): 96 if not super(F, self).validate(): 97 return False 98 99 if not self.validate_mock_chroots_not_empty(): 100 self._mock_chroots_error = 'At least one chroot must be selected' 101 return False 102 return True127 verify = wtf.TextField('Confirm deleting by typing "yes"', 128 validators=[wtf.Required(), wtf.Regexp(r'^yes$', message='Type "yes" - without the quotes, lowercase.')])129131 pkgs = wtf.TextAreaField('Pkgs', 132 validators = [wtf.Required(), UrlListValidator()], 133 filters = [StringListFilter()]) 134 memory_reqs = wtf.IntegerField('Memory requirements', 135 validators = [wtf.NumberRange(min = constants.MIN_BUILD_MEMORY, max = constants.MAX_BUILD_MEMORY)], 136 default = constants.DEFAULT_BUILD_MEMORY) 137 timeout = wtf.IntegerField('Timeout', 138 validators = [wtf.NumberRange(min = constants.MIN_BUILD_TIMEOUT, max = constants.MAX_BUILD_TIMEOUT)], 139 default = constants.DEFAULT_BUILD_TIMEOUT)140142 comment = wtf.TextAreaField('Comment')143145 @staticmethod 149 150 builder_default = False 151 admin_default = False 152 153 if permission: 154 if permission.copr_builder != helpers.PermissionEnum('nothing'): 155 builder_default = True 156 if permission.copr_admin != helpers.PermissionEnum('nothing'): 157 admin_default = True 158 159 setattr(F, 'copr_builder', wtf.BooleanField(default = builder_default, filters = [ValueToPermissionNumberFilter()])) 160 setattr(F, 'copr_admin', wtf.BooleanField(default = admin_default, filters = [ValueToPermissionNumberFilter()])) 161 162 return F163166 """Creates a dynamic form for given set of copr permissions""" 167 @staticmethod 171 172 for perm in permissions: 173 builder_choices = helpers.PermissionEnum.choices_list() 174 admin_choices = helpers.PermissionEnum.choices_list() 175 176 builder_default = perm.copr_builder 177 admin_default = perm.copr_admin 178 179 setattr(F, 'copr_builder_{0}'.format(perm.user.id), wtf.SelectField(choices = builder_choices, default = builder_default, coerce = int)) 180 setattr(F, 'copr_admin_{0}'.format(perm.user.id), wtf.SelectField(choices = admin_choices, default = admin_default, coerce = int)) 181 182 return F183
Trees | Indices | Help |
---|
Generated by Epydoc 3.0.1 on Tue Sep 10 09:30:20 2013 | http://epydoc.sourceforge.net |