Exemplo n.º 1
0
    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.day = request.data["day"]
        event.location = request.data["location"]
        event.gamer = 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)
Exemplo n.º 2
0
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 events, with related user info.
            db_cursor.execute("""
                SELECT
                    eg.id,
                    u.first_name || ' ' || u.last_name AS full_name,
                    u.id user_id,
                    eg.event_id,
                    e.location,
                    e.time,
                    e.day,
                    e.id event_id_number,
                    g.title
                    
                FROM levelupapi_eventgamer eg
                JOIN levelupapi_event e ON e.id = eg.event_id
                JOIN levelupapi_gamer gr ON gr.id = eg.gamer_id
                JOIN levelupapi_game g ON g.id = e.game_id
                JOIN auth_user u ON u.id = gr.user_id
            """)

            dataset = db_cursor.fetchall()
            events_by_user = {}

            for row in dataset:
                # Crete a Event instance and set its properties
                event = Event()
                event.id = row["event_id_number"]
                event.day = row["day"]
                event.time = row["time"]
                event.location = row["location"]
                event.title = row["title"]

                # 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 event to the `events` 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]["user_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 = {'userevents_list': list_of_users_with_events}

        return render(request, template, context)