Updates for the event images

This commit is contained in:
Vivek P Prakash
2025-12-01 04:52:49 +05:30
parent 392f86cfaf
commit d96ba46506
24 changed files with 905 additions and 257 deletions

View File

3
mobile_web_api/admin.py Normal file
View File

@@ -0,0 +1,3 @@
from django.contrib import admin
# Register your models here.

6
mobile_web_api/apps.py Normal file
View File

@@ -0,0 +1,6 @@
from django.apps import AppConfig
class MobileWebApiConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'mobile_web_api'

View File

@@ -0,0 +1 @@
from .user_forms import *

View File

View File

@@ -0,0 +1,42 @@
# accounts/forms.py
from django import forms
from django.contrib.auth import get_user_model
from django.contrib.auth import authenticate
User = get_user_model()
class RegisterForm(forms.ModelForm):
password = forms.CharField(widget=forms.PasswordInput)
class Meta:
model = User
fields = ['email', 'phone_number', 'password']
def clean_email(self):
phone_number = self.cleaned_data.get('phone_number')
if User.objects.filter(phone_number=phone_number).exists():
raise forms.ValidationError("phone_number is already registered.")
return phone_number
def save(self, commit=True):
user = super().save(commit=False)
user.set_password(self.cleaned_data['password'])
if commit:
user.save()
return user
class LoginForm(forms.Form):
username = forms.CharField()
password = forms.CharField(widget=forms.PasswordInput)
def clean(self):
cleaned_data = super().clean()
username = cleaned_data.get('username')
password = cleaned_data.get('password')
user = authenticate(username=username, password=password)
if not user:
raise forms.ValidationError("Invalid credentials.")
cleaned_data['user'] = user
return cleaned_data

View File

3
mobile_web_api/models.py Normal file
View File

@@ -0,0 +1,3 @@
from django.db import models
# Create your models here.

3
mobile_web_api/tests.py Normal file
View File

@@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.

21
mobile_web_api/urls.py Normal file
View File

@@ -0,0 +1,21 @@
from django.urls import path
from .views import *
# User URLS
urlpatterns = [
path('user/register/', RegisterView.as_view(), name='json_register'),
path('user/login/', LoginView.as_view(), name='json_login'),
path('user/status/', StatusView.as_view(), name='user_status'),
path('user/logout/', LogoutView.as_view(), name='user_logout'),
]
# Event URLS
urlpatterns += [
path('events/type-list/', EventTypeListAPIView.as_view()),
path('events/pincode-events/', EventListAPI.as_view()),
path('events/event-details/', EventDetailAPI.as_view()),
path('events/<int:event_id>/images/', EventImagesListAPI.as_view()),
]

View File

@@ -0,0 +1,2 @@
from .user import *
from .events import *

View File

@@ -0,0 +1,162 @@
from django.http import JsonResponse
from rest_framework.views import APIView
from rest_framework.authentication import TokenAuthentication
from rest_framework.permissions import IsAuthenticated
from events.models import Event, EventImages
from rest_framework.authtoken.models import Token
from master_data.models import EventType
from django.forms.models import model_to_dict
from django.utils.decorators import method_decorator
from django.views.decorators.csrf import csrf_exempt
import json
@method_decorator(csrf_exempt, name='dispatch')
class EventTypeListAPIView(APIView):
def post(self, request):
try:
# Manually load JSON because we are not using parsers
data = json.loads(request.body)
token_key = data.get("token")
username = data.get("username")
if not token_key or not username:
return JsonResponse(
{"status": "error", "message": "token and username required"}
)
try:
token = Token.objects.get(key=token_key)
user = token.user
if user.username != username:
return JsonResponse(
{"status": "error", "message": "token does not match user"}
)
# Fetch event types manually without serializer
event_types = list(EventType.objects.values("id", "event_type"))
return JsonResponse({
"status": "success",
"event_types": event_types
})
except Token.DoesNotExist:
return JsonResponse({"status": "invalid_token"})
except json.JSONDecodeError:
return JsonResponse(
{"status": "error", "message": "Invalid JSON"}
)
except Exception as e:
return JsonResponse(
{"status": "error", "message": str(e)},
)
class EventListAPI(APIView):
def post(self, request):
try:
data = json.loads(request.body)
token_key = data.get("token")
username = data.get("username")
pincode = data.get("pincode")
if not token_key or not username:
return JsonResponse(
{"status": "error", "message": "token and username required"}
)
try:
token = Token.objects.get(key=token_key)
user = token.user
if user.username != username:
return JsonResponse(
{"status": "error", "message": "token does not match user"}
)
events = Event.objects.filter(pincode=pincode).order_by('-created_date')
event_list = []
for e in events:
event_list.append(model_to_dict(e))
return JsonResponse({
"status": "success",
"events": event_list
})
except Token.DoesNotExist:
return JsonResponse({"status": "invalid_token"})
except json.JSONDecodeError:
return JsonResponse(
{"status": "error", "message": "Invalid JSON"}
)
except Exception as e:
return JsonResponse(
{"status": "error", "message": str(e)},
)
class EventDetailAPI(APIView):
def post(self, request):
try:
data = json.loads(request.body)
token_key = data.get("token")
username = data.get("username")
event_id = data.get("event_id")
if not token_key or not username:
return JsonResponse(
{"status": "error", "message": "token and username required"}
)
try:
token = Token.objects.get(key=token_key)
user = token.user
if user.username != username:
return JsonResponse(
{"status": "error", "message": "token does not match user"}
)
events = Event.objects.get(id=event_id)
data = model_to_dict(events)
data["status"] = "success"
return JsonResponse(data)
except Token.DoesNotExist:
return JsonResponse({"status": "invalid_token"})
except json.JSONDecodeError:
return JsonResponse(
{"status": "error", "message": "Invalid JSON"}
)
except Exception as e:
return JsonResponse(
{"status": "error", "message": str(e)},
)
class EventImagesListAPI(APIView):
authentication_classes = [TokenAuthentication]
permission_classes = [IsAuthenticated]
def get(self, request, event_id):
images = EventImages.objects.filter(event_id=event_id)
image_list = [model_to_dict(i) for i in images]
return JsonResponse({"status": True, "images": image_list}, safe=False)

