connect / api /views.py
thejagstudio's picture
Refactor bhajanDetail view and URL pattern for improved clarity and consistency
6b9394e
from django.views.decorators.csrf import csrf_exempt
from django.shortcuts import render, get_object_or_404, redirect
from django.contrib.auth.decorators import login_required, user_passes_test
from django.http import JsonResponse, HttpResponse
from django.views.decorators.http import require_POST
from django.utils import timezone
import json
from .models import (
Bhagat,
Event,
Attendance,
PushSubscription,
Notification,
Region,
BhajanCategory,
Bhajan,
OptionPoll,
Poll,
)
from django.conf import settings
from django.core import serializers
import requests
from rest_framework.decorators import api_view, permission_classes
from rest_framework.permissions import AllowAny
from rest_framework.response import Response
from .utils import jwt_required
from rest_framework_simplejwt.tokens import RefreshToken
from pywebpush import webpush, WebPushException
def gCaptchaVerifer(token):
secret_key = settings.RECAPTCHA_SECRET_KEY
data = {"response": token, "secret": secret_key}
resp = requests.post("https://www.google.com/recaptcha/api/siteverify", data=data)
return resp.json()
def is_superadmin(user):
return user.user_type == "superadmin"
def is_regionadmin(user):
return user.user_type == "regionadmin"
def is_monitor(user):
return user.user_type == "monitor"
def dataEntry(request):
# with open("./api/bhajanData.json", "r",encoding="utf-8") as f:
# data = json.load(f)["Prasang"]
# for bhajan in data:
# category = BhajanCategory.objects.filter(link=bhajan['CatId']).first()
# Bhajan.objects.create(
# title=bhajan['title'],
# title_guj=bhajan['title_guj'],
# category=category,
# lyrics=bhajan['lyrics'],
# isEng = bhajan['isEng'],
# isHnd = bhajan['isHnd'],
# isGer = bhajan['isGer'],
# isAudio = bhajan['isAudio'],
# audio_url=bhajan['audio_url'] if bhajan['isAudio'] else ""
# )
# print(bhajan['title'])
return HttpResponse("Data Entry Page")
def bhajanCategoryList(request):
categories = BhajanCategory.objects.all()
categoryArr = []
for category in categories:
categoryArr.append(
{"name": category.name, "link": category.link, "icon": category.icon}
)
lyricsBase = (
"https://huggingface.co/spaces/thejagstudio/MusicStore/raw/main/HTML Files/"
)
audioBase = "https://huggingface.co/spaces/thejagstudio/MusicStore/resolve/main/Bhajan Audio/"
return JsonResponse(
{"categories": categoryArr, "lyricsBase": lyricsBase, "audioBase": audioBase}
)
def bhajanList(request, catLink):
if catLink == "all-kirtan":
bhajans = Bhajan.objects.all()
category = "All Kirtan"
else:
bhajans = Bhajan.objects.filter(category__link=catLink)
category = BhajanCategory.objects.get(link=catLink).name
bhajanArr = []
for bhajan in bhajans:
bhajanArr.append(
{
"id": bhajan.bhajanId,
"title": bhajan.title,
"title_guj": bhajan.title_guj,
"lyrics": bhajan.lyrics,
"audio_url": bhajan.audio_url,
"isEng": bhajan.isEng,
"isHnd": bhajan.isHnd,
"isGer": bhajan.isGer,
"isAudio": bhajan.isAudio,
}
)
lyricsBase = (
"https://huggingface.co/spaces/thejagstudio/MusicStore/raw/main/HTML Files/"
)
audioBase = "https://huggingface.co/spaces/thejagstudio/MusicStore/resolve/main/Bhajan Audio/"
return JsonResponse(
{
"bhajans": bhajanArr,
"lyricsBase": lyricsBase,
"audioBase": audioBase,
"category": category,
}
)
def bhajanDetail(request, bhajanId):
bhajan = Bhajan.objects.filter(bhajanId=bhajanId).first()
if not bhajan:
return JsonResponse({"error": "Bhajan not found"})
else:
return JsonResponse(
{
"id": bhajan.bhajanId,
"title": bhajan.title,
"title_guj": bhajan.title_guj,
"category": bhajan.category.name,
"lyrics": bhajan.lyrics,
"audio_url": bhajan.audio_url,
"isEng": bhajan.isEng,
"isHnd": bhajan.isHnd,
"isGer": bhajan.isGer,
"isAudio": bhajan.isAudio,
"lyricsBase": "https://huggingface.co/spaces/thejagstudio/MusicStore/raw/main/HTML Files/",
"audioBase": "https://huggingface.co/spaces/thejagstudio/MusicStore/resolve/main/Bhajan Audio/",
}
)
def eventList(request):
events = Event.objects.all()
eventArr = []
for event in events:
# convert date to Sept 26,2024 | 8:30 - 9:30
dateFormatted = (
event.date.strftime("%b %d, %Y")
+ " | "
+ event.date.strftime("%I:%M %p")
+ " - "
+ event.time.strftime("%I:%M %p")
)
eventArr.append(
{
"title": event.title,
"description": event.description,
"date": dateFormatted,
"day": int(event.date.strftime("%d")),
"month": int(event.date.strftime("%m")),
"year": int(event.date.strftime("%Y")),
"created_by": event.created_by.__str__(),
"region": event.region.name,
"is_approved": event.is_approved,
"color": event.color,
}
)
return JsonResponse({"events": eventArr})
@jwt_required()
def notification(request):
notifications = Notification.objects.all()
notificationArr = []
for notification in notifications:
notificationArr.append(
{
"sender": notification.sender.__str__(),
"category": notification.sender.user_type,
"title": notification.title,
"content": notification.content,
"timestamp": notification.timestamp.strftime("%b %d, %Y | %I:%M %p"),
"notification_type": notification.notification_type,
}
)
return JsonResponse({"notifications": notificationArr})
@user_passes_test(is_superadmin)
@csrf_exempt
@jwt_required()
def send_notification(request):
if request.method == "POST":
content = request.POST.get("content")
recipient_type = request.POST.get("recipient_type")
notification = Notification.objects.create(
sender=request.user, content=content, notification_type="custom"
)
# Send web push notifications
subscriptions = PushSubscription.objects.all()
for subscription in subscriptions:
send_push_notification(
subscription,
{
"title": "New Notification",
"content": content,
"url": "/notifications",
},
)
return JsonResponse({"status": "success"})
return JsonResponse({"status": "error"})
def birthday_notifications():
today = timezone.now().date()
birthday_users = Bhagat.objects.filter(
birthday__month=today.month, birthday__day=today.day
)
for user in birthday_users:
notification = Notification.objects.create(
sender=Bhagat.objects.get(user_type="superadmin"),
content=f"Happy Birthday to {user.get_full_name()}!",
notification_type="birthday",
)
notification.recipients.set(Bhagat.objects.all())
@csrf_exempt
def login(request):
if request.method == "POST":
username = request.POST.get("username")
password = request.POST.get("password")
captcha_response = request.POST.get("captcha_response")
# Verify captcha
result = gCaptchaVerifer(captcha_response)
if not result.get("success"):
return JsonResponse({"error": "Invalid Captcha", "status": "error"})
# Authenticate user
user = Bhagat.objects.filter(username=username).first()
if user is not None and user.check_password(password):
# Generate tokens
refresh = RefreshToken.for_user(user)
return JsonResponse(
{
"status": "success",
"tokens": {
"access_token": str(refresh.access_token),
"refresh_token": str(refresh),
},
"user": {
"id": user.id,
"username": user.username,
"first_name": user.first_name,
"last_name": user.last_name,
"email": user.email,
"phone": user.phone,
"region": user.region.name,
"user_type": user.user_type,
"profile_image": user.profile_image,
},
}
)
return JsonResponse({"error": "Invalid credentials", "status": "error"})
return JsonResponse({"error": "Invalid Method", "status": "error"})
@csrf_exempt
@jwt_required()
def logout(request):
if request.method == "POST":
refresh_token = request.POST.get("refresh_token")
if not refresh_token:
return JsonResponse(
{"error": "Refresh token is required", "status": "error"}
)
else:
try:
refresh = RefreshToken(refresh_token)
refresh.blacklist()
return JsonResponse(
{"status": "success", "message": "Successfully logged out"}
)
except Exception as e:
return JsonResponse(
{"error": "Invalid token | " + str(e), "status": "error"}
)
return JsonResponse({"status": "error", "error": "Invalid Method"})
@jwt_required()
def get_user_profile(request):
try:
user = request.user
return JsonResponse(
{
"status": "success",
"user": {
"id": user.id,
"username": user.username,
"first_name": user.first_name,
"last_name": user.last_name,
"email": user.email,
"phone": user.phone,
"region": user.region.name,
"user_type": user.user_type,
"profile_image": user.profile_image,
},
}
)
except Exception as e:
return JsonResponse({"status": "error", "error": str(e)})
@csrf_exempt
@jwt_required()
def profile_updater(request):
if request.method == "POST":
try:
user = request.user
first_name = request.POST.get("first_name")
if first_name:
user.first_name = first_name
last_name = request.POST.get("last_name")
if last_name:
user.last_name = last_name
email = request.POST.get("email")
if email:
user.email = email
phone = request.POST.get("phone")
if phone:
user.phone = phone
region_name = request.POST.get("region")
if region_name:
user.region = Region.objects.get(name=region_name)
birth_date = request.POST.get("birth_date")
if birth_date:
user.birthday = birth_date
street_name = request.POST.get("street_name")
if street_name:
user.streetName = street_name
pincode = request.POST.get("pincode")
if pincode:
user.pincode = pincode
city = request.POST.get("city")
if city:
user.city = city
state = request.POST.get("state")
if state:
user.state = state
country = request.POST.get("country")
if country:
user.country = country
profile_image = request.POST.get("profile_image")
if profile_image:
user.profile_image = profile_image
user.save()
return JsonResponse(
{"status": "success", "message": "Profile updated successfully"}
)
except Exception as e:
return JsonResponse({"status": "error", "error": str(e)})
else:
user = request.user
regions = Region.objects.all()
data = {
"first_name": user.first_name,
"last_name": user.last_name,
"email": user.email,
"phone": user.phone,
"region": user.region.name,
"birth_date": user.birthday,
"street_name": user.streetName,
"pincode": user.pincode,
"city": user.city,
"state": user.state,
"country": user.country,
"profile_image": user.profile_image,
}
return JsonResponse(
{
"status": "success",
"regions": [region.name for region in regions],
"user": data,
}
)
@jwt_required()
def bhaktoList(request):
current_user = request.user
bhaktos = Bhagat.objects.filter(assigned_to=current_user).all()
bhaktoArr = []
for bhakto in bhaktos:
bhaktoArr.append(
{
"id": bhakto.id,
"first_name": bhakto.first_name,
"last_name": bhakto.last_name,
"region": bhakto.region.name,
"user_type": bhakto.user_type,
"profile_image": bhakto.profile_image,
}
)
return JsonResponse({"bhaktos": bhaktoArr})
@api_view(["POST"])
@permission_classes([AllowAny])
def send_otp(request):
try:
data = json.loads(request.body)
phone = data.get("phone")
if not phone:
return JsonResponse(
{"status": "error", "error": "Phone number is required"}
)
# Check if user exists
user = Bhagat.objects.filter(phone=phone).first()
if not user:
return JsonResponse(
{"status": "error", "error": "No account found with this phone number"}
)
# Generate dummy OTP (in production, use proper OTP generation and SMS service)
otp = "123456" # Dummy OTP for testing
print(f"Generated OTP for {phone}: {otp}") # This simulates sending OTP
# In production, store OTP with timestamp in database or cache
# For now, we'll just return success
return JsonResponse({"status": "success", "message": "OTP sent successfully"})
except Exception as e:
return JsonResponse({"status": "error", "error": str(e)})
@api_view(["POST"])
@permission_classes([AllowAny])
def verify_otp(request):
try:
data = json.loads(request.body)
phone = data.get("phone")
otp = data.get("otp")
if not phone or not otp:
return JsonResponse(
{"status": "error", "error": "Phone number and OTP are required"}
)
# For demo purposes, accept any 6-digit OTP
if otp == "123456": # Dummy verification
return JsonResponse(
{"status": "success", "message": "OTP verified successfully"}
)
return JsonResponse({"status": "error", "error": "Invalid OTP"})
except Exception as e:
return JsonResponse({"status": "error", "error": str(e)})
@api_view(["POST"])
@jwt_required()
@csrf_exempt
@permission_classes([AllowAny])
def change_password(request):
try:
data = json.loads(request.body)
new_password = data.get("new_password")
if not new_password:
return JsonResponse(
{
"status": "error",
"error": "Phone number and new password are required",
}
)
# Update user's password
user = request.user
if not user:
return JsonResponse({"status": "error", "error": "User not found"})
user.password = new_password
user.save()
return JsonResponse(
{"status": "success", "message": "Password changed successfully"}
)
except Exception as e:
return JsonResponse({"status": "error", "error": str(e)})
@jwt_required()
@api_view(["POST"])
@csrf_exempt
@permission_classes([AllowAny])
def save_push_subscription(request):
try:
subscription_data = json.loads(request.body)
user = request.user
pushUser = PushSubscription.objects.filter(user=user).first()
if pushUser:
pushUser.endpoint = subscription_data["endpoint"]
pushUser.p256dh = subscription_data["keys"]["p256dh"]
pushUser.auth = subscription_data["keys"]["auth"]
pushUser.save()
else:
PushSubscription.objects.create(
user=user,
endpoint=subscription_data["endpoint"],
p256dh=subscription_data["keys"]["p256dh"],
auth=subscription_data["keys"]["auth"],
)
return JsonResponse({"status": "success"})
except Exception as e:
return JsonResponse({"status": "error", "message": str(e)})
def send_push_notification(subscription, message):
try:
webpush(
subscription_info={
"endpoint": subscription.endpoint,
"keys": {"p256dh": subscription.p256dh, "auth": subscription.auth},
},
data=json.dumps(message),
vapid_private_key=settings.WEBPUSH_SETTINGS["VAPID_PRIVATE_KEY"],
vapid_claims={
"sub": f"mailto:{settings.WEBPUSH_SETTINGS['VAPID_ADMIN_EMAIL']}"
},
)
except WebPushException as e:
print(f"Web push failed: {e}")
@api_view(["GET", "POST"])
@jwt_required()
@csrf_exempt
@permission_classes([AllowAny])
def polls(request):
if request.method == "GET":
user = request.user
polls = Poll.objects.filter(participants=user)
pollArr = []
for poll in polls:
tempPoll = {}
tempPoll["id"] = poll.id
tempPoll["question"] = poll.question
tempPoll["options"] = []
for option in poll.options.all():
tempOption = {}
tempOption["id"] = option.id
tempOption["optionText"] = option.optionText
tempOption["count"] = option.voters.all().count()
tempOption["precentage"] = (
option.voters.all().count() / poll.participants.all().count()
) * 100
tempOption["voters"] = []
for voter in option.voters.all()[:2]:
tempOption["voters"].append(
{
"id": voter.profile_image,
"first_name": voter.first_name,
"last_name": voter.last_name,
"profile_image": voter.profile_image,
}
)
tempPoll["options"].append(tempOption)
tempPoll["created_by"] = {
"id": poll.created_by.profile_image,
"first_name": poll.created_by.first_name,
"last_name": poll.created_by.last_name,
"profile_image": poll.created_by.profile_image,
}
tempPoll["created_at"] = poll.created_at
pollArr.append(tempPoll)
return JsonResponse({"data": pollArr})
if request.method == "POST":
data = json.loads(request.body)
user = request.user
poll = Poll.objects.get(id=id)
option = OptionPoll.objects.get(id=data.get("option"))
option.votes.add(user)
return JsonResponse(
{"status": "success", "message": "Vote submitted successfully"}
)
@api_view(["GET"])
@jwt_required()
@permission_classes([AllowAny])
def voterList(request, id):
poll = Poll.objects.filter(participants=user).get(id=id)
if not poll:
return JsonResponse(
{"error": "Poll may not exist or you may not have access to it"}
)
else:
user = request.user
options = poll.options.all()
optionsList = []
for option in options:
tempOption = {}
tempOption["id"] = option.id
tempOption["optionText"] = option.optionText
tempOption["voters"] = []
for voter in option.voters.all():
tempOption["voters"].append(
{
"id": voter.profile_image,
"first_name": voter.first_name,
"last_name": voter.last_name,
"profile_image": voter.profile_image,
}
)
return JsonResponse({"data": optionsList})