def addTeam(): if request.method == 'POST': if not request.is_json: return jsonify({"msg": "Not a proper JSON"}), 400 name = request.json.get('name') leaderId = request.json.get('leaderId') users = request.json.get('users') tasks = request.json.get('tasks') if (models.User.query.filter_by(name=name).first() != None): return jsonify({"msg": "Team already exists with that name"}), 400 team = models.Team(name=name, leaderId=leaderId) print(team.users) for user in users: userId = user["id"] userToAdd = models.User.query.filter_by(id=userId).first() team.users.append(userToAdd) print(team.users) try: database.db_session.add(team) database.db_session.commit() token = { 'access_token': create_access_token(identity={'id': team.id, 'name': team.name, 'leaderId': team.leaderId}), } except Exception as e: print(e) return jsonify({"msg": "Cannot add team"}), 401 return jsonify(token), 200
def add_team(school, team_num=None, password=None, reorganize=True): if not password: password = choice(passwords) if not team_num: team_nums = [t.team_num for t in school.teams] team_num = 1 while team_num in team_nums: team_num += 1 team = models.Team(team_num=team_num, password=password, school=school) db.session.add(team) if reorganize: reorganize_school(school)
def test_get_team(team_id: int, status_code: int, field: str, value: str, monkeypatch, test_app: TestClient): mock_team = models.Team(id=1, team_name='Engineering') def mock_read(*args): if team_id == 0: # team that does not exist return None return mock_team monkeypatch.setattr(crud, 'read', mock_read) response = test_app.get(f'/teams/{team_id}') assert response.status_code == status_code assert response.json()[field] == value
def test_get_user_team(user_id: int, status_code: int, field: Union[int, str], value: Union[int, str], monkeypatch, test_app: TestClient): MockUserWithTeam = namedtuple('MockUserWithTeam', field_names=['team']) mock_user = MockUserWithTeam(team=models.Team(id=1, team_name='Testing')) def mock_read(*args): if user_id == 0: return None return mock_user monkeypatch.setattr(crud, 'read', mock_read) response = test_app.get(f'/users/{user_id}/team') assert response.status_code == status_code assert response.json()[field] == value
def populate_db(): room = models.ChatRooms(name="General", roomName="General") team = models.Team(name="team1", leaderId=1) user = models.User( name="test", email="*****@*****.**", password="******", userType=0) user.rooms.append(room) database.db_session.add(user) team.users.append(user) user = models.User( name="John", email="*****@*****.**", password="******", userType=0) user.rooms.append(room) database.db_session.add(user) team.users.append(user) user = models.User( name="Olivia", email="*****@*****.**", password="******", userType=0) user.rooms.append(room) database.db_session.add(user) team.users.append(user) user = models.User( name="Manager", email="*****@*****.**", password="******", userType=1) user.rooms.append(room) database.db_session.add(user) team.users.append(user) user = models.User( name="Emma", email="*****@*****.**", password="******", userType=0) user.rooms.append(room) database.db_session.add(user) team.users.append(user) database.db_session.add(team) database.db_session.commit()
def test_delete_team(team_id: int, status_code: int, field: str, value: Union[str, dict], monkeypatch, test_app: TestClient): mock_team = models.Team(id=team_id, team_name='Engineering') def mock_read(*args): if team_id == 0: return None return mock_team def mock_delete(*args): return mock_team monkeypatch.setattr(crud, 'read', mock_read) monkeypatch.setattr(crud, 'delete', mock_delete) response = test_app.delete(f'/teams/{team_id}') assert response.status_code == status_code assert response.json()[field] == value
def test_update_team(team_id: int, team_name: str, status_code: int, field: str, value: Union[str, dict], monkeypatch, test_app: TestClient): mock_team = models.Team(id=team_id, team_name='Pre-Change') def mock_read(*args): if team_id == 0: return None return mock_team def mock_update(*args): mock_team.team_name = team_name return mock_team monkeypatch.setattr(crud, 'read', mock_read) monkeypatch.setattr(crud, 'update', mock_update) payload = {'team': {'team_name': team_name}} response = test_app.put(f'/teams/{team_id}', json=payload) assert response.status_code == status_code assert response.json()[field] == value
def mock_read_all(*args): mock_team1 = models.Team(id=1, team_name='Engineering') mock_team2 = models.Team(id=2, team_name='IT') return [mock_team1, mock_team2]
def mock_create(*args): return models.Team(id=1, team_name='Engineering')
from app import db, models team_data = open('Scripts/teams.txt', 'r') for line in team_data: line = line.strip() split_line = line.split(',') new_team = models.Team(city=split_line[0], nickname=split_line[1], stadium=split_line[2]) db.session.add(new_team) db.session.commit() team_data.close()
def addteam(request): # if this is a POST request we need to process the form data if request.method == 'POST': # create a form instance and populate it with data from the request: form = forms.AddTeamForm(request.POST, request.FILES) # check whether it's valid: if form.is_valid(): text = form.cleaned_data['description'].lower() name = form.cleaned_data['team_name'].lower() badword = False words = [] for word in models.BadWords.objects.all(): if word.word in text: badword = True words.append(word.word) elif word.word in name: badword = True words.append(word.word) text = list(map(len, text.split())) text.sort(reverse=True) if text[0] > 30: return render( request, 'app/text.html', { 'title': 'Ошибка', 'text': _('Какое-то слово(а) в вашем описании' ' больше 30 символов.') }) elif False: # Не, ну это надо пофиксить return render( request, 'app/text.html', { 'title': 'Ошибка', 'text': _('Проверьте формат ссылки на группу в' ' Steam. Например: http://steamcommunity.com/groups/potatogroup' ) }) elif badword: return render( request, 'app/text.html', { 'title': 'Ошибка', 'text': _('В названии или тексте содержатся недопустимые слова ' ) + str(words) }) elif False: # Ну прям совсем позор return render(request, 'app/text.html', { 'title': 'Ошибка', 'text': _('Не более 2-х команд на аккаунт') }) else: # process the data in form.cleaned_data as required tm = models.Team( name=form.cleaned_data['team_name'], owner=request.META.get('REMOTE_ADDR'), founded=form.cleaned_data['founded'], description=form.cleaned_data['description'], team_url=form.cleaned_data['team_url'], image=form.cleaned_data['img_url'], min_rank=form.cleaned_data['min_rank'], max_rank=form.cleaned_data['max_rank'], is_mm=form.cleaned_data['is_mm'], is_pu=form.cleaned_data['is_pu'], is_le=form.cleaned_data['is_le'], is_ca=form.cleaned_data['is_ca'], need_awp=form.cleaned_data['need_awp'], need_rif=form.cleaned_data['need_rif'], need_sup=form.cleaned_data['need_sup'], need_luk=form.cleaned_data['need_luk'], need_frg=form.cleaned_data['need_frg'], need_igl=form.cleaned_data['need_igl'], ) tm.save() # return render(request, 'app/text.html', {'text': }) # redirect to a new URL: return HttpResponseRedirect('/teams') # if a GET (or any other method) we'll create a blank form else: form = forms.AddTeamForm return render(request, 'app/addteam.html', { 'title': _('Добавить команду'), 'form': form })
def upload_teams(album): teams = ['red', 'neutral', 'death', 'blue'] images = client.get_album_images(album) for team, image in zip(teams, images): db.session.add(models.Team(team=team, image=image.link)) db.session.commit()