admin: new route to try a flag

This commit is contained in:
nemunaire 2018-12-08 03:13:16 +01:00
parent d89cd2f0ca
commit 5dbf60eaa2
3 changed files with 45 additions and 2 deletions

View File

@ -35,6 +35,7 @@ func init() {
router.POST("/api/exercices/:eid/flags", apiHandler(exerciceHandler(createExerciceFlag)))
router.GET("/api/exercices/:eid/flags/:kid", apiHandler(flagHandler(showExerciceFlag)))
router.PUT("/api/exercices/:eid/flags/:kid", apiHandler(flagHandler(updateExerciceFlag)))
router.POST("/api/exercices/:eid/flags/:kid/try", apiHandler(flagHandler(tryExerciceFlag)))
router.DELETE("/api/exercices/:eid/flags/:kid", apiHandler(flagHandler(deleteExerciceFlag)))
router.GET("/api/exercices/:eid/flags/:kid/choices/", apiHandler(flagHandler(listFlagChoices)))
router.GET("/api/exercices/:eid/flags/:kid/choices/:cid", apiHandler(choiceHandler(showFlagChoice)))
@ -300,6 +301,23 @@ func showExerciceFlag(flag fic.Flag, _ fic.Exercice, body []byte) (interface{},
return flag, nil
}
func tryExerciceFlag(flag fic.Flag, _ fic.Exercice, body []byte) (interface{}, error) {
var uk uploadedFlag
if err := json.Unmarshal(body, &uk); err != nil {
return nil, err
}
if len(uk.Flag) == 0 {
return nil, errors.New("Empty submission")
}
if flag.Check([]byte(uk.Flag)) {
return true, nil
} else {
return nil, errors.New("Bad submission")
}
}
func updateExerciceFlag(flag fic.Flag, exercice fic.Exercice, body []byte) (interface{}, error) {
var uk uploadedFlag
if err := json.Unmarshal(body, &uk); err != nil {

View File

@ -1210,6 +1210,21 @@ angular.module("FICApp")
}
$rootScope.staticFilesNeedUpdate++;
}
$scope.testFlag = function(flag) {
if (flag.test_str) {
$http({
url: "/api/exercices/" + $routeParams.exerciceId + "/flags/" + flag.id + "/try",
data: {"flag": flag.test_str},
method: "POST"
}).then(function(response) {
flag.test_str = "";
$rootScope.newBox('success', "Flag Ok !");
}, function(response) {
flag.test_str = "";
$rootScope.newBox('danger', 'An error occurs: ', response.data.errmsg);
});
}
}
$scope.inSync = false;
$scope.syncFlags = function() {
$scope.inSync = true;

View File

@ -105,7 +105,8 @@
<button type="button" ng-click="addFlag()" class="btn btn-sm btn-primary"><span class="glyphicon glyphicon-plus" aria-hidden="true"></span> Ajouter</button>
</div>
</div>
<form ng-submit="saveFlag()" class="list-group-item form-horizontal bg-light text-dark" ng-repeat="flag in flags">
<div ng-repeat="flag in flags" class="list-group-item bg-light text-dark">
<form ng-submit="saveFlag()" class="form-horizontal">
<div class="row" id="flag-{{flag.id}}">
<input type="text" id="klabel{{flag.id}}" ng-model="flag.label" class="col form-control form-control-sm" placeholder="Intitulé" title="Intitulé">
<div class="col-auto" ng-show="flag.id">
@ -129,7 +130,16 @@
<div class="row">
<input type="text" id="kvre{{flag.id}}" ng-model="flag.validator_regexp" class="col form-control form-control-sm" placeholder="Regexp selecting validation string" title="Regexp selecting validation string">
</div>
</form>
</form>
<form ng-submit="testFlag(flag)">
<div class="input-group">
<input type="text" id="ktest{{flag.id}}" ng-model="flag.test_str" class="col form-control form-control-sm" placeholder="Test the flag">
<div class="input-group-append">
<button class="btn btn-sm btn-warning" type="submit"><span class="glyphicon glyphicon-play" aria-hidden="true"></span></button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>