Skip to content

Commit

Permalink
Merge branch 'dev' into tournaments/nonstaff-create-edit
Browse files Browse the repository at this point in the history
  • Loading branch information
jb-01 authored Dec 6, 2023
2 parents b2e3cd5 + 7dd1681 commit ae99927
Show file tree
Hide file tree
Showing 64 changed files with 1,668 additions and 85 deletions.
7 changes: 7 additions & 0 deletions src/chigame/api/serializers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from rest_framework import serializers

from chigame.games.models import Chat, Game, Lobby, Message, Tournament, User
from chigame.users.models import Group


class GameSerializer(serializers.ModelSerializer):
Expand Down Expand Up @@ -68,6 +69,12 @@ def create(self, validated_data):
return message


class GroupSerializer(serializers.ModelSerializer):
class Meta:
model = Group
fields = "__all__"


class MessageFeedSerializer(serializers.ModelSerializer):
sender = serializers.SerializerMethodField()

Expand Down
3 changes: 3 additions & 0 deletions src/chigame/api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,7 @@
# CHAT API URLS
path("tournaments/chat/", views.MessageView.as_view(), name="api-chat-list"),
path("tournaments/chat/feed/", views.MessageFeedView.as_view(), name="api-chat-detail"),
# GROUP API URLS
path("groups/<int:pk>/members/", views.GroupMembersView.as_view(), name="api-group-members"),
path("users/<slug:slug>/groups/", views.UserGroupsView.as_view(), name="api-user-groups"),
]
41 changes: 33 additions & 8 deletions src/chigame/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,24 @@
from chigame.api.filters import GameFilter
from chigame.api.serializers import (
GameSerializer,
GroupSerializer,
LobbySerializer,
MessageFeedSerializer,
MessageSerializer,
UserSerializer,
)
from chigame.games.models import Game, Lobby, Message, User
from chigame.users.models import UserProfile
from chigame.users.models import Group, UserProfile


# Helper function to get user from slug
def get_user(lookup_value):
# If the lookup_value is an integer, use the id field
if lookup_value.isdigit():
return get_object_or_404(User, pk=lookup_value)
else:
# Otherwise, use the slug field
return get_object_or_404(User, username=lookup_value)


class GameListView(generics.ListCreateAPIView):
Expand Down Expand Up @@ -65,20 +76,34 @@ class UserDetailView(generics.RetrieveUpdateDestroyAPIView):

def get_object(self):
lookup_value = self.kwargs.get(self.lookup_field)

# If the lookup_value is an integer, use the id field
if lookup_value.isdigit():
return get_object_or_404(User, pk=lookup_value)
else:
# Otherwise, use the slug field
return get_object_or_404(User, username=lookup_value)
return get_user(lookup_value)


class MessageView(generics.CreateAPIView):
queryset = Message.objects.all()
serializer_class = MessageSerializer


class GroupMembersView(generics.ListAPIView):
serializer_class = UserSerializer

def get_queryset(self):
group_id = self.kwargs["pk"]
group = Group.objects.get(pk=group_id)
return group.members.all()


class UserGroupsView(generics.ListAPIView):
serializer_class = GroupSerializer
lookup_field = "slug"

def get_queryset(self):
lookup_value = self.kwargs.get(self.lookup_field)
user_id = get_user(lookup_value).id
groups = Group.objects.filter(members__pk=user_id)
return groups


class MessageFeedView(APIView):
def post(self, request, *args, **kwargs):
# Get data from the frontend
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@

class ForumsConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "forums"
name = "chigame.forums"
16 changes: 16 additions & 0 deletions src/chigame/forums/base/forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from django import forms
from machina.apps.forum.models import Forum


class ForumForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
kwargs.setdefault("label_suffix", "")
super().__init__(*args, **kwargs)

