Skip to content

Commit

Permalink
add groups
Browse files Browse the repository at this point in the history
  • Loading branch information
linkin committed Nov 17, 2024
1 parent eba2f1f commit edb0435
Show file tree
Hide file tree
Showing 24 changed files with 274 additions and 26 deletions.
9 changes: 9 additions & 0 deletions justhink_client/src/API/GroupService.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import api from "../api";

export default class GroupService {
static async getAllUsersGroups() {
const response = await api.get("/api/groups/");
return response;
}
}

20 changes: 20 additions & 0 deletions justhink_client/src/components/UI/Modal/Modal.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import React from "react";
import cl from "./Modal.module.css";

const Modal = ({ children, visible, setVisible }) => {
const rootClasses = [cl.myModal];

if (visible) {
rootClasses.push(cl.active);
}

return (
<div className={rootClasses.join(" ")} onClick={() => setVisible(false)}>
<div className={cl.myModalContent} onClick={(e) => e.stopPropagation()}>
{children}
</div>
</div>
);
};

export default Modal;
22 changes: 22 additions & 0 deletions justhink_client/src/components/UI/Modal/Modal.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
.myModal {
position: fixed;
top: 0;
bottom: 0;
right: 0;
left: 0;
display: none;
background: rgba(0, 0, 0, 0.5);
}

.myModalContent {
padding: 25px;
background: white;
border-radius: 16px;
min-width: 250px;
}

.myModal.active {
display: flex;
justify-content: center;
align-items: center;
}
19 changes: 18 additions & 1 deletion justhink_client/src/pages/DeckId.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,41 @@ import CardSlider from "../components/UI/CardSlider/CardSlider";
import DeckService from "../API/DeckService";
import { useParams } from "react-router-dom";
import { useFetching } from "../hooks/useFetching";
import Modal from "../components/UI/Modal/Modal";
import GroupService from "../API/GroupService";

const DeckId = () => {
const params = useParams();
const [deck, setDeck] = useState({});
const [modal, setModal] = useState(false);
const [groups, setGroups] = useState([]);

const [fetchDeckById, isLoading, error] = useFetching(async () => {
const response = await DeckService.getById(params.id);
setDeck(response.data);
});

const [fetchGroups, grLoading, grError] = useFetching(async () => {
const response = await GroupService.getAllUsersGroups();
setGroups(response.data);
});

useEffect(() => {
fetchDeckById();
fetchGroups();
}, []);
return (
<div>
{/* <CardSlider /> */}
{deck.name}
<button>Играть</button>
<button onClick={() => setModal(true)}>Играть</button>
<Modal visible={modal} setVisible={setModal}>
{groups.map((group, id) => (
<div key={id}>
<h5>{group.name}</h5>
</div>
))}
</Modal>
</div>
);
};
Expand Down
2 changes: 2 additions & 0 deletions justhink_client/src/router/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,7 @@ export const routes = [
{ path: "/register", element: <RegisterAndLogout /> },
{ path: "/decks", element: <Decks /> },
{ path: "/decks/:id", element: <DeckId /> },
// { path: "/groups", element: <Decks /> },
// { path: "/groups/:id", element: <DeckId /> },
{ path: "/lobby/:uid", element: <Lobby /> },
];
3 changes: 0 additions & 3 deletions justhink_server/justhink/auth/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ class Meta:
extra_kwargs = {"password": {"write_only": True}}

def create(self, validated_data):
# user = core.models.User.objects.create(**validated_data)
# return user

user = core.models.User(username=validated_data["username"])
user.set_password(validated_data["password"])
user.save()
Expand Down
6 changes: 3 additions & 3 deletions justhink_server/justhink/cards/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ class Card(django.db.models.Model):
on_delete=django.db.models.CASCADE,
)

def __str__(self):
return self.text

class Meta:
verbose_name = "карта"
verbose_name_plural = "карты"

def __str__(self):
return self.text


__all__ = [Card]
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Generated by Django 5.1.1 on 2024-11-16 11:42

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


class Migration(migrations.Migration):

dependencies = [
("core", "0002_alter_user_username"),
]

operations = [
migrations.AlterModelOptions(
name="user",
options={
"verbose_name": "пользователь",
"verbose_name_plural": "пользователи",
},
),
migrations.AddField(
model_name="user",
name="friends",
field=models.ManyToManyField(
blank=True, to=settings.AUTH_USER_MODEL
),
),
]
26 changes: 11 additions & 15 deletions justhink_server/justhink/core/models.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,8 @@
import pathlib
import uuid

import django.contrib.auth
import django.core.exceptions
import django.db.models


def get_path_image(instance, filename):
file_extension = pathlib.Path(filename).suffix
return f"users/{uuid.uuid4()}{file_extension}"


