from django.shortcuts import render, redirect, get_object_or_404 from django.contrib.auth import authenticate, login as auth_login, logout from .models import CustomUser, JobPosting from skyonnadmin.models import ClientDetails from django.contrib import messages from django.http import JsonResponse from django.db.models import Q from .models import Message from django.contrib.auth.models import User from django.http import HttpResponseRedirect def home(request): return render(request, 'user/home.html') def create_user(request): if request.method == 'POST': # Handle signup form submission first_name = request.POST.get('first_name') last_name = request.POST.get('last_name') phone = request.POST.get('phone') phone2 = request.POST.get('phone_2') company_email = request.POST.get('company_email') personal_email = request.POST.get('personal_email') address = request.POST.get('address') work_location = request.POST.get('work_location') role = request.POST.get('role') password = request.POST.get('password') picture = request.FILES.get('picture') # Create user_id based on role role_prefix = role[0].upper() user_count = CustomUser.objects.filter(role=role).count() + 1 user_id = f"{role_prefix}{first_name[:3].upper()}{user_count:03}" # Create the CustomUser object user = CustomUser.objects.create( first_name=first_name, last_name=last_name, phone=phone, phone2=phone2, company_email=company_email, personal_email=personal_email, address=address, work_location=work_location, role=role, user_id=user_id ) user.set_password(password) user.picture = picture user.save() user.backend = 'user.backend.PhoneAuthenticationBackend' auth_login(request, user) return redirect('dashboard', user_id=user.user_id) else: return render(request, 'user/home.html') def user_login(request): if request.method == 'POST': phone = request.POST.get('phone') password = request.POST.get('password') user = authenticate(request, phone=phone, password=password) print(phone, password, user) if user is not None: auth_login(request, user) user = CustomUser.objects.filter(phone=phone).first() if user: print(user.user_id) return redirect('dashboard', user_id=user.user_id) else: messages.error(request, 'No user found for the provided phone number.') else: messages.error(request, 'Invalid phone number or password.') return render(request, 'user/home.html') def dashboard(request, user_id): # Retrieve the manager object using manager_id user = get_object_or_404(CustomUser, user_id=user_id) # Get the manager's first name first_name = user.first_name.capitalize() full_name = f"{first_name}'s" print(full_name) if user.role == 'manager': print("hiii") # Redirect to manager dashboard return render(request, 'user/test.html', {'first_name': full_name, 'user_id': user_id}) if user.role == 'recruiter': # Redirect to manager dashboard return render(request, 'recruiter/recruiter_dashboard.html', {'first_name': full_name, 'user_id': user_id}) def new_job_posting(request): clients = ClientDetails.objects.all() return render(request, 'user/new_job_posting.html', {'clients': clients}) def add_recruiter(request): user_id = request.GET.get('user_id', None) print(user_id) context = {'user_id': user_id} return render(request, 'user/add_recruiter.html', context) def logout_view(request): logout(request) return redirect('home') def save_job_posting(request): if request.method == 'POST': client = request.POST.get('Client') spoc = request.POST.get('SPOC') start_date = request.POST.get('StartDate') budget_max = request.POST.get('BudgetMax') budget_min = request.POST.get('BudgetMin') job_description = request.POST.get('JobDescription') special_instructions = request.POST.get('SpecialInstructions') job_id = request.POST.get('JobID') spoc_2 = request.POST.get('SPOC2') close_date = request.POST.get('CloseDate') location = request.POST.get('Location') no_of_posting = request.POST.get('NoOfPosting') job_type = request.POST.get('Type') header = request.POST.get('Header') experience_in_years = request.POST.get('Experience_in_Yrs') print(location) job_posting = JobPosting( client=client, spoc=spoc, budget_min=budget_min, start_date=start_date, budget_max=budget_max, job_description=job_description, special_instructions=special_instructions, job_id=job_id, spoc_2=spoc_2, close_date=close_date, location=location, no_of_posting=no_of_posting, job_type=job_type, header=header, experience_in_years=experience_in_years ) job_posting.save() return JsonResponse({'status': 'success', 'message': 'Job posting saved successfully.'}) return redirect('error_url') def create_recruiter(request): if request.method == 'POST': # Handle signup form submission # print(request.post) first_name = request.POST.get('first_name') last_name = request.POST.get('last_name') phone = request.POST.get('phone') phone2 = request.POST.get('phone_2') company_email = request.POST.get('company_email') personal_email = request.POST.get('personal_email') address = request.POST.get('address') work_location = request.POST.get('work_location') role = "recruiter" # request.POST.get('role') password = request.POST.get('password') picture = request.FILES.get('picture') manager_assigned = request.POST.get('manager_assigned', None) print(manager_assigned, 1) # Create user_id based on role role_prefix = role[0].upper() user_count = CustomUser.objects.filter(role=role).count() + 1 user_id = f"{role_prefix}{first_name[:3].upper()}{user_count:03}" # Create the CustomUser object user = CustomUser.objects.create( first_name=first_name, last_name=last_name, phone=phone, phone2=phone2, company_email=company_email, personal_email=personal_email, address=address, work_location=work_location, role=role, user_id=user_id, manager_assigned=manager_assigned ) user.set_password(password) user.picture = picture user.save() return redirect('dashboard', user_id=manager_assigned) # return JsonResponse({'success': True, 'message': 'Recruiter added successfully!'}) else: messages.success(request, 'Data saved successfully.') # Instead of redirecting, render the same form page again return render(request, 'user/add_recruiter.html') def all_job_posting(request): query = request.GET.get('query', '') # Get the query parameter if it exists if query: job_postings = JobPosting.objects.filter( job_id__icontains=query) | JobPosting.objects.filter( spoc__icontains=query) | JobPosting.objects.filter( client__icontains=query ) print(query) else: job_postings = JobPosting.objects.all() return render(request, 'user/all_job_postings.html', {'job_postings': job_postings}) def send_message(request, recipient_id): recipient = get_object_or_404(User, pk=recipient_id) if request.method == 'POST': subject = request.POST.get('subject') body = request.POST.get('body') message = Message.objects.create(sender=request.user, recipient=recipient, subject=subject, body=body) return redirect('messages') return render(request, 'send_message.html', {'recipient': recipient}) def mark_as_read(request, message_id): message = get_object_or_404(Message, pk=message_id, recipient=request.user) message.is_read = True message.save() return redirect('messages')