Spaces:
Running
Running
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 .models import Bhagat, Event, Attendance, Message, Notification | |
from django.views.decorators.http import require_POST | |
from django.utils import timezone | |
import json | |
from .models import Bhagat, Event, Attendance, Message, Notification, Region, BhajanCategory, Bhajan | |
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() | |
bhajans = Bhajan.objects.all() | |
bhajanArr = [] | |
for bhajan in bhajans: | |
bhajanArr.append({ | |
"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 | |
}) | |
categoryArr = [] | |
for category in categories: | |
categoryArr.append({ | |
"name": category.name, | |
"link": category.link | |
}) | |
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, "bhajans": bhajanArr, "lyricsBase": lyricsBase, "audioBase": audioBase}) | |
def bhajanDetail(request,id): | |
bhajan = Bhajan.objects.get(bhajanId=id) | |
if bhajan is None: | |
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/" | |
}) | |
return HttpResponse("Bhajan Detail Page") | |
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}) | |
def dashboard(request): | |
user = request.user | |
context = { | |
'user': user, | |
} | |
if user.user_type == 'superadmin': | |
context['users'] = Bhagat.objects.all() | |
context['events'] = Event.objects.all() | |
elif user.user_type == 'regionadmin': | |
context['monitors'] = Bhagat.objects.filter(user_type='monitor', region=user.region) | |
context['users'] = Bhagat.objects.filter(user_type='user', region=user.region) | |
context['events'] = Event.objects.filter(region=user.region) | |
elif user.user_type == 'monitor': | |
context['users'] = Bhagat.objects.filter(assigned_to=user) | |
context['events'] = Event.objects.filter(region=user.region) | |
else: # regular user | |
context['events'] = Event.objects.filter(region=user.region, is_approved=True) | |
return render(request, 'dashboard.html', context) | |
def manage_users(request): | |
if request.method == 'POST': | |
# Handle user creation/modification | |
pass | |
users = Bhagat.objects.all() | |
return render(request, 'manage_users.html', {'users': users}) | |
def create_event(request): | |
if request.method == 'POST': | |
# Handle event creation | |
pass | |
return render(request, 'create_event.html') | |
def approve_event(request, event_id): | |
event = get_object_or_404(Event, id=event_id) | |
event.is_approved = True | |
event.save() | |
return redirect('event_list') | |
def update_attendance(request, event_id): | |
event = get_object_or_404(Event, id=event_id) | |
status = request.POST.get('status') | |
attendance, created = Attendance.objects.update_or_create( | |
user=request.user, | |
event=event, | |
defaults={'status': status} | |
) | |
return JsonResponse({'status': 'success'}) | |
def send_message(request): | |
if request.method == 'POST': | |
recipient_id = request.POST.get('recipient') | |
content = request.POST.get('content') | |
recipient = get_object_or_404(Bhagat, id=recipient_id) | |
Message.objects.create(sender=request.user, recipient=recipient, content=content) | |
return redirect('messages') | |
def send_notification(request): | |
if request.method == 'POST': | |
content = request.POST.get('content') | |
recipient_type = request.POST.get('recipient_type') | |
if recipient_type == 'all': | |
recipients = Bhagat.objects.all() | |
elif recipient_type == 'monitors': | |
recipients = Bhagat.objects.filter(user_type='monitor') | |
elif recipient_type == 'regionadmins': | |
recipients = Bhagat.objects.filter(user_type='regionadmin') | |
notification = Notification.objects.create(sender=request.user, content=content, notification_type='custom') | |
notification.recipients.set(recipients) | |
return redirect('notifications') | |
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()) | |