forms.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. # -*- coding: utf-8 -*-
  2. """
  3. (c) 2014 - Copyright Red Hat Inc
  4. Authors:
  5. Pierre-Yves Chibon <pingou@pingoured.fr>
  6. """
  7. import re
  8. from flask.ext import wtf
  9. import wtforms
  10. # pylint: disable=R0903,W0232,E1002
  11. STRICT_REGEX = '^[a-zA-Z0-9-_]+$'
  12. TAGS_REGEX = '^[a-zA-Z0-9-_, .]+$'
  13. PROJECT_NAME_REGEX = \
  14. '^[a-zA-z0-9_][a-zA-Z0-9-_]*(/?[a-zA-z0-9_][a-zA-Z0-9-_]+)?$'
  15. class ProjectFormSimplified(wtf.Form):
  16. ''' Form to edit the description of a project. '''
  17. description = wtforms.TextField(
  18. 'Description <span class="error">*</span>',
  19. [wtforms.validators.Required()]
  20. )
  21. url = wtforms.TextField(
  22. 'URL',
  23. [wtforms.validators.optional()]
  24. )
  25. avatar_email = wtforms.TextField(
  26. 'Avatar email',
  27. [wtforms.validators.optional()]
  28. )
  29. tags = wtforms.TextField(
  30. 'Project tags',
  31. [wtforms.validators.optional()]
  32. )
  33. class ProjectForm(ProjectFormSimplified):
  34. ''' Form to create or edit project. '''
  35. name = wtforms.TextField(
  36. 'Project name <span class="error">*</span>',
  37. [
  38. wtforms.validators.Required(),
  39. wtforms.validators.Regexp(PROJECT_NAME_REGEX, flags=re.IGNORECASE)
  40. ]
  41. )
  42. create_readme = wtforms.BooleanField(
  43. 'Create README',
  44. [wtforms.validators.optional()],
  45. )
  46. class IssueFormSimplied(wtf.Form):
  47. ''' Form to create or edit an issue. '''
  48. title = wtforms.TextField(
  49. 'Title<span class="error">*</span>',
  50. [wtforms.validators.Required()]
  51. )
  52. issue_content = wtforms.TextAreaField(
  53. 'Content<span class="error">*</span>',
  54. [wtforms.validators.Required()]
  55. )
  56. private = wtforms.BooleanField(
  57. 'Private',
  58. [wtforms.validators.optional()],
  59. )
  60. class IssueForm(IssueFormSimplied):
  61. ''' Form to create or edit an issue. '''
  62. status = wtforms.SelectField(
  63. 'Status',
  64. [wtforms.validators.Required()],
  65. choices=[]
  66. )
  67. def __init__(self, *args, **kwargs):
  68. """ Calls the default constructor with the normal argument but
  69. uses the list of collection provided to fill the choices of the
  70. drop-down list.
  71. """
  72. super(IssueForm, self).__init__(*args, **kwargs)
  73. if 'status' in kwargs:
  74. self.status.choices = [
  75. (status, status) for status in kwargs['status']
  76. ]
  77. class RequestPullForm(wtf.Form):
  78. ''' Form to create a request pull. '''
  79. title = wtforms.TextField(
  80. 'Title<span class="error">*</span>',
  81. [wtforms.validators.Required()]
  82. )
  83. initial_comment = wtforms.TextAreaField(
  84. 'Initial Comment', [wtforms.validators.Optional()])
  85. class RemoteRequestPullForm(RequestPullForm):
  86. ''' Form to create a remote request pull. '''
  87. git_repo = wtforms.TextField(
  88. 'Git repo address<span class="error">*</span>',
  89. [wtforms.validators.Required()]
  90. )
  91. branch_from = wtforms.TextField(
  92. 'Git branch<span class="error">*</span>',
  93. [wtforms.validators.Required()]
  94. )
  95. branch_to = wtforms.TextField(
  96. 'Git branch to merge in<span class="error">*</span>',
  97. [wtforms.validators.Required()]
  98. )
  99. class AddIssueTagForm(wtf.Form):
  100. ''' Form to add a comment to an issue. '''
  101. tag = wtforms.TextField(
  102. 'tag',
  103. [
  104. wtforms.validators.Optional(),
  105. wtforms.validators.Regexp(TAGS_REGEX, flags=re.IGNORECASE)
  106. ]
  107. )
  108. class StatusForm(wtf.Form):
  109. ''' Form to add/change the status of an issue. '''
  110. status = wtforms.SelectField(
  111. 'Status',
  112. [wtforms.validators.Required()],
  113. choices=[]
  114. )
  115. def __init__(self, *args, **kwargs):
  116. """ Calls the default constructor with the normal argument but
  117. uses the list of collection provided to fill the choices of the
  118. drop-down list.
  119. """
  120. super(StatusForm, self).__init__(*args, **kwargs)
  121. if 'status' in kwargs:
  122. self.status.choices = [
  123. (status, status) for status in kwargs['status']
  124. ]
  125. class NewTokenForm(wtf.Form):
  126. ''' Form to add/change the status of an issue. '''
  127. acls = wtforms.SelectMultipleField(
  128. 'ACLs',
  129. [wtforms.validators.Required()],
  130. choices=[]
  131. )
  132. def __init__(self, *args, **kwargs):
  133. """ Calls the default constructor with the normal argument but
  134. uses the list of collection provided to fill the choices of the
  135. drop-down list.
  136. """
  137. super(NewTokenForm, self).__init__(*args, **kwargs)
  138. if 'acls' in kwargs:
  139. self.acls.choices = [
  140. (acl.name, acl.name) for acl in kwargs['acls']
  141. ]
  142. class UpdateIssueForm(wtf.Form):
  143. ''' Form to add a comment to an issue. '''
  144. tag = wtforms.TextField(
  145. 'tag',
  146. [
  147. wtforms.validators.Optional(),
  148. wtforms.validators.Regexp(TAGS_REGEX, flags=re.IGNORECASE)
  149. ]
  150. )
  151. depends = wtforms.TextField(
  152. 'dependency issue', [wtforms.validators.Optional()]
  153. )
  154. blocks = wtforms.TextField(
  155. 'blocked issue', [wtforms.validators.Optional()]
  156. )
  157. comment = wtforms.TextAreaField(
  158. 'Comment', [wtforms.validators.Optional()]
  159. )
  160. assignee = wtforms.TextAreaField(
  161. 'Assigned to', [wtforms.validators.Optional()]
  162. )
  163. status = wtforms.SelectField(
  164. 'Status',
  165. [wtforms.validators.Optional()],
  166. choices=[]
  167. )
  168. priority = wtforms.SelectField(
  169. 'Priority',
  170. [wtforms.validators.Optional()],
  171. choices=[]
  172. )
  173. def __init__(self, *args, **kwargs):
  174. """ Calls the default constructor with the normal argument but
  175. uses the list of collection provided to fill the choices of the
  176. drop-down list.
  177. """
  178. super(UpdateIssueForm, self).__init__(*args, **kwargs)
  179. if 'status' in kwargs:
  180. self.status.choices = [
  181. (status, status) for status in kwargs['status']
  182. ]
  183. self.priority.choices = []
  184. if 'priorities' in kwargs:
  185. for key in sorted(kwargs['priorities']):
  186. self.priority.choices.append(
  187. (key, kwargs['priorities'][key])
  188. )
  189. class AddPullRequestCommentForm(wtf.Form):
  190. ''' Form to add a comment to a pull-request. '''
  191. commit = wtforms.HiddenField('commit identifier')
  192. filename = wtforms.HiddenField('file changed')
  193. row = wtforms.HiddenField('row')
  194. requestid = wtforms.HiddenField('requestid')
  195. tree_id = wtforms.HiddenField('treeid')
  196. comment = wtforms.TextAreaField(
  197. 'Comment<span class="error">*</span>',
  198. [wtforms.validators.Required()]
  199. )
  200. class AddPullRequestFlagForm(wtf.Form):
  201. ''' Form to add a flag to a pull-request. '''
  202. username = wtforms.TextField(
  203. 'Username', [wtforms.validators.Required()])
  204. percent = wtforms.TextField(
  205. 'Percentage of completion', [wtforms.validators.Required()])
  206. comment = wtforms.TextAreaField(
  207. 'Comment', [wtforms.validators.Required()])
  208. url = wtforms.TextField(
  209. 'URL', [wtforms.validators.Required()])
  210. uid = wtforms.TextField(
  211. 'UID', [wtforms.validators.optional()])
  212. class UserSettingsForm(wtf.Form):
  213. ''' Form to create or edit project. '''
  214. ssh_key = wtforms.TextAreaField(
  215. 'Public SSH key <span class="error">*</span>',
  216. [wtforms.validators.Required()]
  217. )
  218. class AddUserForm(wtf.Form):
  219. ''' Form to add a user to a project. '''
  220. user = wtforms.TextField(
  221. 'Username <span class="error">*</span>',
  222. [wtforms.validators.Required()]
  223. )
  224. class AssignIssueForm(wtf.Form):
  225. ''' Form to assign an user to an issue. '''
  226. assignee = wtforms.TextField(
  227. 'Assignee <span class="error">*</span>',
  228. [wtforms.validators.Required()]
  229. )
  230. class AddGroupForm(wtf.Form):
  231. ''' Form to add a group to a project. '''
  232. group = wtforms.TextField(
  233. 'Group <span class="error">*</span>',
  234. [
  235. wtforms.validators.Required(),
  236. wtforms.validators.Regexp(STRICT_REGEX, flags=re.IGNORECASE)
  237. ]
  238. )
  239. class ConfirmationForm(wtf.Form):
  240. ''' Simple form used just for CSRF protection. '''
  241. pass
  242. class UploadFileForm(wtf.Form):
  243. ''' Form to upload a file. '''
  244. filestream = wtforms.FileField(
  245. 'File',
  246. [wtforms.validators.Required()])
  247. class UserEmailForm(wtf.Form):
  248. ''' Form to edit the description of a project. '''
  249. email = wtforms.TextField(
  250. 'email', [wtforms.validators.Required()]
  251. )
  252. def __init__(self, *args, **kwargs):
  253. super(UserEmailForm, self).__init__(*args, **kwargs)
  254. if 'emails' in kwargs:
  255. if kwargs['emails']:
  256. self.email.validators.append(
  257. wtforms.validators.NoneOf(kwargs['emails'])
  258. )
  259. else:
  260. self.email.validators = [wtforms.validators.Required()]
  261. class ProjectCommentForm(wtf.Form):
  262. ''' Form to represent project. '''
  263. objid = wtforms.TextField(
  264. 'Ticket/Request id',
  265. [wtforms.validators.Required()]
  266. )
  267. useremail = wtforms.TextField(
  268. 'Email',
  269. [wtforms.validators.Required()]
  270. )
  271. class CommentForm(wtf.Form):
  272. ''' Form to upload a file. '''
  273. comment = wtforms.FileField(
  274. 'Comment',
  275. [wtforms.validators.Required()])
  276. class NewGroupForm(wtf.Form):
  277. """ Form to ask for a password change. """
  278. group_name = wtforms.TextField(
  279. 'Group name <span class="error">*</span>',
  280. [
  281. wtforms.validators.Required(),
  282. wtforms.validators.Length(max=16),
  283. wtforms.validators.Regexp(STRICT_REGEX, flags=re.IGNORECASE)
  284. ]
  285. )
  286. group_type = wtforms.SelectField(
  287. 'Group type',
  288. [wtforms.validators.Required()],
  289. choices=[]
  290. )
  291. def __init__(self, *args, **kwargs):
  292. """ Calls the default constructor with the normal argument but
  293. uses the list of collection provided to fill the choices of the
  294. drop-down list.
  295. """
  296. super(NewGroupForm, self).__init__(*args, **kwargs)
  297. if 'group_types' in kwargs:
  298. self.group_type.choices = [
  299. (grptype, grptype) for grptype in kwargs['group_types']
  300. ]
  301. class EditFileForm(wtf.Form):
  302. """ Form used to edit a file. """
  303. content = wtforms.TextAreaField(
  304. 'content', [wtforms.validators.Required()])
  305. commit_title = wtforms.TextField(
  306. 'Title', [wtforms.validators.Required()])
  307. commit_message = wtforms.TextAreaField(
  308. 'Commit message', [wtforms.validators.optional()])
  309. email = wtforms.SelectField(
  310. 'Email', [wtforms.validators.Required()],
  311. choices=[]
  312. )
  313. branch = wtforms.TextField(
  314. 'Branch', [wtforms.validators.Required()])
  315. def __init__(self, *args, **kwargs):
  316. """ Calls the default constructor with the normal argument but
  317. uses the list of collection provided to fill the choices of the
  318. drop-down list.
  319. """
  320. super(EditFileForm, self).__init__(*args, **kwargs)
  321. if 'emails' in kwargs:
  322. self.email.choices = [
  323. (email.email, email.email) for email in kwargs['emails']
  324. ]
  325. class DefaultBranchForm(wtf.Form):
  326. """Form to change the default branh for a repository"""
  327. branches = wtforms.SelectField(
  328. 'default_branch',
  329. [wtforms.validators.Required()],
  330. choices=[]
  331. )
  332. def __init__(self, *args, **kwargs):
  333. """ Calls the default constructor with the normal argument but
  334. uses the list of collection provided to fill the choices of the
  335. drop-down list.
  336. """
  337. super(DefaultBranchForm, self).__init__(*args, **kwargs)
  338. if 'branches' in kwargs:
  339. self.branches.choices = [
  340. (branch, branch) for branch in kwargs['branches']
  341. ]
  342. class EditCommentForm(wtf.Form):
  343. """ Form to verify that comment is not empty
  344. """
  345. update_comment = wtforms.TextAreaField(
  346. 'Comment<span class="error">*</span>',
  347. [wtforms.validators.Required()]
  348. )
  349. class ForkRepoForm(wtf.Form):
  350. ''' Form to fork a project in the API. '''
  351. repo = wtforms.TextField(
  352. 'The project name <span class="error">*</span>',
  353. [wtforms.validators.Required()]
  354. )
  355. username = wtforms.TextField(
  356. 'User who forked the project',
  357. [wtforms.validators.optional()])