davidcr01 / WordlePlus

Repository to store all the documentation, files and structure of my Final Degree Project (TFG in Spanish). The main goal is to develop, as full-stack web developer, a remodel of the Wordle game, including more features and functionalities using Ionic, Django REST Api and PostgreSQL.
1 stars 0 forks source link

Tournament creation (S5/S6) #34

Closed davidcr01 closed 1 year ago

davidcr01 commented 1 year ago

Description

Is necessary for the admins to create an open tournament. This tournament will be opened to the rest of the players.

Tasks

This issue also solves HU8 and HU9 completely.

davidcr01 commented 1 year ago

Update Report

Model, serializer, and view

The model, view, serializer, and URL have been added to the project:

The model:

# Model to store the tournament information.
class Tournament(models.Model):
    name = models.CharField(max_length=20)
    description = models.TextField(blank=True)
    max_players = models.PositiveIntegerField()
    word_length = models.PositiveIntegerField()
    is_closed = models.BooleanField(default=False)

The serializer:

# Model to store the tournaments information.
class TournamentSerializer(serializers.ModelSerializer):
    class Meta:
        model = Tournament
        fields = ['name', 'description', 'max_players', 'word_length', 'is_closed']

The view:

class TournamentViewSet(viewsets.ReadOnlyModelViewSet):
    queryset = Tournament.objects.order_by('max_players')
    serializer_class = TournamentSerializer

    def get_queryset(self):
        queryset = super().get_queryset()

        # Obtain the word_lenght parameter
        word_length = self.request.query_params.get('word_length')

        if word_length:
            # Filter tournaments by length
            queryset = queryset.filter(word_length=word_length)

        return queryset

The URL:

    path('api/tournaments/', TournamentViewSet.as_view({'get': 'list'}), name='tournaments-list'),

Notice that the view only accepts the GET request, as all the tournament manage will be done in the Admin site. With this, the tournaments will be ordered by the max_players fields, and the parameter word_length has been added. The URL works as expected:

GET http://localhost:8080/api/tournaments?word_length=4

RESULT:
{
  "count": 1,
  "next": null,
  "previous": null,
  "results": [
    {
      "name": "First tournament",
      "description": "",
      "max_players": 2,
      "word_length": 4,
      "is_closed": false
    }
  ]
}
GET http://localhost:8080/api/tournaments

RESULT
{
  "count": 2,
  "next": null,
  "previous": null,
  "results": [
    {
      "name": "First tournament",
      "description": "",
      "max_players": 2,
      "word_length": 4,
      "is_closed": false
    },
    {
      "name": "Second tournament",
      "description": "",
      "max_players": 8,
      "word_length": 5,
      "is_closed": false
    }
  ]
}

Admin site

The code of the admin site is the following:

class TournamentsForm(forms.ModelForm):
    word_length = forms.ChoiceField(choices=[
        (4, '4'),
        (5, '5'),
        (6, '6'),
        (7, '7'),
        (8, '8'),
    ])

    max_players = forms.ChoiceField(choices=[
        (2, '2'),
        (4, '4'),
        (8, '8'),
        (16, '16'),
    ])

    class Meta:
        model = Tournament
        fields = '__all__'

class TournamentAdmin(admin.ModelAdmin):
    list_display = ('name', 'description', 'max_players', 'word_length', 'is_closed')
    form = TournamentsForm

We added a form to limit the options of the word_length and the max_players input to avoid introducing wrong values. The form is specified in the Admin class, in the form variable.

image

Now, in the admin site, these fields contain the valid values in a dropdown.