Beispiel #1
0
    def post(self):
        data = flask.request.get_json()
        unlocked = data.get('unlocked', False)
        answer = utils.normalize_input(data['answer'])
        if not validators.IsValidator(data.get('validator', None)):
            raise errors.ValidationError('Invalid validator.')
        chall = models.Challenge.create(
            data['name'], data['description'], data['points'], '', unlocked,
            data.get('validator', validators.GetDefaultValidator()))
        validator = validators.GetValidatorForChallenge(chall)
        validator.change_answer(answer)
        if 'attachments' in data:
            chall.set_attachments(data['attachments'])
        if 'prerequisite' in data:
            chall.set_prerequisite(data['prerequisite'])
        if 'tags' in data:
            chall.set_tags(data['tags'])

        if unlocked and utils.GameTime.open():
            news = 'New challenge created: "%s"' % chall.name
            models.News.game_broadcast(message=news)

        models.commit()
        app.logger.info('Challenge %s created by %r.', chall,
                        models.User.current())
        return chall
Beispiel #2
0
    def put(self, challenge_id):
        challenge = models.Challenge.query.get_or_404(challenge_id)
        data = flask.request.get_json()
        old_unlocked = challenge.unlocked
        for field in ('name', 'description', 'points', 'unlocked', 'weight'):
            setattr(challenge, field, data.get(field,
                                               getattr(challenge, field)))
        if 'validator' in data:
            if not validators.IsValidator(data['validator']):
                raise errors.ValidationError('Invalid validator.')
            challenge.validator = data['validator']
        if 'answer' in data and data['answer']:
            answer = utils.normalize_input(data['answer'])
            validator = validators.GetValidatorForChallenge(challenge)
            validator.change_answer(answer)
        if 'attachments' in data:
            challenge.set_attachments(data['attachments'])
        if 'prerequisite' in data:
            challenge.set_prerequisite(data['prerequisite'])
        else:
            challenge.prerequisite = ''
        if 'tags' in data:
            challenge.set_tags(data['tags'])
        if challenge.unlocked and not old_unlocked:
            news = 'Challenge "%s" unlocked!' % challenge.name
            models.News.game_broadcast(message=news)

        app.logger.info('Challenge %s updated by %r.', challenge,
                        models.User.current())

        models.commit()
        cache.clear()
        return challenge