def companies(request): q = request.GET.get('q') has_review = get_boolean_from_request(request, 'hasReview') mine = get_boolean_from_request(request, 'mine') companies = Company.objects.all() if has_review: companies_has_review = Review.objects.filter( is_published=True).order_by('company__id').distinct('company__id') companies = Company.objects.all().filter( id__in=[r.company.id for r in companies_has_review]) if mine: users_companies = JobApplication.objects.filter(user=request.user, is_deleted=False) companies = companies.filter( id__in=[j.company_object.id for j in users_companies]) if q is not None: companies = companies.filter(company__icontains=q) paginator = pagination.CustomPagination() companies = paginator.paginate_queryset(companies, request) serialized_companies = CompanySerializer(instance=companies, many=True, context={ 'user': request.user }).data return JsonResponse(create_response(data=serialized_companies, paginator=paginator), safe=False)
def update_jobapp(request): status_id = request.POST.get('status_id') rejected = get_boolean_from_request(request, 'rejected', 'POST') jobapp_id = request.POST.get('jobapp_id') success = True code = 0 if jobapp_id is None: success = False code = 10 elif rejected is None and status_id is None: success = False code = 10 else: user_job_app = JobApplication.objects.filter(pk=jobapp_id) if len(user_job_app) == 0: success = False code = 11 else: user_job_app = user_job_app[0] if status_id is None: user_job_app.isRejected = rejected else: new_status = ApplicationStatus.objects.filter(pk=status_id) if len(new_status) == 0: success = False code = 11 else: if rejected is None: user_job_app.applicationStatus = new_status[0] else: user_job_app.applicationStatus = new_status[0] user_job_app.isRejected = rejected user_job_app.save() return JsonResponse(create_response(None, success, code), safe=False)
def get_profile(request): basic = get_boolean_from_request(request, 'basic') if basic: return JsonResponse(create_response(data=UserSerializer( instance=request.user, context={ 'detailed': True }, many=False).data), safe=False) else: return JsonResponse(create_response( data=ProfileSerializer(instance=request.user, many=False).data), safe=False)
def positions(request, company_pk): has_review = get_boolean_from_request(request, 'hasReview') company = Company.objects.get(pk=company_pk) queryset = JobApplication.objects.filter(company_object=company) if has_review: positions_has_review = Review.objects.filter( company=company, is_published=True).order_by( 'position__id').distinct('position__id') queryset = queryset.filter( position__id__in=[r.position.id for r in positions_has_review]) positions = set() for q in queryset: if q is not None: positions.add(q.position) return JsonResponse(create_response( data=JobPositionSerializer(instance=positions, many=True).data), safe=False)
def agg_generic(request): response = [] user_profile = request.user User = get_user_model() filter_by_college = False public = get_boolean_from_request(request, 'public') if user_profile.user_type >= int(User.UserTypes.student) and not public: college_users = get_user_model().objects.filter(id__in=[ p.id for p in User.objects.filter(college=user_profile.college, is_demo=False) ]) filter_by_college = True total_jobs_applied = JobApplication.objects.filter( user__in=college_users, is_deleted=False) else: if user_profile.user_type < int(User.UserTypes.student) and not public: return JsonResponse(create_response( data=None, success=False, error_code=ResponseCodes.not_supported_user), safe=False) total_jobs_applied = JobApplication.objects.filter(is_deleted=False) for graph in range(4): item = dict(graph={}) if graph == 0: item['graph']['type'] = 'pie' item['graph']['legend'] = [] item['graph']['series'] = [] item['title'] = 'Phone Screen Rate' if total_jobs_applied.count() == 0: item['value'] = '0%' else: item['value'] = str( round( total_jobs_applied.filter( application_status__value='PHONE SCREEN').count() / total_jobs_applied.count() * 100, 2)) + '%' item['description'] = '13% INCREASE from last month' statuses = total_jobs_applied.filter(~Q( application_status=None)).values( 'application_status').annotate(count=Count('pk')) for status in statuses: status_text = ApplicationStatus.objects.get( pk=status['application_status']).value.upper() item['graph']['legend'].append(status_text) serie = {'name': status_text, 'value': status['count']} if status_text == 'PHONE SCREEN': serie['selected'] = True item['graph']['series'].append(serie) elif graph == 1: item['graph']['type'] = 'pie' item['graph']['legend'] = [] item['graph']['series'] = [] item['title'] = 'Onsite Interview Rate' if total_jobs_applied.count() == 0: item['value'] = '0%' else: item['value'] = str( round( total_jobs_applied.filter( application_status__value='ONSITE INTERVIEW'). count() / total_jobs_applied.count() * 100, 2)) + '%' item['description'] = '4% DECREASE from last month' statuses = total_jobs_applied.filter(~Q( application_status=None)).values( 'application_status').annotate(count=Count('pk')) for status in statuses: status_text = ApplicationStatus.objects.get( pk=status['application_status']).value.upper() item['graph']['legend'].append(status_text) serie = {'name': status_text, 'value': status['count']} if status_text == 'ONSITE INTERVIEW': serie['selected'] = True item['graph']['series'].append(serie) elif graph == 2: item['graph']['type'] = 'line' item['graph']['series'] = [] item['title'] = 'Total Applied Jobs' item['value'] = total_jobs_applied.count() item['description'] = '21% INCREASE from last month' today = datetime.date.today() + relativedelta(days=+1) last_year = datetime.date.today() + relativedelta(years=-2) months = [] for i in range(0, today.month): d = today + relativedelta(months=-1 * i) months.insert(0, d) dec = today + relativedelta(months=-1 * today.month) while len(months) != 12: months.insert(0, dec) dec = dec + relativedelta(months=-1) apps_by_month = total_jobs_applied.filter( apply_date__range=[last_year, today]).values( 'apply_date__year', 'apply_date__month').annotate(count=Count('pk')) serie = {'name': item['title'], 'type': 'line'} data = [0] * 12 for j in range(0, 12): count = apps_by_month.filter(apply_date__year=months[j].year, apply_date__month=months[j].month) if count.count() > 0: data[j] = count[0]['count'] serie['data'] = data item['graph']['series'].append(serie) elif graph == 3: item['graph']['type'] = 'line' item['graph']['series'] = [] item['title'] = 'Total Users' User = get_user_model() if filter_by_college: total_user = User.objects.filter(id__in=[ p.id for p in User.objects.filter(college=user_profile.college, is_demo=False) ]) else: total_user = User.objects.filter(is_demo=False) total_user_count = total_user.count() item['value'] = total_user_count total_application = total_jobs_applied.count() total_average = total_application / total_user_count item['description'] = 'Average ' + str( round(total_average, 2)) + ' & ' + 'Total ' + str(total_application) + ' Jobs' today = datetime.date.today() + relativedelta(days=+1) last_year = datetime.date.today() + relativedelta(years=-2) months = [] for i in range(0, today.month): d = today + relativedelta(months=-1 * i) months.insert(0, d) dec = today + relativedelta(months=-1 * today.month) while len(months) != 12: months.insert(0, dec) dec = dec + relativedelta(months=-1) apps_by_month = total_user.filter( date_joined__range=[last_year, today]).values( 'date_joined__year', 'date_joined__month').annotate(count=Count('pk')) serie = {'name': item['title'], 'type': 'line'} data = [0] * 12 for j in range(0, 12): count = apps_by_month.filter( date_joined__year=months[j].year, date_joined__month=months[j].month) if count.count() > 0: if j == 0: data[j] = count[0]['count'] else: data[j] = data[j - 1] + count[0]['count'] serie['data'] = data item['graph']['series'].append(serie) response.append(item) return JsonResponse(create_response(data=response), safe=False)
def agg_detailed(request): response = [] user_profile = request.user User = get_user_model() filter_by_college = False public = get_boolean_from_request(request, 'public') if user_profile.user_type >= int(User.UserTypes.student) and not public: college_users = User.objects.filter(id__in=[ p.id for p in User.objects.filter(college=user_profile.college, is_demo=False) ]) filter_by_college = True if user_profile.user_type < int(User.UserTypes.student) and not public: return JsonResponse(create_response( data=None, success=False, error_code=ResponseCodes.not_supported_user), safe=False) for graph in range(4): item = {} if graph == 0: item['graph'] = {} item['graph']['type'] = 'bar' item['graph']['series'] = [] item['graph']['title'] = 'Top Companies Applied' item['list'] = {} item['list']['data'] = [] item['list']['title'] = 'Top Companies Applied' today = datetime.date.today() + relativedelta(days=+1) last_year = datetime.date.today() + relativedelta(years=-1) months = [] months_string = [] if filter_by_college: distinct_jobapps = JobApplication.objects.filter( user__in=college_users).distinct('company_object', 'user') else: distinct_jobapps = JobApplication.objects.filter( user__is_demo=False).distinct('company_object', 'user') # ~Q(application_status__pk = 2) indicates not 'To Apply' statuses in the prod DB. top_companies = JobApplication.objects.filter( ~Q(application_status=None), ~Q(application_status__pk=2), apply_date__range=[last_year, today], is_deleted=False).values( company=F('company_object__company')).annotate( count=Count('company_object')).filter( id__in=distinct_jobapps).order_by('-count') for i in range(0, today.month): d = today + relativedelta(months=-1 * i) months.insert(0, d) months_string.insert(0, d.strftime("%B")) dec = today + relativedelta(months=-1 * today.month) while len(months) != 12: months.insert(0, dec) months_string.insert(0, dec.strftime("%B")) dec = dec + relativedelta(months=-1) item['graph']['xAxis'] = months_string if top_companies.count() > 10: top_companies = top_companies[:10] total = 0 for company in top_companies: serie = {'name': company['company'], 'type': 'bar'} data = [0] * 12 for j in range(0, 12): count = JobApplication.objects.filter( company_object__company=company['company'], apply_date__year=months[j].year, apply_date__month=months[j].month, is_deleted=False, id__in=distinct_jobapps) data[j] = count.count() serie['data'] = data serie['stack'] = 'Company' item['graph']['series'].append(serie) item['list']['data'].append({ 'id': company['company'], 'value': company['count'] }) total += company['count'] item['list']['total'] = total elif graph == 1: item['graph'] = {} item['graph']['type'] = 'line' item['graph']['series'] = [] item['graph']['title'] = 'Peak Season' item['list'] = {} item['list']['data'] = [] item['list']['title'] = 'Peak Season' today = datetime.date.today() + relativedelta(days=+1) last_year = datetime.date.today() + relativedelta(years=-1) months = [] months_string = [] for i in range(0, today.month): d = today + relativedelta(months=-1 * i) months.insert(0, d) months_string.insert(0, d.strftime("%B")) dec = today + relativedelta(months=-1 * today.month) while len(months) != 12: months.insert(0, dec) months_string.insert(0, dec.strftime("%B")) dec = dec + relativedelta(months=-1) item['graph']['xAxis'] = months_string system_sources = Source.objects.filter(system=True) sources = ['Others', 'Total'] for s in system_sources: sources.insert(0, s.value) for i in sources: if i == 'Total': if filter_by_college: apps = JobApplication.objects.filter( user__in=college_users, apply_date__range=[last_year, today], is_deleted=False) else: apps = JobApplication.objects.filter( apply_date__range=[last_year, today], is_deleted=False, user__is_demo=False) apps_by_months = apps.values( 'apply_date__year', 'apply_date__month').annotate(count=Count('pk')) elif i != 'Others': if filter_by_college: apps = JobApplication.objects.filter( user__in=college_users, app_source__value=i, apply_date__range=[last_year, today], is_deleted=False) else: apps = JobApplication.objects.filter( app_source__value=i, apply_date__range=[last_year, today], is_deleted=False, user__is_demo=False) apps_by_months = apps.values( 'apply_date__year', 'apply_date__month').annotate(count=Count('pk')) else: if filter_by_college: apps = JobApplication.objects.filter( user__in=college_users, app_source__system=False, apply_date__range=[last_year, today], is_deleted=False) else: apps = JobApplication.objects.filter( app_source__system=False, apply_date__range=[last_year, today], is_deleted=False, user__is_demo=False) apps_by_months = apps.values( 'apply_date__year', 'apply_date__month').annotate(count=Count('pk')) serie = {'name': i} data = [0] * 12 for j in range(0, 12): count = apps_by_months.filter( apply_date__year=months[j].year, apply_date__month=months[j].month) if len(count) > 0: data[j] = count[0]['count'] serie['data'] = data serie['type'] = "line" # serie['stack'] = 'Source' item['graph']['series'].append(serie) total = 0 for idx, month in enumerate(months): if filter_by_college: apps = JobApplication.objects.filter( user__in=college_users, apply_date__year=month.year, apply_date__month=month.month, is_deleted=False) else: apps = JobApplication.objects.filter( apply_date__year=month.year, apply_date__month=month.month, is_deleted=False, user__is_demo=False) item['list']['data'].append({ 'id': months_string[idx] + ' ' + str(month.year), 'value': apps.count() }) total += apps.count() item['list']['data'].sort(key=lambda x: x['value'], reverse=True) item['list']['total'] = total elif graph == 2: item['graph'] = {} item['graph']['type'] = 'radar' item['graph']['polar'] = [] item['graph']['series'] = [] item['graph']['title'] = 'Skill Analysis' item['list'] = {} item['list']['data'] = [] item['list']['title'] = 'Top Skills' # dummy data serie = {'value': [9, 8, 9, 7, 5, 3], 'name': 'Market Demand'} item['graph']['series'].append(serie) serie = {'value': [10, 8, 5, 7, 3, 2], 'name': 'Overall Skills'} item['graph']['series'].append(serie) indicators = [] indicator = {'text': 'Java', 'max': 10} indicators.append(indicator) indicator = {'text': 'Python', 'max': 10} indicators.append(indicator) indicator = {'text': 'R', 'max': 10} indicators.append(indicator) indicator = {'text': 'React Native', 'max': 10} indicators.append(indicator) indicator = {'text': 'GO', 'max': 10} indicators.append(indicator) indicator = {'text': 'React', 'max': 10} indicators.append(indicator) item['graph']['polar'].append({'indicator': indicators}) item['list']['data'].append({'id': 'Java', 'value': 27}) item['list']['data'].append({'id': 'GO', 'value': 18}) item['list']['data'].append({'id': 'React', 'value': 16}) item['list']['data'].append({'id': 'R', 'value': 14}) item['list']['data'].append({'id': 'Python', 'value': 12}) item['list']['data'].append({'id': 'React Native', 'value': 8}) item['list']['total'] = 32 elif graph == 3: item['graph'] = {} item['graph']['type'] = 'bar' item['graph']['series'] = [] item['graph']['title'] = 'Top Positions Applied' item['list'] = {} item['list']['data'] = [] item['list']['title'] = 'Top Positions Applied' today = datetime.date.today() + relativedelta(days=+1) last_year = datetime.date.today() + relativedelta(years=-1) months = [] months_string = [] if filter_by_college: distinct_positions = JobApplication.objects.filter( user__in=college_users).distinct('position', 'user') else: distinct_positions = JobApplication.objects.filter( user__is_demo=False).distinct('position', 'user') # ~Q(application_status__pk = 2) indicates not 'To Apply' statuses in the prod DB. top_positions = JobApplication.objects.filter( ~Q(application_status=None), ~Q(application_status__pk=2), apply_date__range=[last_year, today], is_deleted=False).values( position_=F('position__job_title')).annotate( count=Count('position')).filter( id__in=distinct_positions).order_by( '-count').order_by('-count') for i in range(0, today.month): d = today + relativedelta(months=-1 * i) months.insert(0, d) months_string.insert(0, d.strftime("%B")) dec = today + relativedelta(months=-1 * today.month) while len(months) != 12: months.insert(0, dec) months_string.insert(0, dec.strftime("%B")) dec = dec + relativedelta(months=-1) item['graph']['xAxis'] = months_string if top_positions.count() > 10: top_positions = top_positions[:10] total = 0 for position in top_positions: serie = {'name': position['position_'], 'type': 'bar'} data = [0] * 12 for j in range(0, 12): count = JobApplication.objects.filter( ~Q(application_status=None), ~Q(application_status__pk=2), position__job_title=position['position_'], apply_date__year=months[j].year, apply_date__month=months[j].month, is_deleted=False, id__in=distinct_positions) data[j] = count.count() serie['data'] = data item['graph']['series'].append(serie) serie['stack'] = 'Company' item['list']['data'].append({ 'id': position['position_'], 'value': position['count'] }) total += position['count'] item['list']['total'] = total response.append(item) return JsonResponse(create_response(data=response), safe=False)
def coaches(request): user_profile = request.user body = request.data if request.method == "GET": if not request.user.user_type.coach_listing_enabled: return JsonResponse(create_response( data=None, success=False, error_code=ResponseCodes.not_supported_user), safe=False) college_coaches = CollegeCoach.objects.filter( college=request.user.college) paginator = pagination.CustomPagination() college_coaches = paginator.paginate_queryset(college_coaches, request) serialized_college_coaches = CollegeCoachSerializer( instance=college_coaches, many=True).data return JsonResponse(create_response(data=serialized_college_coaches, paginator=paginator), safe=False) elif user_profile.user_type.name == 'Career Service': if request.method == "DELETE": coach = HomePageVideo.objects.get(pk=body['coach_id']) coach.delete() return JsonResponse(create_response(data=None), safe=False) elif request.method == "POST" and user_profile.user_type.name == 'Career Service': coach = CollegeCoach() coach.first_name = body['first_name'] coach.last_name = body['last_name'] coach.title = body['title'] if 'email' in body: coach.email = body['email'] coach.content = body['content'] coach.calendar_link = body['calendar_link'] if 'online_conference_link' in body: coach.online_conference_link = body['online_conference_link'] coach.college = user_profile.college if 'is_publish' in body: coach.is_publish = get_boolean_from_request( request, 'is_publish', 'POST') else: coach.is_publish = True file = body['profile_photo'] ext = file.name.split('.')[-1] filename = "%s.%s" % (uuid.uuid4(), ext) coach.profile_photo.save(filename, file, save=True) file = body['summary_photo'] ext = file.name.split('.')[-1] filename = "%s.%s" % (uuid.uuid4(), ext) coach.summary_photo.save(filename, file, save=True) coach.save() return JsonResponse(create_response( data=CollegeCoachSerializer(instance=coach, many=False).data), safe=False) elif request.method == "PUT" and user_profile.user_type.name == 'Career Service': coach = CollegeCoach.objects.get(pk=body['coach_id']) if 'first_name' in body: coach.first_name = body['first_name'] if 'last_name' in body: coach.last_name = body['last_name'] if 'title' in body: coach.title = body['title'] if 'email' in body: coach.email = body['email'] if 'content' in body: coach.content = body['content'] if 'calendar_link' in body: coach.calendar_link = body['calendar_link'] if 'online_conference_link' in body: coach.online_conference_link = body['online_conference_link'] if 'profile_photo' in body: file = body['profile_photo'] ext = file.name.split('.')[-1] filename = "%s.%s" % (uuid.uuid4(), ext) coach.profile_photo.save(filename, file, save=True) if 'summary_photo' in body: file = body['summary_photo'] ext = file.name.split('.')[-1] filename = "%s.%s" % (uuid.uuid4(), ext) coach.summary_photo.save(filename, file, save=True) if 'is_publish' in body: coach.is_publish = get_boolean_from_request( request, 'is_publish', 'POST') coach.save() return JsonResponse(create_response( data=CollegeCoachSerializer(instance=coach, many=False).data), safe=False) return JsonResponse(create_response( data=None, success=False, error_code=ResponseCodes.not_supported_user), safe=False)
def blogs(request): user_profile = request.user if request.method == "GET": if user_profile.user_type.name == 'Career Service' and get_boolean_from_request(request, 'waiting'): queryset = Blog.objects.filter(is_approved=False, is_publish=True, is_rejected=False, college=user_profile.college) elif request.user.user_type.name == 'Career Service' and request.GET.get('type', '') != '': queryset = Blog.objects.filter(Q(is_publish=True, is_rejected=False, is_approved=True) | Q(publisher_profile=request.user), publisher_profile__user_type__id=int(request.GET.get('type')), college=request.user.college) else: if user_profile.user_type.name == 'Career Service': student = get_boolean_from_request(request, 'student') if student: user_type = UserType.objects.get(name='Student') else: user_type = UserType.objects.get(name='Alumni') queryset = Blog.objects.filter(is_approved=True, is_publish=True, college=user_profile.college, user_types__in=[user_type]) else: if user_profile.user_type.name == 'Public': queryset = Blog.objects.filter(Q(is_approved=True, is_publish=True) | Q(publisher_profile=request.user), Q(user_types__in=[user_profile.user_type]) | Q(publisher_profile__is_superuser=True)) else: queryset = Blog.objects.filter(Q(is_approved=True, is_publish=True) | Q(publisher_profile=request.user), Q(user_types__in=[user_profile.user_type],college=user_profile.college) | Q(publisher_profile__is_superuser=True)) queryset = queryset.filter(publisher_profile__isnull=False) paginator = pagination.CustomPagination() blogs = paginator.paginate_queryset(queryset, request) serialized_blogs = BlogSnippetSerializer( instance=blogs, many=True, context={'user': request.user}).data return JsonResponse(create_response(data=serialized_blogs, paginator=paginator), safe=False) else: if not user_profile.user_type.blog_creation_enabled: return JsonResponse(create_response(data=None, success=False, error_code=ResponseCodes.not_supported_user), safe=False) if request.method == "POST": body = request.data blog = Blog() blog.college = request.user.college blog.publisher_profile = request.user if 'header_image' in body: file = body['header_image'] ext = file.name.split('.')[-1] filename = "%s.%s" % (uuid.uuid4(), ext) blog.header_image.save(filename, file, save=True) if 'title' in body: title = body['title'] blog.title = title if 'content' in body: content = body['content'] blog.content = content if 'snippet' in body: snippet = body['snippet'][:130] + '...' blog.snippet = snippet if 'is_publish' in body: is_publish = get_boolean_from_request(request, 'is_publish', 'POST') blog.is_publish = is_publish if request.user.user_type.name == 'Career Service': user_types = body['user_types'].split(',') blog.save() for type in user_types: user_type = UserType.objects.get(pk=int(type)) blog.user_types.add(user_type) blog.is_approved = True else: blog.user_types.add(request.user.user_type) if blog.is_publish: send_notification_email_to_admins('blog', blog.college.id) blog.is_approved = False blog.save() return JsonResponse(create_response(data={"id": blog.id}), safe=False) elif request.method == "PUT": body = request.data blog = Blog.objects.get(pk=body['blog_id'], publisher_profile=request.user) if 'title' in body: blog.title = body['title'] if 'content' in body: blog.content = body['content'] blog.snippet = body['snippet'][:130] + '...' if 'header_image' in body: file = body['header_image'] ext = file.name.split('.')[-1] filename = "%s.%s" % (uuid.uuid4(), ext) blog.header_image.save(filename, file, save=True) if 'is_publish' in body: blog.is_publish = get_boolean_from_request(request, 'is_publish', 'POST') if request.user.user_type.name == 'Career Service': user_types = body['user_types'].split(',') blog.user_types.clear() for type in user_types: user_type = UserType.objects.get(pk=int(type)) blog.user_types.add(user_type) blog.is_approved = True else: if blog.is_publish: send_notification_email_to_admins('blog', blog.college.id) blog.is_approved = False blog.updated_at = timezone.now() blog.save() return JsonResponse(create_response(data={"id": blog.id}), safe=False) elif request.method == "PATCH": if request.user.user_type.name == 'Career Service': body = request.data blog = Blog.objects.get(pk=body['blog_id']) approved = body['approved'] blog.is_approved = approved blog.is_rejected = not approved blog.save() return JsonResponse(create_response(data=None), safe=False) else: return JsonResponse( create_response(data=None, success=False, error_code=ResponseCodes.not_supported_user), safe=False) elif request.method == "DELETE": body = request.data blog = Blog.objects.get(pk=body['blog_id'], publisher_profile=request.user) blog.delete() return JsonResponse(create_response(data=None), safe=False)
def blogs(request): user_profile = Profile.objects.get(user=request.user) if request.method == "GET": mine = request.GET.get('mine') if mine is None: if user_profile.user_type >= int(Profile.UserTypes.student): queryset = Blog.objects.filter( Q(is_approved=True) | Q(publisher_profile=request.user)) else: queryset = Blog.objects.filter(is_approved=True, is_public=True) else: queryset = Blog.objects.filter(publisher_profile=request.user) paginator = pagination.CustomPagination() blogs = paginator.paginate_queryset(queryset, request) serialized_blogs = BlogSnippetSerializer(instance=blogs, many=True, context={ 'user': request.user }).data return JsonResponse(create_response(data=serialized_blogs, paginator=paginator), safe=False) else: if user_profile.user_type < int(Profile.UserTypes.student): return JsonResponse(create_response( data=None, success=False, error_code=ResponseCodes.not_supported_user), safe=False) if request.method == "POST": body = request.data blog = Blog() if 'header_image' in body: file = body['header_image'] ext = file.name.split('.')[-1] filename = "%s.%s" % (uuid.uuid4(), ext) blog.header_image.save(filename, file, save=True) if 'title' in body: title = body['title'] blog.title = title if 'content' in body: content = body['content'] blog.content = content if 'snippet' in body: snippet = body['snippet'][:130] + '...' blog.snippet = snippet if 'is_publish' in body: is_publish = get_boolean_from_request(request, 'is_publish', request.method) blog.is_publish = is_publish send_notification_email_to_admins('blog') if 'is_public' in body: is_public = get_boolean_from_request(request, 'is_public', request.method) blog.is_public = is_public blog.publisher_profile = request.user blog.save() return JsonResponse(create_response(data={"id": blog.id}), safe=False) elif request.method == "PUT": body = request.data blog = Blog.objects.get(pk=body['blog_id'], publisher_profile=request.user) if 'title' in body: blog.title = body['title'] if 'content' in body: blog.content = body['content'] blog.snippet = body['snippet'][:130] + '...' if 'header_image' in body: file = body['header_image'] ext = file.name.split('.')[-1] filename = "%s.%s" % (uuid.uuid4(), ext) blog.header_image.save(filename, file, save=True) if 'is_publish' in body: blog.is_publish = get_boolean_from_request( request, 'is_publish', request.method) if 'is_public' in body: blog.is_public = get_boolean_from_request( request, 'is_public', request.method) blog.is_approved = False blog.updated_at = datetime.now() blog.save() send_notification_email_to_admins('blog') return JsonResponse(create_response(data={"id": blog.id}), safe=False) elif request.method == "DELETE": body = request.data blog = Blog.objects.get(pk=body['blog_id'], publisher_profile=request.user) blog.delete() return JsonResponse(create_response(data=None), safe=False)
def reviews(request): body = request.data user = request.user if 'recaptcha_token' in body and utils.verify_recaptcha( user, body['recaptcha_token'], 'review') == ResponseCodes.verify_recaptcha_failed: return JsonResponse(create_response( data=None, success=False, error_code=ResponseCodes.verify_recaptcha_failed), safe=False) if request.method == "GET" and user.user_type.name == 'Career Service': reviews_list = Review.objects.filter(is_published=False, is_rejected=False, user__college=user.college) return JsonResponse(create_response( data=ReviewSerializer(instance=reviews_list, many=True).data), safe=False) elif request.method == "PATCH": if request.user.user_type.name == 'Career Service': body = request.data review = Review.objects.get(pk=body['review_id']) approved = body['approved'] review.is_published = approved review.is_rejected = not approved review.save() return JsonResponse(create_response(data=None), safe=False) else: return JsonResponse(create_response( data=None, success=False, error_code=ResponseCodes.not_supported_user), safe=False) elif request.method == "GET": company_id = request.GET.get('company_id') position_id = request.GET.get('position_id') all_reviews = get_boolean_from_request(request, 'all_reviews') review_id = request.GET.get('review_id') if review_id is not None: reviews_list = Review.objects.filter(pk=review_id, user=request.user) if reviews_list.count() == 0: return JsonResponse(create_response( data=None, success=False, error_code=ResponseCodes.record_not_found), safe=False) return JsonResponse(create_response(data=ReviewSerializer( instance=reviews_list[0], many=False).data), safe=False) elif company_id is None and position_id is None: return JsonResponse(create_response( data=None, success=False, error_code=ResponseCodes.invalid_parameters), safe=False) if company_id is None: reviews_list = Review.objects.filter(Q(is_published=True) | Q(user=request.user), position__pk=position_id) elif position_id is None: reviews_list = Review.objects.filter(Q(is_published=True) | Q(user=request.user), company__pk=company_id) else: if all_reviews: reviews_list = Review.objects.filter(is_published=True, position__pk=position_id, company__pk=company_id) else: reviews_list = Review.objects.filter(user=request.user, position__pk=position_id, company__pk=company_id) if reviews_list.count() > 0: return JsonResponse(create_response(data=ReviewSerializer( instance=reviews_list[0], many=False).data), safe=False) else: return JsonResponse(create_response(data=None), safe=False) return JsonResponse(create_response( data=ReviewSerializer(instance=reviews_list, many=True).data), safe=False) else: if 'company_id' not in body or 'position_id' not in body: return JsonResponse(create_response( data=None, success=False, error_code=ResponseCodes.invalid_parameters), safe=False) company = Company.objects.get(pk=body['company_id']) position = JobPosition.objects.get(pk=body['position_id']) if request.method == "PUT": review = Review.objects.get(pk=body['review_id']) if review.user.pk != user.pk: return JsonResponse(create_response( data=None, success=False, error_code=ResponseCodes.record_not_found), safe=False) review.update_date = timezone.now() elif request.method == "POST": review = Review() review.company = company review.position = position review.user = request.user if 'pros' in body: review.pros = body['pros'] if 'cons' in body: review.cons = body['cons'] if 'interview_notes' in body: review.interview_notes = body['interview_notes'] if 'overall_company_experience' in body: review.overall_company_experience = body[ 'overall_company_experience'] if 'interview_difficulty' in body: review.interview_difficulty = body['interview_difficulty'] if 'overall_interview_experience' in body: review.overall_interview_experience = body[ 'overall_interview_experience'] if 'anonymous' in body: review.anonymous = body['anonymous'] if 'emp_auths' in body: for a in body['emp_auths']: if 'value' in a: auth = EmploymentAuth.objects.get(pk=a['id']) review.save() if CompanyEmploymentAuth.objects.filter( review=review, employment_auth=auth).count() == 0: c_auth = CompanyEmploymentAuth(review=review, employment_auth=auth, value=a['value']) c_auth.save() else: c_auth = CompanyEmploymentAuth.objects.get( review=review, employment_auth=auth) c_auth.value = a['value'] c_auth.save() if 'emp_status_id' in body: review.emp_status = EmploymentStatus.objects.get( pk=body['emp_status_id']) if 'source_type_id' in body: review.source_type = SourceType.objects.get( pk=body['source_type_id']) # publish review if there is no content to approve if 'pros' not in body and 'cons' not in body and 'interview_notes' not in body: review.is_published = True else: review.is_published = False send_notification_email_to_admins('review', review.user.college.id) review.save() response = { 'review': ReviewSerializer(instance=review, many=False).data, 'company': CompanySerializer(instance=company, many=False, context={ 'user': request.user, 'position': position }).data } return JsonResponse(create_response(data=response), safe=False)
def events(request): if request.method == "GET": if request.user.user_type.name == 'Career Service' and get_boolean_from_request(request, 'waiting'): queryset = Event.objects.filter(is_approved=False, is_publish=True, is_rejected=False, college=request.user.college).order_by('-updated_at') elif request.user.user_type.name == 'Career Service' and request.GET.get('type', '') != '': queryset = Event.objects.filter(Q(is_publish=True, is_rejected=False, is_approved=True) | Q(host_user=request.user), host_user__user_type__id=int(request.GET.get('type')), college=request.user.college).order_by('-updated_at') else: attended = get_boolean_from_request(request, 'attended') if not attended: user_profile = request.user if user_profile.user_type.name == 'Career Service': student = get_boolean_from_request(request, 'student') if student: user_type = UserType.objects.get(name='Student') else: user_type = UserType.objects.get(name='Alumni') queryset = Event.objects.filter(is_approved=True, is_publish=True, college=user_profile.college, user_types__in=[user_type]) else: if user_profile.user_type.name == 'Public': queryset = Event.objects.filter(Q(is_approved=True, is_publish=True) | Q(host_user=request.user), Q(user_types__in=[user_profile.user_type]) | Q(host_user__is_superuser=True)) else: queryset = Event.objects.filter(Q(is_approved=True, is_publish=True) | Q(host_user=request.user), Q(user_types__in=[user_profile.user_type], college=user_profile.college) | Q(host_user__is_superuser=True)) else: attended_events = EventAttendee.objects.filter(user=request.user) queryset = Event.objects.all().filter(id__in=[e.event.id for e in attended_events]) queryset = queryset.filter(host_user__isnull=False) paginator = pagination.CustomPagination() event_list = paginator.paginate_queryset(queryset, request) serialized_events = EventSimpleSerializer( instance=event_list, many=True, context={'user': request.user, 'detailed': False}).data return JsonResponse(create_response(data=serialized_events, paginator=paginator), safe=False) elif request.method == "DELETE": body = request.data event = Event.objects.get(pk=body['event_id'], host_user=request.user) event.delete() return JsonResponse(create_response(data=None), safe=False) elif request.method == "PATCH": if request.user.user_type.name == 'Career Service': body = request.data event = Event.objects.get(pk=body['event_id']) approved = body['approved'] event.is_approved = approved event.is_rejected = not approved event.save() return JsonResponse(create_response(data=None), safe=False) else: return JsonResponse( create_response(data=None, success=False, error_code=ResponseCodes.not_supported_user), safe=False) else: user_profile = request.user if not user_profile.user_type.event_creation_enabled: return JsonResponse(create_response(data=None, success=False, error_code=ResponseCodes.not_supported_user), safe=False) body = request.data if request.method == "POST": event = Event() if request.user.user_type.name != 'Career Service': event.user_types.add(request.user.user_type) else: event.save() user_types = body['user_types'].split(',') for type in user_types: user_type = UserType.objects.get(pk=type) event.user_types.add(user_type) event.college = request.user.college else: event = Event.objects.get(pk=body['event_id']) event.updated_at = timezone.now() if request.user.user_type.name == 'Career Service': event.user_types.clear() user_types = body['user_types'].split(',') for type in user_types: user_type = UserType.objects.get(pk=type) event.user_types.add(user_type) event.host_user = request.user if 'title' in body: event.title = body['title'] if 'short_description' in body: event.short_description = body['short_description'] if 'details' in body: event.details = body['details'] if 'location_lat' in body: event.location_lat = body['location_lat'] if 'location_lon' in body: event.location_lon = body['location_lon'] if 'location_title' in body: event.location_title = body['location_title'] if 'location_address' in body: event.location_address = body['location_address'] if 'event_date_start' in body: event.event_date_start = body['event_date_start'] if 'event_date_end' in body: event.event_date_end = body['event_date_end'] if 'event_type_id' in body: event.event_type = EventType.objects.get(pk=int(body['event_type_id'])) if 'spot_count' in body: event.spot_count = int(body['spot_count']) if 'header_image' in body: file = body['header_image'] ext = file.name.split('.')[-1] filename = "%s.%s" % (uuid.uuid4(), ext) event.header_image.save(filename, file, save=True) if 'is_publish' in body: event.is_publish = get_boolean_from_request(request, 'is_publish', 'POST') if request.user.user_type.name == 'Career Service': event.is_approved = True else: if event.is_publish: send_notification_email_to_admins('event', event.college.id) event.is_approved = False event.save() return JsonResponse(create_response(data={"id": event.id}), safe=False)
def events(request): if request.method == "GET": attended = get_boolean_from_request(request, 'attended') if not attended: user_profile = request.user if user_profile.user_type >= int(User.UserTypes.student): queryset = Event.objects.filter(is_approved=True) else: queryset = Event.objects.filter(is_approved=True, is_public=True) else: attended_events = EventAttendee.objects.filter(user=request.user) queryset = Event.objects.all().filter(id__in=[e.event.id for e in attended_events]) queryset = queryset.filter(host_user__isnull=False) paginator = pagination.CustomPagination() event_list = paginator.paginate_queryset(queryset, request) serialized_events = EventSimpleSerializer( instance=event_list, many=True, context={'user': request.user, 'detailed': False}).data return JsonResponse(create_response(data=serialized_events, paginator=paginator), safe=False) elif request.method == "DELETE": user_profile = request.user if user_profile.user_type < int(User.UserTypes.student): return JsonResponse(create_response(data=None, success=False, error_code=ResponseCodes.not_supported_user), safe=False) body = request.data event = Event.objects.get(pk=body['event_id'], host_user=request.user) event.delete() return JsonResponse(create_response(data=None), safe=False) else: user_profile = request.user if user_profile.user_type < int(User.UserTypes.student): return JsonResponse(create_response(data=None, success=False, error_code=ResponseCodes.not_supported_user), safe=False) body = request.data if request.method == "POST": event = Event() else: event = Event.objects.get(pk=body['event_id']) event.updated_at = datetime.now() event.host_user = request.user if 'title' in body: event.title = body['title'] if 'short_description' in body: event.short_description = body['short_description'] if 'details' in body: event.details = body['details'] if 'location_lat' in body: event.location_lat = body['location_lat'] if 'location_lon' in body: event.location_lon = body['location_lon'] if 'location_address' in body: event.location_address = body['location_address'] if 'event_date_start' in body: event.event_date_start = body['event_date_start'] if 'event_date_end' in body: event.event_date_end = body['event_date_end'] if 'event_type_id' in body: event.event_type = EventType.objects.get(pk=body['event_type_id']) if 'spot_count' in body: event.spot_count = body['spot_count'] if 'header_image' in body: file = body['header_image'] ext = file.name.split('.')[-1] filename = "%s.%s" % (uuid.uuid4(), ext) event.header_image.save(filename, file, save=True) if 'is_public' in body: event.is_public = get_boolean_from_request(request, 'is_public') if 'is_publish' in body: event.is_publish = get_boolean_from_request(request, 'is_publish') event.is_approved = False event.save() send_notification_email_to_admins('event') return JsonResponse(create_response(data={"id": event.id}), safe=False)