def singin_user(request): if request.POST.get('login') and request.POST.get('password'): login = request.POST['login'] password = request.POST['password'] if User.is_user_valid(login, password): user = User.objects().get(login=login) if pwd_context.verify(password, user.get_password()): request.session['logged'] = 1 request.session['username'] = user.get_fullname() request.session['admin'] = user.get_status() return request return False else: return False
def save_user(user): if User.objects().count() == 0: status = True else: status = False User(name=user['name'], surname=user[ 'surname'], login=user['login'], password=user['password'], superuser=status).save() data_for_template['status'] = u'Użytkownik dodany.'
def get_players(): players = [] for user in User.objects(): player = {'name': user.get_fullname(), 'login': user.get_login(), 'admin': user.get_status()} players.append(player) return players
def validate_data(request): user = { 'name': request.POST.get('name'), 'surname': request.POST.get('surname'), 'login': request.POST.get('login'), 'password': request.POST.get('password'), } if user['name'] == '' or user['surname'] == '' or user['login'] == '' or user['password'] == '': data_for_template['status'] = u'Brakuje danych' else: user['password'] = pwd_context.encrypt( request.POST.get('password')) try: data_for_template[ 'status'] = u'Użytkownik istnieje z tym loginem.' User.objects().get(login=user['login']) except: save_user(user) request.session['registered'] = 1 data_for_template['status'] = u'Użytkownik nieistnieje.' return True finally: data_for_template['name'] = user['name'] data_for_template['surname'] = user['surname']
def get_players(): def get_goals(players): for player in players: for match in Match.objects(): player = match.get_players_scores(player) return players players = [] player = {} for user in User.objects(): player = {'scores': 0, 'own': 0} player['name'] = name = user.get_fullname() player['win_matches'] = Match.objects( __raw__={'win_team.username': name}).count() player['defeat_matches'] = Match.objects( __raw__={'defeat_team.username': name}).count() query = {'$or': [{'win_team.username': name}, { 'defeat_team.username': name}]} player['matches'] = Match.objects(__raw__=query).count() players.append(dict(player)) players = get_goals(players) players = sorted(players, key=lambda k: k['scores'], reverse=True) return players
def get_number_players(): return User.objects().count()
def set_admin(userlogin, status): User.objects().get(login=userlogin).update(__raw__={ '$set': {'superuser': status}})
def delete_user_from_db(userlogin): user = User.objects().get(login=userlogin) user.delete()