2424THE SOFTWARE.
2525"""
2626
27+ import json
2728from sys import intern
2829
2930from django .utils .translation import (
@@ -1124,10 +1125,12 @@ def __init__(self, add_new, *args, **kwargs):
11241125
11251126 if add_new :
11261127 self .helper .add_input (
1127- Submit ("submit" , _ ("Add" )))
1128+ Submit ("submit" , _ ("Add" ), css_class = "btn-success" ))
11281129 else :
11291130 self .helper .add_input (
1130- Submit ("submit" , _ ("Update" )))
1131+ Submit ("submit" , _ ("Update" ), css_class = "btn-success" ))
1132+ self .helper .add_input (
1133+ Submit ("delete" , _ ("Delete" ), css_class = "btn-danger" ))
11311134
11321135 class Meta :
11331136 model = ParticipationTag
@@ -1143,18 +1146,13 @@ def view_participation_tag_list(pctx):
11431146
11441147 return render_course_page (pctx , "course/participation-tag-list.html" , {
11451148 "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" : "%}" ,
11511149 })
11521150
11531151
11541152@course_view
11551153def edit_participation_tag (pctx , ptag_id ):
11561154 # type: (CoursePageContext, int) -> http.HttpResponse
1157- if not pctx .has_permission (pperm .edit_participation ):
1155+ if not pctx .has_permission (pperm .edit_participation_tag ):
11581156 raise PermissionDenied ()
11591157
11601158 request = pctx .request
@@ -1176,13 +1174,23 @@ def edit_participation_tag(pctx, ptag_id):
11761174 form = EditParticipationTagForm (add_new , request .POST , instance = ptag )
11771175 try :
11781176 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." )
1177+ if "submit" in request .POST or "update" in request .POST :
1178+ # Ref: https://stackoverflow.com/q/21458387/3437454
1179+ with transaction .atomic ():
1180+ form .save ()
1181+
1182+ if "submit" in request .POST :
1183+ assert add_new
1184+ msg = _ ("New participation tag saved." )
1185+ else :
1186+ msg = _ ("Changes saved." )
1187+ elif "delete" in request .POST :
1188+ ptag .delete ()
1189+ msg = (_ ("successfully deleted participation tag '%(tag)s'." )
1190+ % {"tag" : ptag .name })
11841191 else :
1185- msg = _ ("Changes saved." )
1192+ raise SuspiciousOperation (_ ("invalid operation" ))
1193+
11861194 messages .add_message (request , messages .SUCCESS , msg )
11871195 return redirect (
11881196 "relate-view_participation_tags" , pctx .course .identifier )
@@ -1199,48 +1207,6 @@ def edit_participation_tag(pctx, ptag_id):
11991207 "form" : form ,
12001208 })
12011209
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-
12441210# }}}
12451211
12461212
@@ -1253,10 +1219,12 @@ def __init__(self, add_new, *args, **kwargs):
12531219
12541220 if add_new :
12551221 self .helper .add_input (
1256- Submit ("submit" , _ ("Add" )))
1222+ Submit ("submit" , _ ("Add" ), css_class = "btn-success" ))
12571223 else :
12581224 self .helper .add_input (
1259- Submit ("submit" , _ ("Update" )))
1225+ Submit ("submit" , _ ("Update" ), css_class = "btn-success" ))
1226+ self .helper .add_input (
1227+ Submit ("delete" , _ ("Delete" ), css_class = "btn-danger" ))
12601228
12611229 class Meta :
12621230 model = ParticipationRole
@@ -1272,18 +1240,13 @@ def view_participation_role_list(pctx):
12721240
12731241 return render_course_page (pctx , "course/participation-role-list.html" , {
12741242 "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" : "%}" ,
12801243 })
12811244
12821245
12831246@course_view
12841247def edit_participation_role (pctx , prole_id ):
12851248 # type: (CoursePageContext, int) -> http.HttpResponse
1286- if not pctx .has_permission (pperm .edit_participation ):
1249+ if not pctx .has_permission (pperm .edit_participation_role ):
12871250 raise PermissionDenied ()
12881251
12891252 request = pctx .request
@@ -1305,14 +1268,24 @@ def edit_participation_role(pctx, prole_id):
13051268 form = EditParticipationRoleForm (add_new , request .POST , instance = prole )
13061269 try :
13071270 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." )
1271+ if "submit" in request .POST or "update" in request .POST :
1272+ # Ref: https://stackoverflow.com/q/21458387/3437454
1273+ with transaction .atomic ():
1274+ form .save ()
1275+
1276+ if "submit" in request .POST :
1277+ assert add_new
1278+ msg = _ ("New participation role saved." )
1279+ else :
1280+ msg = _ ("Changes saved." )
1281+ elif "delete" in request .POST :
1282+ prole .delete ()
1283+ msg = (
1284+ _ ("successfully deleted participation role '%(role)s'." )
1285+ % {"role" : prole .identifier })
13141286 else :
1315- msg = _ ("Changes saved." )
1287+ raise SuspiciousOperation (_ ("invalid operation" ))
1288+
13161289 messages .add_message (request , messages .SUCCESS , msg )
13171290 return redirect (
13181291 "relate-view_participation_roles" , pctx .course .identifier )
@@ -1329,48 +1302,6 @@ def edit_participation_role(pctx, prole_id):
13291302 "form" : form ,
13301303 })
13311304
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-
13741305# }}}
13751306
13761307
0 commit comments