parent
e1249b3b69
commit
39dab848ae
@ -0,0 +1,3 @@
|
||||
from django.contrib import admin
|
||||
|
||||
# Register your models here.
|
@ -0,0 +1,6 @@
|
||||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class ActivitiesConfig(AppConfig):
|
||||
default_auto_field = 'django.db.models.BigAutoField'
|
||||
name = 'activities'
|
@ -0,0 +1,25 @@
|
||||
from django.conf import settings
|
||||
from django.db import models
|
||||
|
||||
# Create your models here.
|
||||
|
||||
|
||||
class Photo(models.Model):
|
||||
image = models.ImageField(upload_to='users/photos/')
|
||||
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name='gallery')
|
||||
|
||||
|
||||
class FavouriteOrganization(models.Model):
|
||||
organization = models.ForeignKey('organizations.Organization', on_delete=models.CASCADE)
|
||||
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
|
||||
|
||||
|
||||
# class Test(models.Model):
|
||||
# title = models.CharField(max_length=64)
|
||||
# image = models.ImageField(upload_to='images/tests/')
|
||||
#
|
||||
#
|
||||
# class Bonus(models.Model):
|
||||
# user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
|
||||
# amount = models.IntegerField()
|
||||
# descr = models.TextField()
|
@ -0,0 +1,14 @@
|
||||
from core.repositories import BaseRepository
|
||||
from .models import Photo, FavouriteOrganization
|
||||
|
||||
|
||||
class PhotoRepository(BaseRepository):
|
||||
model = Photo
|
||||
|
||||
|
||||
class FavouriteOrganizationRepository(BaseRepository):
|
||||
model = FavouriteOrganization
|
||||
|
||||
@classmethod
|
||||
def get_filtered(cls, **kwargs):
|
||||
return cls.model.objects.filter(**kwargs)
|
@ -0,0 +1,27 @@
|
||||
from rest_framework import serializers
|
||||
|
||||
from organizations.repositories import OrganizationRepository
|
||||
from .models import Bonus, FavouriteOrganization, Photo
|
||||
|
||||
|
||||
class PhotoSerializer(serializers.ModelSerializer):
|
||||
user = serializers.StringRelatedField()
|
||||
|
||||
class Meta:
|
||||
model = Photo
|
||||
fields = ('image', 'user')
|
||||
|
||||
|
||||
class ListPhotoSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = Photo
|
||||
fields = ('image',)
|
||||
|
||||
|
||||
class FavouriteOrganizationSerializer(serializers.ModelSerializer):
|
||||
organization = serializers.PrimaryKeyRelatedField(queryset=OrganizationRepository.all(), read_only=False)
|
||||
user = serializers.StringRelatedField()
|
||||
|
||||
class Meta:
|
||||
model = FavouriteOrganization
|
||||
fields = ('organization', 'user')
|
@ -0,0 +1,3 @@
|
||||
from django.test import TestCase
|
||||
|
||||
# Create your tests here.
|
@ -0,0 +1,9 @@
|
||||
from django.urls import path
|
||||
|
||||
from .views import PhotoListCreateAPIView, FavouriteOrganizationAPIView
|
||||
|
||||
|
||||
urlpatterns = [
|
||||
path('gallery/', PhotoListCreateAPIView.as_view()),
|
||||
path('wishlist/', FavouriteOrganizationAPIView.as_view())
|
||||
]
|
@ -0,0 +1,43 @@
|
||||
from django.shortcuts import get_object_or_404
|
||||
from rest_framework.generics import ListAPIView, CreateAPIView, DestroyAPIView
|
||||
from rest_framework.pagination import PageNumberPagination
|
||||
|
||||
from core.permissions import IsAuthorOrReadOnly
|
||||
from organizations.repositories import OrganizationRepository
|
||||
from .repositories import PhotoRepository, FavouriteOrganizationRepository
|
||||
from .serializers import PhotoSerializer, ListPhotoSerializer
|
||||
|
||||
# Create your views here.
|
||||
|
||||
|
||||
class PhotoListCreateAPIView(ListAPIView, CreateAPIView):
|
||||
queryset = PhotoRepository.all()
|
||||
pagination_class = PageNumberPagination
|
||||
permission_classes = [IsAuthorOrReadOnly]
|
||||
|
||||
def get_serializer_class(self):
|
||||
if self.request.POST:
|
||||
return PhotoSerializer
|
||||
else:
|
||||
return ListPhotoSerializer
|
||||
|
||||
def perform_create(self, serializer):
|
||||
serializer.save(user=self.request.user)
|
||||
|
||||
|
||||
class FavouriteOrganizationAPIView(ListAPIView, CreateAPIView, DestroyAPIView):
|
||||
|
||||
def get_queryset(self):
|
||||
return FavouriteOrganizationRepository.get_filtered(user=self.request.user)
|
||||
|
||||
def get_object(self):
|
||||
queryset = self.get_queryset()
|
||||
obj = get_object_or_404(queryset, user=self.request.user, organization__pk=self.kwargs.get('id'))
|
||||
return obj
|
||||
|
||||
def perform_create(self, serializer):
|
||||
if FavouriteOrganizationRepository.get_filtered(user=self.request.user, organization=OrganizationRepository.get(
|
||||
self.kwargs.get('id'))):
|
||||
pass
|
||||
else:
|
||||
serializer.save(user=self.request.user, organization=self.kwargs.get('id'))
|
Loading…
Reference in new issue