def delete_story(request, story_id): try: user = request.user body = request.data if not user.is_employer: return response_400("User is not employer", "User is not employer", None) if not Employer.objects.filter(user=user).exists(): return response_400("Employer not found", "Employer not found", None) employer = Employer.objects.filter(user=user).first() if not Stories.objects.filter(id=story_id): return response_400("Story not found", "Story not found", None) story = Stories.objects.filter(id=story_id).first() if story.company_id != employer.company_id: return response_400("Company does not match", "Company does not match", None) story.removed = True story.save() return response_204("Story deleted successfully") except Exception as e: return response_500("internal error", e)
def unsubscribe_company(request, company_id): try: user = request.user if not Company.objects.filter(id=company_id, removed=False).exists(): return response_400("Company not found", "Company not found", None) if user.is_employer: return response_400("Only Candidates can access this page.", "Only Candidates can access this page", None) if not Candidate.objects.filter(user=user).exists(): return response_400("Candidate not found.", "Candidate not found.", None) candidate = Candidate.objects.filter(user=user).first() if not SubscribedCompanies.objects.filter( candidate=candidate, company__id=company_id).exists(): return response_400("Company not already subscribed", "Company not already subscribed", None) subscribe_company = SubscribedCompanies.objects.filter( candidate=candidate, company__id=company_id).first() subscribe_company.delete() return response_204("Company unfollowed") except Exception as e: return response_500("internal error", e)
def get_jobs_in_preferred_cities(request): try: user = request.user if not Candidate.objects.filter(user=user).exists(): return response_400("No candidate associated with this user", "No candidate associated with this user", None) candidate = Candidate.objects.filter(user=user).first() preferred_cities = candidate.preferred_city.all() preferred_cities_name = [p.name for p in preferred_cities] locations = request.query_params.getlist( 'location') if request.query_params.getlist( 'location') else preferred_cities_name jobs = JobPost.objects.filter(status__in=['Expired', 'Published']) if locations: locationObjects = City.objects.filter(name__in=locations) jobs = jobs.filter(removed=False, locations__in=locationObjects, status__in=['Expired', 'Published']) return response_200( "Jobs in preferred cities fetched", JobSerializer(jobs, context={ 'request': request }, many=True).data) except Exception as e: return response_500("internal error", e)
def get_subscribed_company_jobs(request): try: user = request.user if not Candidate.objects.filter(user=user).exists(): return response_400("No candidate associated with this user", "No candidate associated with this user", None) candidate = Candidate.objects.filter(user=user).first() result = [] if SubscribedCompanies.objects.filter(candidate=candidate).exists(): sub_companies = SubscribedCompanies.objects.filter( candidate=candidate) for company in sub_companies: obj = {} obj['subscribed_company'] = SubscribedCompaniesSerializer( company, context={ 'request': request }).data jobs = JobPost.objects.filter( company=company.company, status__in=['Expired', 'Published']) obj['jobs'] = JobSerializer(jobs, context={ 'request': request }, many=True).data result.append(obj) return response_200("Subscribed Companies and jobs fetched", result) except Exception as e: return response_500("internal error", e)
def add_story(request): try: user = request.user body = request.data if user.is_employer: description = body['description'] if 'description' in body else None photo_url = body['photo_url'] if 'photo_url' in body else None name = body['name'] if 'name' in body else None company = user.employer_set.first().company story = Stories(company=company) story.name = name if description: story.description = description if photo_url: story.photo_url = photo_url story.save() return response_201( "Story saved", StoriesSerializer(story, context={ 'request': request }).data) else: return response_400("User is not employer", "User is not employer", None) except Exception as e: return response_500("internal error", e)
def add_company(request): try: user = request.user body = request.data if not user.is_employer: return response_400("Only employers can add company profile", "Only employers can add company profile", None) else: name = body['name'] if 'name' in body else None title = body['title'] if 'title' in body else None short_code = body['short_code'] if 'short_code' in body else None phone_number = body[ 'phone_number'] if 'phone_number' in body else None size = body['size'] if 'size' in body else None level = body['level'] if 'level' in body else None website = body['website'] if 'website' in body else None logo_url = body['logo_url'] if 'logo_url' in body else None profile = body['profile'] if 'profile' in body else None description = body['description'] if 'description' in body else None email = body['email'] if 'email' in body else None is_active = body['is_active'] if 'is_active' in body else False facebook = body['facebook'] if 'facebook' in body else None twitter = body['twitter'] if 'twitter' in body else None linkedin = body['linkedin'] if 'linkedin' in body else None instagram = body['instagram'] if 'instagram' in body else None address = body['address'] if 'address' in body else None company = Company() company.name = name company.title = title company.description = description company.short_code = short_code company.size = size company.level = level company.website = website company.logo_url = logo_url company.profile = profile company.email = email company.phone_number = phone_number company.is_active = is_active company.facebook = facebook company.twitter = twitter company.linkedin = linkedin company.instagram = instagram company.address = address company.save() return response_200( "Company details added successfully", CompanySerializer(company, context={ 'request': request }).data) except Exception as e: return response_500("internal error", e)
def get_initiative_individual(request, initiative_id): try: if not Initiative.objects.filter(id=initiative_id, removed=False).exists(): return response_400('Requested Initiative does not exists', 'INITIATIVE DOES NOT EXISTS', None) else: initiative_details = Initiative.objects.filter( id=initiative_id, removed=False).first() return response_200("Initiative fetched successfully", InitiativeSerializer(initiative_details).data) except Exception as e: return response_500("internal error", e)
def delete_company(request, company_id): try: user = request.user if user.is_employer: if Company.objects.filter(id=company_id).exists(): company = Company.objects.filter(id=company_id).first() employer = Employer.objects.filter(user_id=user.id).first() if company.id == employer.company_id: company.removed = True company.save() return response_204("Company deleted successfully!") else: return response_400( "Your are not the employer of this Company", "COMPANY_EMPLOYER_ID_MISMATCH", None) else: return response_400("Company does not exist", "COMPANY_DOES_NOT_EXIST", None) else: return response_400("Only employer can delete a company", "NOT_EMPLOYER", None) except Exception as e: return response_500("Internal server error: ", e)
def subscribe_company(request, company_id): try: user = request.user if not Company.objects.filter(id=company_id, removed=False).exists(): return response_400("Company not found", "Company not found", None) if user.is_employer: return response_400("Only Candidates can access this page.", "Only Candidates can access this page", None) if not Candidate.objects.filter(user=user).exists(): return response_400("Candidate not found.", "Candidate not found.", None) candidate = Candidate.objects.filter(user=user).first() if SubscribedCompanies.objects.filter(candidate=candidate, company__id=company_id).exists(): return response_400("Company already subscribed", "Company already subscribed", None) subscribe_company = SubscribedCompanies() subscribe_company.company = Company.objects.filter( id=company_id, removed=False).first() subscribe_company.candidate = candidate subscribe_company.save() return response_201( "Company followed", SubscribedCompaniesSerializer(subscribe_company, context={ 'request': request }).data) except Exception as e: return response_500("internal error", e)
def delete_initiative(request, initiative_id): try: user = request.user if user.is_employer: if Initiative.objects.filter(id=initiative_id).exists(): initiative = Initiative.objects.filter( id=initiative_id).first() employer = Employer.objects.filter(user_id=user.id).first() if initiative.company.id == employer.company_id: initiative.removed = True initiative.save() return response_204("Initiative deleted successfully!") else: return response_400( "Your are not the employer of this Company", "COMPANY_EMPLOYER_ID_MISMATCH", None) else: return response_400("Initiative does not exist", "INITIATIVE_DOES_NOT_EXIST", None) else: return response_400("Only employer can delete a Initiative", "NOT_EMPLOYER", None) except Exception as e: return response_500("Internal server error: ", e)
def get_video_individual(request, video_id): try: if Video.objects.filter(removed=False, id=video_id).exists(): video = Video.objects.filter(removed=False, id=video_id).first() return response_200( "Video fetched successfully", VideoSerializer(video, context={ 'request': request }).data) else: return response_400("Video does not exists", "Video does not exists", None) except Exception as e: return response_500("internal error", e)
def get_blog_individual(request, blog_id): try: if Blog.objects.filter(removed=False, id=blog_id).exists(): blog = Blog.objects.filter(removed=False, id=blog_id).first() return response_200( "Blog fetched successfully", BlogSerializer(blog, context={ 'request': request }).data) else: return response_400("Blog does not exists", "Blog does not exists", None) except Exception as e: return response_500("internal error", e)
def get_story_individual(request, story_id): try: user = request.user if Stories.objects.filter(removed=False, id=story_id).exists(): story = Stories.objects.filter(removed=False, id=story_id).first() return response_200( "Stories fetched successfully", StoriesSerializer(story, context={ 'request': request }).data) else: return response_400("Story does not exists", "Story does not exists", None) except Exception as e: return response_500("internal error", e)
def get_liked_scholarships(request): try: user = request.user if not Candidate.objects.filter(user=user).exists(): return response_400("No candidate associated with this user", "No candidate associated with this user", None) candidate = Candidate.objects.filter(user=user).first() liked_scholarships = LikedScholarships.objects.filter( candidate=candidate) return response_200( "Liked scholarships fetched", LikedScholarshipsSerializer(liked_scholarships, context={ 'request': request }, many=True).data) except Exception as e: return response_500("internal error", e)
def get_liked_jobs(request): try: user = request.user if not Candidate.objects.filter(user=user).exists(): return response_400("No candidate associated with this user", "No candidate associated with this user", None) candidate = Candidate.objects.filter(user=user).first() liked_jobs = LikedJobs.objects.filter( candidate=candidate, job_post__status__in=['Expired', 'Published']) return response_200( "Liked jobs fetched", LikedJobsSerializer(liked_jobs, context={ 'request': request }, many=True).data) except Exception as e: return response_500("internal error", e)
def get_company_details(request, company_id): try: user = request.user if not Company.objects.filter(id=company_id).exists(): return response_400( 'Requested company is not registered with Inclusivo', 'COMPANY_NOT_REGISTERED', None) else: company_details = Company.objects.filter(id=company_id).first() is_following = False if SubscribedCompanies.objects.filter(company=company_details, candidate__user=user): is_following = True data = {} data['company'] = CompanySerializer(company_details, context={ 'request': request }).data data['is_following'] = is_following return response_200("Companies fetched successfully", data) except Exception as e: return response_500("internal error", e)
def add_initiatives(request): try: user = request.user body = request.data if user.is_employer: name = body['name'] if 'name' in body else None description = body['description'] if 'description' in body else None company = user.employer_set.first().company initiative = Initiative(company=company) initiative.name = name initiative.description = description initiative.save() return response_201("Initiative saved", InitiativeSerializer(initiative).data) else: return response_400("User is not employer", "User is not employer", None) except Exception as e: return response_500("internal error", e)