class User(django.contrib.auth.models.AbstractUser):
username = django.db.models.CharField(
verbose_name="никнейм",
Expand All @@ -24,10 +16,14 @@ class User(django.contrib.auth.models.AbstractUser):
null=True,
blank=True,
)
# image = django.db.models.ImageField(
# "аватарка",
# help_text="Загрузите аватарку",
# upload_to=get_path_image,
# null=True,
# blank=True,
# )
friends = django.db.models.ManyToManyField(
"self",
blank=True,
)

class Meta:
verbose_name = "пользователь"
verbose_name_plural = "пользователи"

def __str__(self):
return self.username
6 changes: 3 additions & 3 deletions justhink_server/justhink/deck/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ class Languages(models.TextChoices):
related_name="decks",
)

def __str__(self):
return self.name

class Meta:
verbose_name = "колода"
verbose_name_plural = "колоды"

def __str__(self):
return self.name


__all__ = [Deck]
Empty file.
14 changes: 14 additions & 0 deletions justhink_server/justhink/groups/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import django.contrib.admin

import groups.models


@django.contrib.admin.register(groups.models.Group)
class AdminGroup(django.contrib.admin.ModelAdmin):
list_display = (
groups.models.Group.name.field.name,
groups.models.Group.created_at.field.name,
)


__all__ = []
7 changes: 7 additions & 0 deletions justhink_server/justhink/groups/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from django.apps import AppConfig


class GroupsConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "groups"
verbose_name = "Группы"
52 changes: 52 additions & 0 deletions justhink_server/justhink/groups/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Generated by Django 5.1.1 on 2024-11-16 12:16

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


class Migration(migrations.Migration):

initial = True

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]

operations = [
migrations.CreateModel(
name="Group",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
(
"name",
models.CharField(
blank=True,
help_text="Придумайте название",
max_length=255,
verbose_name="название",
),
),
("created_at", models.DateTimeField(auto_now_add=True)),
(
"users",
models.ManyToManyField(
blank=True,
related_name="user_groups",
to=settings.AUTH_USER_MODEL,
),
),
],
options={
"verbose_name": "Группа",
"verbose_name_plural": "Группы",
},
),
]
Empty file.
27 changes: 27 additions & 0 deletions justhink_server/justhink/groups/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import django.db.models

import core.models


class Group(django.db.models.Model):
name = django.db.models.CharField(
verbose_name="название",
help_text="Придумайте название",
max_length=255,
blank=True,
)
users = django.db.models.ManyToManyField(
core.models.User,
blank=True,
related_name="user_groups",
)
created_at = django.db.models.DateTimeField(
auto_now_add=True,
)

class Meta:
verbose_name = "Группа"
verbose_name_plural = "Группы"

def __str__(self):
return self.name
6 changes: 6 additions & 0 deletions justhink_server/justhink/groups/permissions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from rest_framework import permissions


class IsGroupParticipant(permissions.BasePermission):
def has_object_permission(self, request, view, obj):
return obj.users.filter(id=request.user.id).exists()
18 changes: 18 additions & 0 deletions justhink_server/justhink/groups/serializers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import rest_framework.serializers

import groups.models


class GroupSerializer(rest_framework.serializers.ModelSerializer):
class Meta:
model = groups.models.Group
fields = (groups.models.Group.name.field.name,)

def create(self, validated_data):
user = self.context["request"].user
group = groups.models.Group.objects.create(**validated_data)
group.users.add(user)
return group


__all__ = [GroupSerializer]
3 changes: 3 additions & 0 deletions justhink_server/justhink/groups/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.test import TestCase

# Create your tests here.
23 changes: 23 additions & 0 deletions justhink_server/justhink/groups/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import rest_framework.generics
import rest_framework.viewsets
import rest_framework.decorators
import rest_framework.permissions

import groups.models
import groups.serializers
import groups.permissions


class GroupViewSet(rest_framework.viewsets.ModelViewSet):
queryset = groups.models.Group.objects.all()
serializer_class = groups.serializers.GroupSerializer
permission_classes = [
rest_framework.permissions.IsAuthenticated,
groups.permissions.IsGroupParticipant,
]

def get_queryset(self):
return groups.models.Group.objects.filter(users=self.request.user)


__all__ = [GroupViewSet]
3 changes: 3 additions & 0 deletions justhink_server/justhink/justhink/routers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import deck.views
import cards.views
import groups.views


deck_router = routers.SimpleRouter()
Expand All @@ -10,3 +11,5 @@
card_router = routers.SimpleRouter()
card_router.register(r"card", cards.views.CardViewSet)

group_router = routers.SimpleRouter()
group_router.register(r"groups", groups.views.GroupViewSet)
1 change: 1 addition & 0 deletions justhink_server/justhink/justhink/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"deck.apps.DeckConfig",
"lobby.apps.LobbyConfig",
"users.apps.UsersConfig",
"groups.apps.GroupsConfig",
"core.apps.CoreConfig",
"rest_framework",
"corsheaders",
Expand Down
Loading

0 comments on commit edb0435

Please sign in to comment.