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
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
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
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()
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
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
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
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()
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), )}
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),)}
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
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)
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)
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)}
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)
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)
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)
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
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)
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)}
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')
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)
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)
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) }
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)}
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'}
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'}
def delete(self, category_id): category = models.Category.query.get_or_404(category_id) models.db.session.delete(category) cache.clear() models.commit()
def delete(self, category_slug): category = models.Category.query.get_or_404(category_slug) models.db.session.delete(category) cache.clear() models.commit()
def delete(self, tag_slug): tag = models.Tag.query.get_or_404(tag_slug) models.db.session.delete(tag) cache.clear() models.commit()
def delete(self, tag_slug): tag = models.Tag.query.get_or_404(tag_slug) models.db.session.delete(tag) cache.clear() models.commit()
def delete(self, challenge_id): challenge = models.Challenge.query.get_or_404(challenge_id) models.db.session.delete(challenge) models.commit() cache.clear()
def delete(self, challenge_id): challenge = models.Challenge.query.get_or_404(challenge_id) models.db.session.delete(challenge) models.commit() cache.clear()