@@ -1124,10 +1124,12 @@ def __init__(self, add_new, *args, **kwargs):
11241124
11251125 if add_new :
11261126 self .helper .add_input (
1127- Submit ("submit" , _ ("Add" )))
1127+ Submit ("submit" , _ ("Add" ), css_class = "btn-success" ))
11281128 else :
11291129 self .helper .add_input (
1130- Submit ("submit" , _ ("Update" )))
1130+ Submit ("submit" , _ ("Update" ), css_class = "btn-success" ))
1131+ self .helper .add_input (
1132+ Submit ("delete" , _ ("Delete" ), css_class = "btn-danger" ))
11311133
11321134 class Meta :
11331135 model = ParticipationTag
@@ -1143,18 +1145,13 @@ def view_participation_tag_list(pctx):
11431145
11441146 return render_course_page (pctx , "course/participation-tag-list.html" , {
11451147 "participation_tags" : participation_tags ,
1146-
1147- # Wrappers used by JavaScript template (tmpl) so as not to
1148- # conflict with Django template's tag wrapper
1149- "JQ_OPEN" : "{%" ,
1150- "JQ_CLOSE" : "%}" ,
11511148 })
11521149
11531150
11541151@course_view
11551152def edit_participation_tag (pctx , ptag_id ):
11561153 # type: (CoursePageContext, int) -> http.HttpResponse
1157- if not pctx .has_permission (pperm .edit_participation ):
1154+ if not pctx .has_permission (pperm .edit_participation_tag ):
11581155 raise PermissionDenied ()
11591156
11601157 request = pctx .request
@@ -1176,13 +1173,23 @@ def edit_participation_tag(pctx, ptag_id):
11761173 form = EditParticipationTagForm (add_new , request .POST , instance = ptag )
11771174 try :
11781175 if form .is_valid ():
1179- # Ref: https://stackoverflow.com/q/21458387/3437454
1180- with transaction .atomic ():
1181- form .save ()
1182- if add_new :
1183- msg = _ ("New participation tag saved." )
1176+ if "submit" in request .POST or "update" in request .POST :
1177+ # Ref: https://stackoverflow.com/q/21458387/3437454
1178+ with transaction .atomic ():
1179+ form .save ()
1180+
1181+ if "submit" in request .POST :
1182+ assert add_new
1183+ msg = _ ("New participation tag saved." )
1184+ else :
1185+ msg = _ ("Changes saved." )
1186+ elif "delete" in request .POST :
1187+ ptag .delete ()
1188+ msg = (_ ("successfully deleted participation tag '%(tag)s'." )
1189+ % {"tag" : ptag .name })
11841190 else :
1185- msg = _ ("Changes saved." )
1191+ raise SuspiciousOperation (_ ("invalid operation" ))
1192+
11861193 messages .add_message (request , messages .SUCCESS , msg )
11871194 return redirect (
11881195 "relate-view_participation_tags" , pctx .course .identifier )
@@ -1199,48 +1206,6 @@ def edit_participation_tag(pctx, ptag_id):
11991206 "form" : form ,
12001207 })
12011208
1202-
1203- @course_view
1204- def delete_participation_tag (pctx , ptag_id ):
1205- # type: (CoursePageContext, int) -> http.HttpResponse
1206-
1207- if not pctx .has_permission (pperm .edit_participation ):
1208- raise PermissionDenied ()
1209-
1210- request = pctx .request
1211-
1212- if not request .is_ajax () or request .method != "POST" :
1213- raise PermissionDenied (_ ("only AJAX POST is allowed" ))
1214-
1215- num_ptag_id = int (ptag_id )
1216-
1217- ptag = get_object_or_404 (ParticipationTag , id = num_ptag_id )
1218-
1219- if ptag .course .id != pctx .course .id :
1220- raise SuspiciousOperation (
1221- "may not delete participation tag in different course" )
1222-
1223- if "delete" in request .POST :
1224- try :
1225- ptag .delete ()
1226- except Exception as e :
1227- return http .JsonResponse (
1228- {"error" : _ (
1229- "Error when deleting participation tag '%(tag)s'."
1230- " %(error_type)s: %(error)s." ) % {
1231- "tag" : ptag .name ,
1232- "error_type" : type (e ).__name__ ,
1233- "error" : str (e )}},
1234- status = 400 )
1235- else :
1236- return http .JsonResponse (
1237- {"message" : _ ("successfully deleted participation tag '%(tag)s'." )
1238- % {"tag" : ptag .name },
1239- "message_level" : messages .DEFAULT_TAGS [messages .SUCCESS ]})
1240-
1241- else :
1242- raise SuspiciousOperation (_ ("invalid operation" ))
1243-
12441209# }}}
12451210
12461211
@@ -1253,10 +1218,12 @@ def __init__(self, add_new, *args, **kwargs):
12531218
12541219 if add_new :
12551220 self .helper .add_input (
1256- Submit ("submit" , _ ("Add" )))
1221+ Submit ("submit" , _ ("Add" ), css_class = "btn-success" ))
12571222 else :
12581223 self .helper .add_input (
1259- Submit ("submit" , _ ("Update" )))
1224+ Submit ("submit" , _ ("Update" ), css_class = "btn-success" ))
1225+ self .helper .add_input (
1226+ Submit ("delete" , _ ("Delete" ), css_class = "btn-danger" ))
12601227
12611228 class Meta :
12621229 model = ParticipationRole
@@ -1272,18 +1239,13 @@ def view_participation_role_list(pctx):
12721239
12731240 return render_course_page (pctx , "course/participation-role-list.html" , {
12741241 "participation_roles" : participation_roles ,
1275-
1276- # Wrappers used by JavaScript template (tmpl) so as not to
1277- # conflict with Django template's tag wrapper
1278- "JQ_OPEN" : "{%" ,
1279- "JQ_CLOSE" : "%}" ,
12801242 })
12811243
12821244
12831245@course_view
12841246def edit_participation_role (pctx , prole_id ):
12851247 # type: (CoursePageContext, int) -> http.HttpResponse
1286- if not pctx .has_permission (pperm .edit_participation ):
1248+ if not pctx .has_permission (pperm .edit_participation_role ):
12871249 raise PermissionDenied ()
12881250
12891251 request = pctx .request
@@ -1305,14 +1267,24 @@ def edit_participation_role(pctx, prole_id):
13051267 form = EditParticipationRoleForm (add_new , request .POST , instance = prole )
13061268 try :
13071269 if form .is_valid ():
1308- # Ref: https://stackoverflow.com/q/21458387/3437454
1309- with transaction .atomic ():
1310- form .save ()
1311-
1312- if add_new :
1313- msg = _ ("New participation role saved." )
1270+ if "submit" in request .POST or "update" in request .POST :
1271+ # Ref: https://stackoverflow.com/q/21458387/3437454
1272+ with transaction .atomic ():
1273+ form .save ()
1274+
1275+ if "submit" in request .POST :
1276+ assert add_new
1277+ msg = _ ("New participation role saved." )
1278+ else :
1279+ msg = _ ("Changes saved." )
1280+ elif "delete" in request .POST :
1281+ prole .delete ()
1282+ msg = (
1283+ _ ("successfully deleted participation role '%(role)s'." )
1284+ % {"role" : prole .identifier })
13141285 else :
1315- msg = _ ("Changes saved." )
1286+ raise SuspiciousOperation (_ ("invalid operation" ))
1287+
13161288 messages .add_message (request , messages .SUCCESS , msg )
13171289 return redirect (
13181290 "relate-view_participation_roles" , pctx .course .identifier )
@@ -1329,48 +1301,6 @@ def edit_participation_role(pctx, prole_id):
13291301 "form" : form ,
13301302 })
13311303
1332-
1333- @course_view
1334- def delete_participation_role (pctx , prole_id ):
1335- # type: (CoursePageContext, int) -> http.HttpResponse
1336-
1337- if not pctx .has_permission (pperm .edit_participation ):
1338- raise PermissionDenied ()
1339-
1340- request = pctx .request
1341-
1342- if not request .is_ajax () or request .method != "POST" :
1343- raise PermissionDenied (_ ("only AJAX POST is allowed" ))
1344-
1345- num_prole_id = int (prole_id )
1346-
1347- prole = get_object_or_404 (ParticipationRole , id = num_prole_id )
1348-
1349- if prole .course .id != pctx .course .id :
1350- raise SuspiciousOperation (
1351- "may not delete participation role in different course" )
1352-
1353- if "delete" in request .POST :
1354- try :
1355- prole .delete ()
1356- except Exception as e :
1357- return http .JsonResponse (
1358- {"error" : _ (
1359- "Error when deleting participation role '%(role)s'."
1360- " %(error_type)s: %(error)s." ) % {
1361- "role" : prole .identifier ,
1362- "error_type" : type (e ).__name__ ,
1363- "error" : str (e )}},
1364- status = 400 )
1365- else :
1366- return http .JsonResponse (
1367- {"message" : _ ("successfully deleted participation role '%(role)s'." )
1368- % {"role" : prole .identifier },
1369- "message_level" : messages .DEFAULT_TAGS [messages .SUCCESS ]})
1370-
1371- else :
1372- raise SuspiciousOperation (_ ("invalid operation" ))
1373-
13741304# }}}
13751305
13761306
0 commit comments