Move repo

This commit is contained in:
willy 2026-06-20 07:06:19 +02:00
parent fc35b28704
commit 9c7dd935da
5 changed files with 48 additions and 11 deletions

View file

@ -3,9 +3,14 @@
from rest_framework import generics from rest_framework import generics
from rest_framework.parsers import MultiPartParser, FormParser, JSONParser from rest_framework.parsers import MultiPartParser, FormParser, JSONParser
from rest_framework.filters import SearchFilter from rest_framework.filters import SearchFilter
from rest_framework.response import Response
from rest_framework import status
from rest_framework.views import APIView
from django.shortcuts import get_object_or_404
from .permissions import IsAuthenticatedOrReadOnly from .permissions import IsAuthenticatedOrReadOnly
from .models import Plant from .models import Plant, PlantImage
from .serializers import PlantSerializer from .serializers import PlantSerializer
# List and Create Plants # List and Create Plants
@ -22,4 +27,14 @@ class PlantRetrieveUpdateDestroyAPIView(generics.RetrieveUpdateDestroyAPIView):
queryset = Plant.objects.all() queryset = Plant.objects.all()
serializer_class = PlantSerializer serializer_class = PlantSerializer
parser_classes = [JSONParser, MultiPartParser, FormParser] # Allow file uploads and form data parser_classes = [JSONParser, MultiPartParser, FormParser] # Allow file uploads and form data
permission_classes = [IsAuthenticatedOrReadOnly] # Apply custom permission permission_classes = [IsAuthenticatedOrReadOnly] # Apply custom permission
class PlantImageDeleteAPIView(APIView):
permission_classes = [IsAuthenticatedOrReadOnly] # Ensure only authenticated users can delete
def delete(self, request, image_id):
image = get_object_or_404(PlantImage, id=image_id)
image.delete() # Delete the image object from the database
return Response({"message": "Image deleted successfully"}, status=status.HTTP_204_NO_CONTENT)

0
plant_catalog/catalog/permissions.py Normal file → Executable file
View file

View file

@ -8,13 +8,34 @@ class PlantImageSerializer(serializers.ModelSerializer):
class PlantSerializer(serializers.ModelSerializer): class PlantSerializer(serializers.ModelSerializer):
images = PlantImageSerializer(many=True, read_only=True) # Include related images images = PlantImageSerializer(many=True, read_only=True) # Read existing images
uploaded_images = serializers.ListField(child=serializers.ImageField(), write_only=True, required=False) # Accept new images
class Meta: class Meta:
model = Plant model = Plant
fields = ['id', 'name', 'description', 'buy_price', 'sell_price', 'stock', 'images'] # Add the `images` field fields = ['id', 'name', 'description', 'buy_price', 'sell_price', 'stock', 'images', 'uploaded_images'] # Include uploaded_images
extra_kwargs = {
'buy_price': {'required': False, 'allow_null': True}, def create(self, validated_data):
'sell_price': {'required': False, 'allow_null': True}, uploaded_images = validated_data.pop('uploaded_images', []) # Get images from request
'description': {'required': False, 'allow_null': True}, plant = Plant.objects.create(**validated_data)
}
for image in uploaded_images:
PlantImage.objects.create(plant=plant, image=image) # Save each image
return plant
def update(self, instance, validated_data):
uploaded_images = validated_data.pop('uploaded_images', [])
instance.name = validated_data.get('name', instance.name)
instance.description = validated_data.get('description', instance.description)
instance.buy_price = validated_data.get('buy_price', instance.buy_price)
instance.sell_price = validated_data.get('sell_price', instance.sell_price)
instance.stock = validated_data.get('stock', instance.stock)
instance.save()
for image in uploaded_images:
PlantImage.objects.create(plant=instance, image=image) # Save new images
return instance

View file

@ -3,7 +3,7 @@
from django.http import HttpResponseRedirect from django.http import HttpResponseRedirect
from django.urls import path from django.urls import path
from rest_framework_simplejwt.views import TokenObtainPairView, TokenRefreshView from rest_framework_simplejwt.views import TokenObtainPairView, TokenRefreshView
from .api_views import PlantListCreateAPIView, PlantRetrieveUpdateDestroyAPIView from .api_views import PlantListCreateAPIView, PlantRetrieveUpdateDestroyAPIView, PlantImageDeleteAPIView
urlpatterns = [ urlpatterns = [
path('', lambda request: HttpResponseRedirect('/api/plants/')), path('', lambda request: HttpResponseRedirect('/api/plants/')),
@ -11,6 +11,7 @@ urlpatterns = [
path('api/plants/<int:pk>/', PlantRetrieveUpdateDestroyAPIView.as_view(), name='plant_detail_update_delete'), # Retrieve, Update, Delete path('api/plants/<int:pk>/', PlantRetrieveUpdateDestroyAPIView.as_view(), name='plant_detail_update_delete'), # Retrieve, Update, Delete
path('api/token/', TokenObtainPairView.as_view(), name='token_obtain_pair'), path('api/token/', TokenObtainPairView.as_view(), name='token_obtain_pair'),
path('api/token/refresh/', TokenRefreshView.as_view(), name='token_refresh'), path('api/token/refresh/', TokenRefreshView.as_view(), name='token_refresh'),
path('api/plant-images/<int:image_id>/', PlantImageDeleteAPIView.as_view(), name='plant_image_delete'),
] ]

View file

@ -26,7 +26,7 @@ SECRET_KEY = 'django-insecure-(e9sx(r=75hv%xni#rcm4)0&67c34+her^!%7dqlghem1=l0lc
# SECURITY WARNING: don't run with debug turned on in production! # SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False DEBUG = False
ALLOWED_HOSTS = ['gonzalohd.eu', 'plantsapi.gonzalohd.eu', 'localhost', '192.168.1.139', '127.0.0.1'] ALLOWED_HOSTS = ['gonzalohd.com', 'plantsapi.gonzalohd.com', 'localhost', '192.168.1.139', '127.0.0.1']
# ALLOWED_HOSTS = ['*'] # ALLOWED_HOSTS = ['*']