Ejemplo n.º 1
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', 'cat_slug', 'unlocked', 'weight'):
            setattr(
                challenge, field, data.get(field, getattr(challenge, field)))
        if 'answer' in data and data['answer']:
            answer = utils.normalize_input(data['answer'])
            challenge.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
Ejemplo n.º 2
0
 def post(self):
     tag = models.Tag.create(get_field('name'),
                             get_field('description', ''))
     models.commit()
     app.logger.info('Tag %s created by %r.', tag, models.User.current())
     cache.clear()
     return tag
Ejemplo n.º 3
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',
                'cat_slug', 'unlocked', 'weight', 'validator'):
            setattr(
                challenge, field, data.get(field, getattr(challenge, field)))
        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
Ejemplo n.º 4
0
    def delete(self, aid):
        attachment = models.Attachment.query.get_or_404(aid)
        #Probably do not need to delete from disk
        attachment.delete()

        app.logger.info('Attachment %s deleted by %r.', attachment, models.User.current())
        models.commit()
        cache.clear()
Ejemplo n.º 5
0
 def post(self):
     cat = models.Category.create(
         get_field('name'),
         get_field('description', ''))
     models.commit()
     app.logger.info('Category %s created by %r.', cat, models.User.current())
     cache.clear()
     return cat
Ejemplo n.º 6
0
 def post(self):
     cat = models.Category.create(
         get_field('name'),
         get_field('description', ''))
     models.commit()
     app.logger.info('Category %s created by %r.', cat, models.User.current())
     cache.clear()
     return cat
Ejemplo n.º 7
0
 def post(self):
     tag = models.Tag.create(
         get_field('name'),
         get_field('description', ''))
     models.commit()
     app.logger.info('Tag %s created by %r.', tag, models.User.current())
     cache.clear()
     return tag
Ejemplo n.º 8
0
    def delete(self, aid):
        attachment = models.Attachment.query.get_or_404(aid)
        #Probably do not need to delete from disk
        attachment.delete()

        app.logger.info('Attachment %s deleted by %r.', attachment, models.User.current())
        models.commit()
        cache.clear()
Ejemplo n.º 9
0
    def post(self):
        # TODO: refactor, this is messy
        raise NotImplementedError('Restore not implemented.')

        challs = []
        models.commit()
        cache.clear()
        return {'message': '%d Challenges imported.' % (len(challs), )}
Ejemplo n.º 10
0
    def post(self):
        # TODO: refactor, this is messy
        raise NotImplementedError('Restore not implemented.')

        challs = []
        models.commit()
        cache.clear()
        return {'message': '%d Challenges imported.' % (len(challs),)}
Ejemplo n.º 11
0
    def put(self, aid):
        attachment = models.Attachment.query.get_or_404(aid)
        attachment.filename = get_field('filename')
        attachment.set_challenges(get_field('challenges'))

        app.logger.info('Attachment %s updated by %r.', attachment, models.User.current())
        models.commit()
        cache.clear()
        return attachment
Ejemplo n.º 12
0
    def put(self, category_slug):
        category = models.Category.query.get_or_404(category_slug)
        category.name = get_field('name')
        category.description = get_field('description', '')

        app.logger.info('Category %s updated by %r.', category, models.User.current())
        models.commit()
        cache.clear()
        return self.get_challenges(category)
Ejemplo n.º 13
0
    def put(self, tag_slug):
        tag = models.Tag.query.get_or_404(tag_slug)
        tag.name = get_field('name')
        tag.description = get_field('description', tag.description)

        app.logger.info('Tag %s updated by %r', tag, models.User.current())
        models.commit()
        cache.clear()
        return self.get_challenges(tag)
Ejemplo n.º 14
0
 def post(self):
     changed = 0
     for team in models.Team.query.all():
         old = team.score
         team.update_score()
         changed += 1 if team.score != old else 0
     models.commit()
     cache.clear()
     return {'message': ('Recalculated, %d changed.' % changed)}
Ejemplo n.º 15
0
 def post(self):
     fp = flask.request.files['file']
     aid, fpath = attachments.backend.upload(fp)
     attachment = models.Attachment.query.get(aid)
     if not attachment:
         models.Attachment.create(aid, fp.filename, fp.mimetype)
         models.commit()
         cache.clear()
     return dict(aid=aid, fpath=fpath, content_type=fp.mimetype)