View File

@@ -0,0 +1,124 @@
# accounts/views.py
import json
from django.views.decorators.csrf import csrf_exempt
from django.http import JsonResponse
from django.utils.decorators import method_decorator
from django.views import View
from rest_framework.authtoken.models import Token
from mobile_web_api.forms import RegisterForm, LoginForm
from rest_framework.authentication import TokenAuthentication
from django.contrib.auth import logout
@method_decorator(csrf_exempt, name='dispatch')
class RegisterView(View):
def post(self, request):
try:
data = json.loads(request.body)
form = RegisterForm(data)
if form.is_valid():
user = form.save()
token, _ = Token.objects.get_or_create(user=user)
return JsonResponse({'message': 'User registered successfully', 'token': token.key}, status=201)
return JsonResponse({'errors': form.errors}, status=400)
except Exception as e:
return JsonResponse({'error': str(e)}, status=500)
@method_decorator(csrf_exempt, name='dispatch')
class LoginView(View):
def post(self, request):
try:
data = json.loads(request.body)
form = LoginForm(data)
if form.is_valid():
user = form.cleaned_data['user']
token, _ = Token.objects.get_or_create(user=user)
return JsonResponse({'message': 'Login successful', 'token': token.key})
return JsonResponse({'errors': form.errors}, status=401)
except Exception as e:
return JsonResponse({'error': str(e)}, status=500)
@method_decorator(csrf_exempt, name='dispatch')
class StatusView(View):
def post(self, request):
try:
data = json.loads(request.body)
token_key = data.get("token")
username = data.get("username")
if not token_key or not username:
return JsonResponse(
{"status": "error", "message": "token and username required"},
status=400
)
try:
token = Token.objects.get(key=token_key)
if token.user.username != username:
return JsonResponse(
{"status": "error", "message": "token does not match user"},
status=401
)
return JsonResponse({
"status": "logged_in",
"username": token.user.username,
"email": token.user.email
})
except Token.DoesNotExist:
return JsonResponse({"status": "invalid_token"}, status=401)
except json.JSONDecodeError:
return JsonResponse({"status": "error", "message": "Invalid JSON"}, status=400)
except Exception as e:
return JsonResponse({"status": "error", "message": str(e)}, status=500)
@method_decorator(csrf_exempt, name='dispatch')
class LogoutView(View):
def post(self, request):
try:
data = json.loads(request.body)
token_key = data.get("token")
username = data.get("username")
if not token_key or not username:
return JsonResponse(
{"status": "error", "message": "token and username required"},
status=400
)
try:
token = Token.objects.get(key=token_key)
user = token.user
if user.username != username:
return JsonResponse(
{"status": "error", "message": "token does not match user"},
status=401
)
# 🔍 Call Django's built-in logout
logout(request)
# 🗑 Delete the token to invalidate future access
token.delete()
return JsonResponse({
"status": "logged_out",
"message": "Logout successful"
})
except Token.DoesNotExist:
return JsonResponse({"status": "invalid_token"}, status=401)
except json.JSONDecodeError:
return JsonResponse({"status": "error", "message": "Invalid JSON"}, status=400)
except Exception as e:
return JsonResponse({"status": "error", "message": str(e)}, status=500)