def test_change_event(self): """ Ensure we can change an existing game. """ event = Event() event.game_id = 1 event.organizer_id = 1 event.time = "12:25:00" event.date = "2000-03-29" event.description = "junior software developer" event.save() # DEFINE NEW PROPERTIES FOR GAME data = { "gameId": 1, "description": "Hasbro", "time": "12:25:00", "date": "2000-03-29" } self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token) response = self.client.put(f"/events/{event.id}", data, format="json") self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) # GET EVENT AGAIN TO VERIFY CHANGES response = self.client.get(f"/events/{event.id}") json_response = json.loads(response.content) self.assertEqual(response.status_code, status.HTTP_200_OK) # Assert that the properties are correct # self.assertEqual(json_response["gameId"], 1) self.assertEqual(json_response["description"], "Hasbro") self.assertEqual(json_response["time"], "12:25:00") self.assertEqual(json_response["date"], "2000-03-29")
def test_get_event(self): """ Ensure we can get an existing event. """ # Seed the database with a event event = Event() event.time = "19:30:00" event.date = "2021-08-14" event.game_id = 1 event.description = "We all live in a yellow submarine" event.organizer_id = 1 event.save() # Make sure request is authenticated self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token) # Initiate request and store response response = self.client.get(f"/events/{event.id}") # Parse the JSON in the response body json_response = json.loads(response.content) # Assert that the game was retrieved self.assertEqual(response.status_code, status.HTTP_200_OK) # Assert that the values are correct self.assertEqual(json_response["time"], "19:30:00") self.assertEqual(json_response["date"], "2021-08-14") self.assertEqual(json_response["description"], "We all live in a yellow submarine")
def test_get_event(self): """ Ensure we can get an existing game. """ # Seed the database with a game event = Event() event.description = "Test" event.date = "2021-02-14" event.time = "12:30:00" event.organizer = Gamer.objects.get(pk=1) event.game = Game.objects.get(pk=1) event.save() # Make sure request is authenticated self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token) # Initiate request and store response response = self.client.get(f"/events/{event.id}") # Parse the JSON in the response body json_response = json.loads(response.content) # Assert that the game was retrieved self.assertEqual(response.status_code, status.HTTP_200_OK) # Assert that the values are correct self.assertEqual(json_response["description"], "Test") self.assertEqual(json_response["date"], "2021-02-14") self.assertEqual(json_response["time"], "12:30:00") self.assertEqual(json_response["organizer"]["id"], 1) self.assertEqual(json_response["game"]["id"], 1)
def create(self, request): """Handle POST operations for events Returns: Response -- JSON serialized event instance """ gamer = Gamer.objects.get(user=request.auth.user) event = Event() event.time = request.data["time"] event.date = request.data["date"] event.name = request.data["name"] event.description = request.data["description"] event.host = gamer game = Game.objects.get(pk=request.data["game_id"]) event.game = game try: event.save() serializer = EventSerializer(event, context={'request': request}) return Response(serializer.data) except ValidationError as ex: return Response({"reason": ex.message}, status=status.HTTP_400_BAD_REQUEST)
def test_get_event(self): """ Ensure we can get an existing game. """ # Seed the database with a game event = Event() event.game_id = 1 event.organizer_id = 1 event.time = "11:45:00" event.date = "2021-05-19" event.description = "budding software developer" event.save() # Make sure request is authenticated self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token) # Initiate request and store response response = self.client.get(f"/events/{event.id}") # Parse the JSON in the response body json_response = json.loads(response.content) # Assert that the game was retrieved self.assertEqual(response.status_code, status.HTTP_200_OK) # Assert that the values are correct # self.assertEqual(json_response["organizer"]["id"], "Monopoly") self.assertEqual(json_response["description"], "budding software developer") # self.assertEqual(json_response["game"]["id"], 5) self.assertEqual(json_response["time"], "11:45:00") self.assertEqual(json_response["date"], "2021-05-19")
def gamerevent_list(request): """Function to build an HTML report of events by gamer""" if request.method == 'GET': # Connect to project database with sqlite3.connect(Connection.db_path) as conn: conn.row_factory = sqlite3.Row db_cursor = conn.cursor() # Query for all events by user db_cursor.execute(""" SELECT e.organizer_id, u.first_name || ' ' || u.last_name AS full_name, e.id, e.date, e.time, g.title FROM levelupapi_event e JOIN levelupapi_game g ON e.game_id = g.id JOIN auth_user u ON e.organizer_id = u.id """) dataset = db_cursor.fetchall() events_by_user = {} for row in dataset: # create an Event instance and set its properties event = Event() event.id = row["id"] event.date = row["date"] event.time = row["time"] event.game_name = row["title"] uid = row["organizer_id"] if uid in events_by_user: events_by_user[uid]['events'].append(event) else: events_by_user[uid] = {} events_by_user[uid]["organizer_id"] = uid events_by_user[uid]["full_name"] = row["full_name"] events_by_user[uid]["events"] = [event] list_of_users_with_events = events_by_user.values() template = 'users/list_with_events.html' context = { 'eventuser_list': list_of_users_with_events } return render(request, template, context)
def event_host_list(request): if request.method == 'GET': with sqlite3.connect(Connection.db_path) as conn: conn.row_factory = sqlite3.Row db_cursor = conn.cursor() db_cursor.execute(""" select e.date, e.time, e.id as event_id, g.title as game_title, u.id as user_id, u.first_name || " " || u.last_name as user_full_name from levelupapi_event e join levelupapi_gamerevent eg on e.id = eg.event_id join levelupapi_gamer gr on eg.gamer_id = gr.id join auth_user u on gr.user_id = u.id join levelupapi_game g on e.game_id = g.id """) dataset = db_cursor.fetchall() events_dict = {} for row in dataset: event = Event() event.id = row["event_id"] event.date = row['date'] event.time = row['time'] event.game_title = row['game_title'] attendee_dict = {} attendee_dict['user_id'] = row['user_id'] attendee_dict['full_name'] = row['user_full_name'] if event.id in events_dict: events_dict[event.id]["attendees"].append(attendee_dict) else: events_dict[event.id] = {} events_dict[event.id]['date'] = event.date events_dict[event.id]['time'] = event.time events_dict[event.id]['game_title'] = event.game_title events_dict[event.id]['attendees'] = [attendee_dict] events_list = events_dict.values template = 'users/list_with_attendees.html' context = {'event_attendee_list': events_list} return render(request, template, context)
def userevent_list(request): if request.method == 'GET': with sqlite3.connect(Connection.db_path) as conn: conn.row_factory = sqlite3.Row db_cursor = conn.cursor() db_cursor.execute(""" SELECT e.id, e.date, e.time, e.game_id, g.title, u.id user_id, u.first_name || ' ' || u.last_name AS full_name FROM levelupapi_event e JOIN levelupapi_game g ON e.game_id = g.id JOIN levelupapi_gamer gr ON g.gamer_id = gr.id JOIN auth_user u ON gr.user_id = u.id """) dataset = db_cursor.fetchall() events_by_user = {} for row in dataset: event = Event() event.date = row["date"] event.time = row["time"] event.game_name = row["title"] uid = row["user_id"] if uid in events_by_user: events_by_user[uid]['events'].append(event) else: events_by_user[uid] = {} events_by_user[uid]["id"] = uid events_by_user[uid]["full_name"] = row["full_name"] events_by_user[uid]["events"] = [event] list_of_users_with_events = events_by_user.values() template = 'users/list_with_events.html' context = { 'userevent_list': list_of_users_with_events } return render(request, template, context)
def create(self, request): gamer = Gamer.objects.get(user=request.auth.user) event = Event() event.time = request.data["time"] event.date = request.data["date"] event.description = request.data["description"] event.organizer = gamer game = Game.objects.get(pk=request.data["gameId"]) event.game = game try: event.save() serializer = EventSerializer(event, context={'request': request}) return Response(serializer.data) except ValidationError as ex: return Response({"reason": ex.message}, status=status.HTTP_400_BAD_REQUEST)
def test_delete_event(self): """ Ensure we can delete an existing game. """ event = Event() event.game_id = 1 event.organizer_id = 1 event.time = "11:25:00" event.date = "2000-03-17" event.description = "senior software developer" event.save() self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token) response = self.client.delete(f"/events/{event.id}") self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) # GET event AGAIN TO VERIFY 404 response response = self.client.get(f"/events/{event.id}") self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR)
def test_change_event(self): """ Ensure we can change an existing event. """ event = Event() event.time = "13:00" event.date = "11-04-2020" event.description = "Party Night" gamer = Gamer.objects.get(pk=1) event.organizer = gamer game = Game.objects.get(pk=1) event.game = game event.save() # DEFINE NEW PROPERTIES FOR GAME data = { "time": "15:00", "date": "12-25-2020", "description": "Xmas Party Night", "organizer": 1, "gameId": 1 } # print('GAME ID ' + str(game.id)) self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token) response = self.client.put(f"/events/{event.id}", data, format='json') self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) # get game again to verify changes response = self.client.get(f"/events/{event.id}") json_response = json.loads(response.content) self.assertEqual(response.status_code, status.HTTP_200_OK) # assert that the properties are correct self.assertEqual(json_response["time"], "15:00") self.assertEqual(json_response["date"], "12-25-2020") self.assertEqual(json_response["description"], "Xmas Party Night")
def test_delete_event(self): """ Ensure we can delete an existing event. """ event = Event() event.time = "13:00" event.date = "11-04-2020" event.description = "Party Night" gamer = Gamer.objects.get(pk=1) event.organizer = gamer game = Game.objects.get(pk=1) event.game = game event.save() self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token) response = self.client.delete(f"/events/{event.id}") self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) # GET GAME AGAIN TO VERIFY 404 response response = self.client.get(f"/events/{event.id}") self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
def userevent_list(request): """Function to build an HTML report of games by user""" if request.method == 'GET': # Connect to project database with sqlite3.connect(Connection.db_path) as conn: conn.row_factory = sqlite3.Row db_cursor = conn.cursor() # Query for all games, with related user info. db_cursor.execute(""" SELECT u.id as user_id, u.first_name || ' ' || u.last_name AS full_name, e.id, e.description, e.date, e.time, g.id as game_id FROM levelupapi_event e JOIN levelupapi_game g ON g.id = e.game_id JOIN levelupapi_gamerevent ge ON e.id = ge.event_id JOIN auth_user u ON ge.gamer_id = u.id """) dataset = db_cursor.fetchall() # { # 1: { # "gamer_id": 1, # "full_name": "Molly Ringwald", # "events": [ # { # "id": 5, # "date": "2020-12-23", # "time": "19:00", # "game_name": "Fortress America" # } # ] # } #} events_by_user = {} for row in dataset: event = Event() event.description = row["description"] event.date = row["date"] event.time = row["time"] event.game = Game.objects.get(pk=row["game_id"]) # Store the user's id uid = row["user_id"] # If the user's id is already a key in the dictionary... if uid in events_by_user: # Add the current game to the `games` list for it events_by_user[uid]['events'].append(event) else: # Otherwise, create the key and dictionary value events_by_user[uid] = {} events_by_user[uid]["id"] = uid events_by_user[uid]["full_name"] = row["full_name"] events_by_user[uid]["events"] = [event] # Get only the values from the dictionary and create a list from them list_of_users_with_events = events_by_user.values() # Specify the Django template and provide data context template = 'users/list_with_events.html' context = {'userevent_list': list_of_users_with_events} return render(request, template, context)
def event_attendee_list(request): if request.method == 'GET': with sqlite3.connect(Connection.db_path) as conn: conn.row_factory = sqlite3.Row db_cursor = conn.cursor() db_cursor.execute(""" SELECT e.id as event_id, e.date, e.time, g.title as game_name, u.id AS user_id, u.first_name || " " || u.last_name as user_full_name FROM levelupapi_event e JOIN levelupapi_eventgamer eg ON e.id = eg.event_id JOIN levelupapi_gamer gr ON eg.gamer_id = gr.id JOIN auth_user u ON gr.user_id = u.id JOIN levelupapi_game g ON e.game_id = g.id """) dataset = db_cursor.fetchall() events_dict = {} """ { 1: { date: time: game_name: attendees: [ { gamer_id: 1 full_name: "steve brownlee" } ] } } """ for row in dataset: event = Event() event.id = row["event_id"] event.date = row["date"] event.time = row["time"] event.game_name = row["game_name"] attendee_dict = {} attendee_dict.user_id = row["user_id"] attendee_dict.full_name = row["user_full_name"] if event.id in events_dict: events_dict[event.id]["attendees"].append(attendee_dict) else: events_dict[event.id] = {} events_dict[event.id]["date"] = event.date events_dict[event.id]["time"] = event.time events_dict[event.id]["game_name"] = event.game_name events_dict[event.id]["attendees"] = [attendee_dict] event_list = events_dict.values() template = "events/list_with_attendees.html" context = {'event_attendee_list': event_list} return render(request, template, context)
def userevent_list(request): """Function to build an HTML report of events by user""" if request.method == 'GET': # Connect to project database with sqlite3.connect(Connection.db_path) as conn: conn.row_factory = sqlite3.Row db_cursor = conn.cursor() # Query for all games with related user info. db_cursor.execute(""" SELECT id, description, date, time, title, user_id, full_name FROM EVENTS_BY_USER """) dataset = db_cursor.fetchall() # { # 1: { # "gamer_id": 1, # "full_name": "Molly Ringwald", # "events": [ # { # "id": 5, # "date": "2020-12-23", # "time": "19:00", # "game_name": "Fortress America" # } # ] # } # } events_by_user = {} for row in dataset: # Create a Game instance and set its properties event = Event() event.date = row["date"] event.time = row["time"] # Store the user's id uid = row["user_id"] #if the user's id is already a key in the dictionary ... if uid in games_by_user: # Add the current game to the 'games' list for it events_by_user[uid]['events'].append(event) else: # Otherwise, create the key and dictionary value events_by_user[uid] = {} events_by_user[uid]["id"] = uid events_by_user[uid]["full_name"] = row["full_name"] events_by_user[uid]["events"] = [event] # Get only the values from the dictionary and create a list from them list_of_users_with_events = events_by_user.values() # Specify the Django template and provide data context template = 'users/list_with_events.html' context = {'userevent_list': list_of_users_with_events} return render(request, template, context)
def userevent_list(request): """Function to build an HTML report events by user""" if request.method == 'GET': # Connect to project database with sqlite3.connect(Connection.db_path) as conn: conn.row_factory = sqlite3.Row db_cursor = conn.cursor() # Query for all games, with related user info. db_cursor.execute(""" SELECT e.id, e.description, e.organizer_id, e.game_id, e.date, e.time, u.id user_id, u.first_name || ' ' || u.last_name AS full_name FROM levelupapi_event e JOIN levelupapi_gamer gr ON e.organizer_id = gr.id JOIN auth_user u ON gr.user_id = u.id """) dataset = db_cursor.fetchall() # Take the flat data from the database, and build the # following data structure for each gamer. # # { # 1: { # "id": 1, # "full_name": "Admina Straytor", # "games": [ # { # "id": 1, # "title": "Foo", # "maker": "Bar Games", # "skill_level": 3, # "number_of_players": 4, # "gametype_id": 2 # } # ] # } # } events_by_user = {} for row in dataset: # Crete a Game instance and set its properties event = Event() event.description = row["description"] event.organizer_id = row["organizer_id"] event.game_id = row["game_id"] event.date = row["date"] event.time = row["time"] # Store the user's id uid = row["user_id"] # If the user's id is already a key in the dictionary... if uid in events_by_user: # Add the current game to the `games` list for it events_by_user[uid]['events'].append(event) else: # Otherwise, create the key and dictionary value events_by_user[uid] = {} events_by_user[uid]["id"] = uid events_by_user[uid]["full_name"] = row["full_name"] events_by_user[uid]["events"] = [event] # Get only the values from the dictionary and create a list from them list_of_users_with_events = events_by_user.values() # Specify the Django template and provide data context template = 'users/list_with_events.html' context = {'userevent_list': list_of_users_with_events} return render(request, template, context)
def event_host_list(request): if request.method == 'GET': with sqlite3.connect(Connection.db_path) as conn: conn.row_factory = sqlite3.Row db_cursor = conn.cursor() db_cursor.execute(""" SELECT e.date, e.time, e.id AS event_id, g.title AS game_name, u.id AS user_id, u.first_name || " " || u.last_name AS user_full_name FROM levelupapi_event e JOIN levelupapi_gamer gr ON e.organizer_id = gr.id JOIN auth_user u ON gr.user_id = u.id JOIN levelupapi_game g ON e.game_id = g.id """) dataset = db_cursor.fetchall() users_dict = {} """ { 1: { organizer_id: full_name: events: [ { date: time: id: game_name: } ] } } """ for row in dataset: event = Event() event.id = row["event_id"] event.date = row["date"] event.time = row["time"] event.game_name = row["game_name"] uid = row["user_id"] if uid in users_dict: users_dict[uid]['events'].append(event) else: users_dict[uid] = {} users_dict[uid]["organizer_id"] = uid users_dict[uid]["full_name"] = row["user_full_name"] users_dict[uid]["events"] = [event] list_of_users = users_dict.values() template = "users/list_with_events.html" context = {'event_host_list': list_of_users} return render(request, template, context)