Spaces:
Running
Running
File size: 13,090 Bytes
00c2d61 aff4b27 4618a46 00c2d61 aff4b27 00c2d61 aff4b27 00c2d61 aff4b27 00c2d61 aff4b27 4618a46 aff4b27 00c2d61 aff4b27 00c2d61 aff4b27 00c2d61 4618a46 00c2d61 4618a46 00c2d61 4618a46 aff4b27 00c2d61 aff4b27 00c2d61 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 |
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, Notification, Region, BhajanCategory, Bhajan
from django.conf import settings
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
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()
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})
@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')
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())
@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 TokenError:
return JsonResponse({
"error": "Invalid token",
"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
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",
"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}) |