def get_matches():
    max_per_page = 50
    page = request.args.get('page', 1, type=int)
    per_page = request.args.get('perPage', 20, type=int)
    q = db.session.query(Match)
    search_term = request.args.get('searchTerm', None, str)
    if search_term:
        q = q.filter((Match.name.ilike('%{}%'.format(search_term)))
                     | (Match.game.name.ilike(
                         '%{}%'.format(search_term))))  # Filter by term
    # Retrieve logged user and filter by private matches and by user owned matches
    logged_user = auth.get_logged_user()
    if logged_user:
        q = q.filter((Match.is_private == False)
                     | (Match.creator_id == logged_user.id))
    else:
        q = q.filter((Match.is_private == False))
    pagination = q.paginate(page, per_page, max_per_page)  # Paginate result
    return_matches = []
    for match in pagination.items:
        return_matches.append(match.short())
    return_data = {
        'matches': return_matches,
        'total_matches': pagination.total,
        'page': pagination.page,
        'pages': pagination.pages,
    }
    return jsonify(return_data)
def delete_tournament(tournament_id):
    tournament = db.session.query(Tournament).filter(Tournament.id == tournament_id).first()
    if not tournament:
        return errors.not_found_error('Tournament not found')
    logged_user = auth.get_logged_user()
    if tournament.creator_id == logged_user.id:
        tournament.delete()
        return '', 204
    errors.forbidden_error('You can\'t delete a not your tournament')
def delete_match(match_id):
    match = db.session.query(Match).filter(Match.id == match_id).first()
    if not match:
        return errors.not_found_error('Match not found')
    logged_user = auth.get_logged_user()
    if match.creator_id != logged_user.id:
        return errors.forbidden_error('You can\'t delete a not your own match')
    match.delete()
    return '', 204
def create_match(payload):
    data = request.json

    # Prepare game
    game = Game.query.filter(Game.id == data['gameId']).first()
    if not game:
        game = Game.query.filter(Game.name.ilike(
            data['gameName'].strip())).first()
    if not game:
        game = Game(name=data['gameName'])
        db.session.add(game)

    match = Match(name=data)
    uuid_len = 6
    tries = 0
    max_tries = 10
    match_found = True
    uuid = ''
    while (match_found):
        if tries >= max_tries:
            tries = 0
            uuid_len += 1
        tries += 1
        uuid = generate_uuid(uuid_len)
        match_found = db.session.query(Match).filter(
            Match.uuid == uuid).first()
    match.uuid = uuid
    match.name = data['name']
    match.max_participants = data[
        'maxParticipants'] if 'maxParticipants' in data else 2
    match.game_id = game.id
    if 'isPrivate' in data:
        match.is_private = data['isPrivate']
    else:
        match.is_private = False
    user = auth.get_logged_user()
    match.creator_id = user.id
    user.matches.append(match)
    db.session.add(match)
    db.session.commit()
    return jsonify(match.long()), 201
def create_tournament(payload):
    data = request.json

    # Prepare game
    game = Game.query.filter(Game.id == data['gameId']).first()
    if not game:
        game = Game.query.filter(Game.name.ilike(data['gameName'].strip())).first()
    if not game:
        game = Game(name=data['gameName'])
        db.session.add(game)
        db.session.flush()

    tournament = Tournament(name=data)
    # Generate the UUID
    uuid_len = 6
    tries = 0
    max_tries = 10
    tournament_found = True
    uuid = ''
    while tournament_found:
        if tries >= max_tries:
            tries = 0
            uuid_len += 1
        tries += 1
        uuid = generate_uuid(uuid_len)
        tournament_found = db.session.query(Tournament).filter(tournament.uuid == uuid).first()
    tournament.uuid = uuid
    tournament.name = data['name']
    tournament.max_participants = data['maxParticipants']
    if 'startDate' in data:
        tournament.start_date = data['startDate']
    if 'startDateTz' in data:
        tournament.start_date_tz = data['startDateTz']
    tournament.game_id = game.id
    user = auth.get_logged_user()
    tournament.creator_id = user.id
    db.session.add(tournament)
    db.session.flush()
    db.session.commit()
    return jsonify(tournament.long()), 201