Ejemplo n.º 16
0
 def post(self):
     fp = flask.request.files['file']
     aid, fpath = attachments.backend.upload(fp)
     attachment = models.Attachment.query.get(aid)
     if not attachment:
         models.Attachment.create(aid, fp.filename, fp.mimetype)
         models.commit()
         cache.clear()
     return dict(aid=aid, fpath=fpath, content_type=fp.mimetype)
Ejemplo n.º 17
0
    def put(self, tag_slug):
        tag = models.Tag.query.get_or_404(tag_slug)
        tag.name = get_field('name')
        tag.description = get_field('description', tag.description)

        app.logger.info('Tag %s updated by %r', tag, models.User.current())
        models.commit()
        cache.clear()
        return self.get_challenges(tag)
Ejemplo n.º 18
0
    def put(self, aid):
        attachment = models.Attachment.query.get_or_404(aid)
        attachment.filename = get_field('filename')
        attachment.set_challenges(get_field('challenges'))

        app.logger.info('Attachment %s updated by %r.', attachment, models.User.current())
        models.commit()
        cache.clear()
        return attachment
Ejemplo n.º 19
0
    def put(self, category_slug):
        category = models.Category.query.get_or_404(category_slug)
        category.name = get_field('name')
        category.description = get_field('description', category.description)

        app.logger.info('Category %s updated by %r.', category, models.User.current())
        models.commit()
        cache.clear()
        return self.get_challenges(category)
Ejemplo n.º 20
0
 def post(self):
     changed = 0
     for team in models.Team.query.all():
         old = team.score
         team.update_score()
         changed += 1 if team.score != old else 0
     models.commit()
     cache.clear()
     return {'message': ('Recalculated, %d changed.' % changed)}
Ejemplo n.º 21
0
 def delete(self, category_slug):
     category = models.Category.query.get_or_404(category_slug)
     try:
         models.db.session.delete(category)
         cache.clear()
         models.commit()
     except exc.IntegrityError:
         models.db.session.rollback()
         raise errors.ValidationError(
             'Unable to delete category: make sure it is empty')
Ejemplo n.º 22
0
 def post(self):
     app.logger.info('Uploading a new file.')
     fp = flask.request.files['file']
     app.logger.info('Using backend: %r', attachments.backend)
     aid, fpath = attachments.backend.upload(fp)
     app.logger.info('File uploaded to backend, got aid %s', aid)
     attachment = models.Attachment.query.get(aid)
     if not attachment:
         models.Attachment.create(aid, fp.filename, fp.mimetype)
         models.commit()
         cache.clear()
     return dict(aid=aid, fpath=fpath, content_type=fp.mimetype)
Ejemplo n.º 23
0
 def post(self):
     app.logger.info('Uploading a new file.')
     fp = flask.request.files['file']
     app.logger.info('Using backend: %r', attachments.backend)
     aid, fpath = attachments.backend.upload(fp)
     app.logger.info('File uploaded to backend, got aid %s', aid)
     attachment = models.Attachment.query.get(aid)
     if not attachment:
         models.Attachment.create(aid, fp.filename, fp.mimetype)
         models.commit()
         cache.clear()
     return dict(aid=aid, fpath=fpath, content_type=fp.mimetype)
Ejemplo n.º 24
0
    def post(self):
        # TODO: refactor, this is messy
        data = flask.request.get_json()
        categories = data['categories']

        if data.get('replace', False):
            models.Attachment.query.delete()
            models.Hint.query.delete()
            models.Challenge.query.delete()
            models.Category.query.delete()

        cats = {}
        challs = 0
        for catid, cat in categories.iteritems():
            newcat = models.Category()
            for f in ('name', 'description', 'slug'):
                setattr(newcat, f, cat[f])
            models.db.session.add(newcat)
            cats[int(catid)] = newcat

            for challenge in cat['challenges']:
                newchall = models.Challenge()
                for f in ('cid', 'name', 'description', 'points',
                          'answer_hash', 'prerequisite', 'weight'):
                    setattr(newchall, f, challenge.get(f, None))
                newchall.category = newcat
                models.db.session.add(newchall)
                challs += 1
                for h in challenge.get('hints', []):
                    hint = models.Hint()
                    hint.challenge = newchall
                    hint.hint = h['hint']
                    hint.cost = int(h['cost'])
                    models.db.session.add(hint)
                for a in challenge.get('attachments', []):
                    attachment = models.Attachment()
                    attachment.challenge = newchall
                    attachment.aid = a['aid']
                    attachment.filename = a['filename']
                    attachment.content_type = a['content_type']
                    models.db.session.add(attachment)

        models.commit()
        cache.clear()
        return {
            'message':
            '%d Categories and %d Challenges imported.' % (len(cats), challs)
        }