class Meta:
model = Forum
fields = ["name", "description", "image", "type"]
labels = {"image": "Upload forum image"}
widgets = {
"description": forms.Textarea(attrs={"rows": 4}),
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@
from django.contrib.messages.views import SuccessMessageMixin
from django.urls import reverse
from django.views.generic import CreateView
from machina.apps.forum.models import Forum

from .forms import ForumForm

class ForumCreateView(LoginRequiredMixin, SuccessMessageMixin, CreateView):
model = Forum

class ForumCreateView(LoginRequiredMixin, SuccessMessageMixin, CreateView):
form_class = ForumForm
success_message = "Forum successfully created!"
template_name = "forum_form.html"
fields = ["name", "description", "image", "type"]

def get_success_url(self):
return reverse("forum:index")
Empty file.
Empty file.
6 changes: 6 additions & 0 deletions src/chigame/forums/forum_conversation/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from machina.apps.forum_conversation.apps import ForumConversationAppConfig as BaseForumConversationAppConfig


class ForumConversationConfig(BaseForumConversationAppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "chigame.forum_conversation"
77 changes: 77 additions & 0 deletions src/chigame/forums/forum_conversation/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations
from django.conf import settings
import machina.models.fields


class Migration(migrations.Migration):

dependencies = [
('forum', '0001_initial'),
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]

operations = [
migrations.CreateModel(
name='Post',
fields=[
('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
('created', models.DateTimeField(auto_now_add=True, verbose_name='Creation date')),
('updated', models.DateTimeField(auto_now=True, verbose_name='Update date')),
('poster_ip', models.GenericIPAddressField(default='2002::0', null=True, verbose_name='Poster IP address', blank=True)),
('subject', models.CharField(max_length=255, verbose_name='Subject')),
('content', machina.models.fields.MarkupTextField(no_rendered_field=True, verbose_name='Content')),
('username', models.CharField(max_length=155, null=True, verbose_name='Username', blank=True)),
('approved', models.BooleanField(default=True, verbose_name='Approved')),
('update_reason', models.CharField(max_length=255, null=True, verbose_name='Update reason', blank=True)),
('updates_count', models.PositiveIntegerField(default=0, verbose_name='Updates count', editable=False, blank=True)),
('_content_rendered', models.TextField(null=True, editable=False, blank=True)),
('poster', models.ForeignKey(related_name='posts', verbose_name='Poster', blank=True, to=settings.AUTH_USER_MODEL, null=True, on_delete=models.CASCADE)),
],
options={
'ordering': ['created'],
'abstract': False,
'get_latest_by': 'created',
'verbose_name': 'Post',
'verbose_name_plural': 'Posts',
},
),
migrations.CreateModel(
name='Topic',
fields=[
('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
('created', models.DateTimeField(auto_now_add=True, verbose_name='Creation date')),
('updated', models.DateTimeField(auto_now=True, verbose_name='Update date')),
('subject', models.CharField(max_length=255, verbose_name='Subject')),
('slug', models.SlugField(max_length=255, verbose_name='Slug')),
('type', models.PositiveSmallIntegerField(db_index=True, verbose_name='Topic type', choices=[(0, 'Default topic'), (1, 'Sticky'), (2, 'Announce')])),
('status', models.PositiveIntegerField(db_index=True, verbose_name='Topic status', choices=[(0, 'Topic unlocked'), (1, 'Topic locked'), (2, 'Topic moved')])),
('approved', models.BooleanField(default=True, verbose_name='Approved')),
('posts_count', models.PositiveIntegerField(default=0, verbose_name='Posts count', editable=False, blank=True)),
('views_count', models.PositiveIntegerField(default=0, verbose_name='Views count', editable=False, blank=True)),
('last_post_on', models.DateTimeField(null=True, verbose_name='Last post added on', blank=True)),
('forum', models.ForeignKey(related_name='topics', verbose_name='Topic forum', to='forum.Forum', on_delete=models.CASCADE)),
('poster', models.ForeignKey(verbose_name='Poster', blank=True, to=settings.AUTH_USER_MODEL, null=True, on_delete=models.CASCADE)),
('subscribers', models.ManyToManyField(related_name='subscriptions', verbose_name='Subscribers', to=settings.AUTH_USER_MODEL, blank=True)),
],
options={
'ordering': ['-type', '-last_post_on'],
'abstract': False,
'get_latest_by': 'last_post_on',
'verbose_name': 'Topic',
'verbose_name_plural': 'Topics',
},
),
migrations.AddField(
model_name='post',
name='topic',
field=models.ForeignKey(related_name='posts', verbose_name='Topic', to='forum_conversation.Topic', on_delete=models.CASCADE),
),
migrations.AddField(
model_name='post',
name='updated_by',
field=models.ForeignKey(blank=True, editable=False, to=settings.AUTH_USER_MODEL, null=True, verbose_name='Lastly updated by', on_delete=models.SET_NULL),
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('forum_conversation', '0001_initial'),
]

operations = [
migrations.AddField(
model_name='post',
name='anonymous_key',
field=models.CharField(max_length=100, null=True, verbose_name='Anonymous user forum key', blank=True),
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.9.2 on 2016-02-28 19:51
from __future__ import unicode_literals

from django.db import migrations
import machina.core.validators
import machina.models.fields


class Migration(migrations.Migration):

dependencies = [
('forum_conversation', '0002_post_anonymous_key'),
]

operations = [
migrations.AlterField(
model_name='post',
name='content',
field=machina.models.fields.MarkupTextField(no_rendered_field=True, validators=[machina.core.validators.NullableMaxLengthValidator(None)], verbose_name='Content'),
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.9.3 on 2016-04-27 03:02
from __future__ import unicode_literals

from django.conf import settings
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('forum_conversation', '0003_auto_20160228_2051'),
]

operations = [
migrations.AlterField(
model_name='topic',
name='subscribers',
field=models.ManyToManyField(blank=True, related_name='topic_subscriptions', to=settings.AUTH_USER_MODEL, verbose_name='Subscribers'),
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.9.6 on 2016-06-07 02:55
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('forum_conversation', '0004_auto_20160427_0502'),
]

operations = [
migrations.AlterField(
model_name='post',
name='approved',
field=models.BooleanField(db_index=True, default=True, verbose_name='Approved'),
),
migrations.AlterField(
model_name='topic',
name='approved',
field=models.BooleanField(db_index=True, default=True, verbose_name='Approved'),
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.9.7 on 2016-07-09 04:16
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('forum_conversation', '0005_auto_20160607_0455'),
]

operations = [
migrations.AddField(
model_name='post',
name='enable_signature',
field=models.BooleanField(db_index=True, default=True, verbose_name='Attach a signature'),
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.10.1 on 2016-09-03 02:50
from __future__ import unicode_literals

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
('forum_conversation', '0006_post_enable_signature'),
]

operations = [
migrations.AddField(
model_name='topic',
name='first_post',
field=models.ForeignKey(blank=True, editable=False, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='+', to='forum_conversation.Post', verbose_name='Last post'),
),
migrations.AddField(
model_name='topic',
name='last_post',
field=models.ForeignKey(blank=True, editable=False, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='+', to='forum_conversation.Post', verbose_name='Last post'),
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.10.1 on 2016-09-03 03:12
from __future__ import unicode_literals

from django.db import migrations


def update_topic_first_post_last_post(apps, schema_editor):
Topic = apps.get_model('forum_conversation', 'Topic')
for topic in Topic.objects.all():
first_post = topic.posts.all().order_by('created').first()
last_post = topic.posts.filter(approved=True).order_by('-created').first()
topic.first_post = first_post
topic.last_post = last_post
topic.save()

def reverse_topic_first_post_last_post(apps, schema_editor):
pass


class Migration(migrations.Migration):

dependencies = [
('forum_conversation', '0007_auto_20160903_0450'),
]

operations = [
migrations.RunPython(
update_topic_first_post_last_post, reverse_code=reverse_topic_first_post_last_post),
]
Loading

0 comments on commit ae99927

Please sign in to comment.