示例#6
0
def user_me():
    user = auth.get_logged_user()
    return jsonify({
        'username': user.username,
        'email': user.email
    })
示例#7
0
 def get_user_info():
     user = auth.get_logged_user()
     if user:
         user = user.base_info()
     return jsonify(user)
def patch_match(payload, match_id):
    data = request.json
    if not 'action' in data:
        return errors.bad_request_error(
            'Passed data must contain "action" value')
    action = data['action']
    # Get Match
    match = db.session.query(Match).filter(Match.id == match_id).first()
    if not match:
        return errors.not_found_error('Match not found')

    logged_user = auth.get_logged_user()
    if action == 'join':
        if len(match.participants) < match.max_participants:
            for participant in match.participants:
                if participant.id == logged_user.id:
                    return errors.bad_request_error(
                        'You can\'t join an already joined match')
            match.participants.append(logged_user)
            match.update()
            return '', 204
        else:
            return errors.bad_request_error('You can\'t join on a full match')
    elif action == 'disjoin':
        MatchParticipants.query.filter(
            (MatchParticipants.match_id == match_id)
            & (MatchParticipants.user_id == logged_user.id)).delete()
        db.session.commit()
        return '', 204
    elif action == 'edit':
        if match.creator_id != logged_user.id:
            return errors.forbidden_error('You can edit only you\'re matches')
        if 'maxParticipants' in data:
            match.max_participants = data['maxParticipants']
        if 'name' in data:
            match.name = data['name']
        if 'isPrivate' in data:
            match.is_private = data['isPrivate']
        if 'join' in data:
            for user_id in data['join']:
                user = User.query.filter(User.id == user_id).first()
                if user:
                    match.participants.append(user)
        if 'remove' in data:
            match.participants.filter(User.id.in_(data['remove'])).clear()
            MatchParticipants.query.filter(
                (MatchParticipants.match_id == match_id)
                & (MatchParticipants.user_id.in_(data['remove']))).delete()
        if 'gameId' in data:
            print('gameId is', data['gameId'])
            if data['gameId'] is None:
                match.game_id = None
            else:
                game = Game.query.filter(Game.id == data['gameId']).first()
                if not game:
                    game = Game.query.filter(
                        Game.name.ilike(data['gameName'].strip())).first()
                    if not game:
                        if 'gameName' in data:
                            game = Game(name=data['gameName'])
                            db.session.add(game)
                            db.session.flush()
                            match.game_id = game.id
                    else:
                        match.game_id = game.id
                else:
                    match.game_id = game.id
        db.session.commit()
        return jsonify(match.long())
    return errors.bad_request_error(
        '"{}" action is not supported'.format(action))
#!/usr/bin/python
# -*- coding: utf-8 -*-
from web import template

from auth import get_logged_user, get_logoff_url
from model import get_exposed_managed_tables 

import logging

admin_render_globals = {'user': get_logged_user(), 'logoff_url': get_logoff_url(), 'exposed_managed_tables': get_exposed_managed_tables()}
admin_template_path = template.render('templates/admin', cache=False, base="admin", globals=admin_render_globals)

def index_admin():
    return admin_template_path.index_admin()

def form(form, titulo=None, verbo=None, display_message=None, **kw):
    method = kw.pop('method', 'post')
    action = kw.pop('action', '')
    return admin_template_path.form(titulo, action, method, form, verbo, display_message)

def listar(frm, pagination, display_message=None):
    filtro = ''
    if pagination.query is not None and pagination.query != 'None' and pagination.query != 'all' and pagination.query !='':
        filtro = 'Filtrando por: %(query)s' % {'query': pagination.query}
    return admin_template_path.lista(frm, filtro, pagination, display_message)