Ejemplo n.º 25
0
    def post(self):
        # TODO: refactor, this is messy
        data = flask.request.get_json()
        categories = data['categories']

        if data.get('replace', False):
            models.Attachment.query.delete()
            models.Hint.query.delete()
            models.Challenge.query.delete()
            models.Category.query.delete()

        cats = {}
        challs = 0
        for catid, cat in categories.iteritems():
            newcat = models.Category()
            for f in ('name', 'description', 'slug'):
                setattr(newcat, f, cat[f])
            models.db.session.add(newcat)
            cats[int(catid)] = newcat

            for challenge in cat['challenges']:
                newchall = models.Challenge()
                for f in ('cid', 'name', 'description', 'points', 'answer_hash',
                          'prerequisite', 'weight'):
                    setattr(newchall, f, challenge.get(f, None))
                newchall.category = newcat
                models.db.session.add(newchall)
                challs += 1
                for h in challenge.get('hints', []):
                    hint = models.Hint()
                    hint.challenge = newchall
                    hint.hint = h['hint']
                    hint.cost = int(h['cost'])
                    models.db.session.add(hint)
                for a in challenge.get('attachments', []):
                    attachment = models.Attachment()
                    attachment.challenge = newchall
                    attachment.aid = a['aid']
                    attachment.filename = a['filename']
                    attachment.content_type = a['content_type']
                    models.db.session.add(attachment)

        models.commit()
        cache.clear()
        return {'message': '%d Categories and %d Challenges imported.' %
                (len(cats), challs)}
Ejemplo n.º 26
0
 def post(self):
     data = flask.request.get_json()
     if data.get('ack') != 'ack':
         raise ValueError('Requires ack!')
     op = data.get('op', '')
     if op == 'scores':
         app.logger.info('Score reset requested by %r.',
                         models.User.current())
         models.ScoreHistory.query.delete()
         models.Answer.query.delete()
         models.NonceFlagUsed.query.delete()
         for team in models.Team.query.all():
             team.score = 0
     elif op == 'players':
         app.logger.info('Player reset requested by %r.',
                         models.User.current())
         models.User.query.filter(
             models.User.admin == False).delete()  # noqa: E712
         models.Team.query.delete()
     else:
         raise ValueError('Unknown operation %s' % op)
     models.commit()
     cache.clear()
     return {'message': 'Done'}
Ejemplo n.º 27
0
 def post(self):
     data = flask.request.get_json()
     if data.get('ack') != 'ack':
         raise ValueError('Requires ack!')
     op = data.get('op', '')
     if op == 'scores':
         app.logger.info('Score reset requested by %r.',
                         models.User.current())
         models.ScoreHistory.query.delete()
         models.Answer.query.delete()
         models.NonceFlagUsed.query.delete()
         for team in models.Team.query.all():
             team.score = 0
     elif op == 'players':
         app.logger.info('Player reset requested by %r.',
                         models.User.current())
         models.User.query.filter(
                 models.User.admin == False).delete()  # noqa: E712
         models.Team.query.delete()
     else:
         raise ValueError('Unknown operation %s' % op)
     models.commit()
     cache.clear()
     return {'message': 'Done'}
Ejemplo n.º 28
0
 def delete(self, category_id):
     category = models.Category.query.get_or_404(category_id)
     models.db.session.delete(category)
     cache.clear()
     models.commit()
Ejemplo n.º 29
0
 def delete(self, category_slug):
     category = models.Category.query.get_or_404(category_slug)
     models.db.session.delete(category)
     cache.clear()
     models.commit()
Ejemplo n.º 30
0
 def delete(self, tag_slug):
     tag = models.Tag.query.get_or_404(tag_slug)
     models.db.session.delete(tag)
     cache.clear()
     models.commit()
Ejemplo n.º 31
0
 def delete(self, tag_slug):
     tag = models.Tag.query.get_or_404(tag_slug)
     models.db.session.delete(tag)
     cache.clear()
     models.commit()
Ejemplo n.º 32
0
 def delete(self, challenge_id):
     challenge = models.Challenge.query.get_or_404(challenge_id)
     models.db.session.delete(challenge)
     models.commit()
     cache.clear()
Ejemplo n.º 33
0
 def delete(self, challenge_id):
     challenge = models.Challenge.query.get_or_404(challenge_id)
     models.db.session.delete(challenge)
     models.commit()
     cache.clear()