Beispiel #1
0
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)
Beispiel #2
0
def colleges(request):
    q = request.GET.get('q')
    if q is None:
        colleges = College.objects.all()
    else:
        colleges = College.objects.filter(name__icontains=q)
    paginator = pagination.CustomPagination()
    colleges = paginator.paginate_queryset(colleges, request)
    serialized_colleges = CollegeSerializer(
        instance=colleges, many=True, ).data
    return JsonResponse(create_response(data=serialized_colleges, paginator=paginator), safe=False)
Beispiel #3
0
def home_page_videos(request):
    user_profile = request.user
    if user_profile.user_type.name == 'Career Service':
        body = request.data
        if request.method == "GET":
            homepage_videos = HomePageVideo.objects.filter(
                college=request.user.college)
            paginator = pagination.CustomPagination()
            homepage_videos = paginator.paginate_queryset(
                homepage_videos, request)
            serialized_homepage_videos = HomePageVideoSerializer(
                instance=homepage_videos, many=True).data
            return JsonResponse(create_response(
                data=serialized_homepage_videos, paginator=paginator),
                                safe=False)
        elif request.method == "DELETE" and user_profile.user_type.name == 'Career Service':
            home_page_video = HomePageVideo.objects.get(
                pk=body['homepage_video_id'])
            home_page_video.delete()
            return JsonResponse(create_response(data=None), safe=False)
        elif request.method == "POST" and user_profile.user_type.name == 'Career Service':
            home_page_video = HomePageVideo()
            if 'is_publish' in body:
                home_page_video.is_publish = body['is_publish']
            else:
                home_page_video.is_publish = True
            home_page_video.embed_code = body['embed_code']
            if 'title' in body:
                home_page_video.title = body['title']
            if 'description' in body:
                home_page_video.description = body['description']
            home_page_video.college = user_profile.college
            home_page_video.save()
            return JsonResponse(create_response(data=HomePageVideoSerializer(
                instance=home_page_video, many=False).data),
                                safe=False)
        elif request.method == "PUT" and user_profile.user_type.name == 'Career Service':
            home_page_video = HomePageVideo.objects.get(
                pk=body['homepage_video_id'])
            if 'embed_code' in body:
                home_page_video.embed_code = body['embed_code']
            if 'title' in body:
                home_page_video.title = body['title']
            if 'description' in body:
                home_page_video.description = body['description']
            if 'is_publish' in body:
                home_page_video.is_publish = body['is_publish']
            home_page_video.save()
            return JsonResponse(create_response(data=HomePageVideoSerializer(
                instance=home_page_video, many=False).data),
                                safe=False)
    return JsonResponse(create_response(
        data=None, success=False, error_code=ResponseCodes.not_supported_user),
                        safe=False)
Beispiel #4
0
def blogs(request):
    queryset = models.Blog.objects.all()
    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)
Beispiel #5
0
def companies(request):
    q = request.GET.get('q')
    companies = Company.objects.all()
    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)
Beispiel #6
0
def majors(request):
    q = request.GET.get('q')
    if q is None:
        majors = Major.objects.all()
    else:
        majors = Major.objects.filter(name__icontains=q)
    paginator = pagination.CustomPagination()
    majors = paginator.paginate_queryset(majors, request)
    serialized_majors = MajorSerializer(
        instance=majors,
        many=True,
    ).data
    return JsonResponse(create_response(data=serialized_majors,
                                        paginator=paginator),
                        safe=False)
Beispiel #7
0
def alumni(request):
    user_profile = Profile.objects.get(user=request.user)
    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)
    alumni_list = Profile.objects.filter(user_type=int(
        Profile.UserTypes.alumni),
                                         college__pk=user_profile.college.id)

    q = request.GET.get('q')
    year = request.GET.get('year')
    major_id = request.GET.get('major_id')
    company_id = request.GET.get('company_id')
    position_id = request.GET.get('position_id')
    country_id = request.GET.get('country_id')
    state_id = request.GET.get('state_id')
    if q is not None:
        alumni_list = alumni_list.filter(
            Q(user__first_name__icontains=q) | Q(user__last_name__icontains=q))
    if year is not None:
        alumni_list = alumni_list.filter(grad_year=year)
    if major_id is not None:
        alumni_list = alumni_list.filter(major__pk=major_id)
    if company_id is not None:
        alumni_list = alumni_list.filter(company__pk=company_id)
    if position_id is not None:
        alumni_list = alumni_list.filter(job_position__pk=position_id)
    if country_id is not None:
        alumni_list = alumni_list.filter(country__pk=country_id)
    if state_id is not None:
        alumni_list = alumni_list.filter(state__pk=state_id)

    paginator = pagination.CustomPagination()
    alumni_list = paginator.paginate_queryset(alumni_list, request)
    serialized_alumni = AlumniSerializer(instance=alumni_list,
                                         many=True,
                                         context={
                                             'user': request.user
                                         }).data
    return JsonResponse(create_response(data=serialized_alumni,
                                        paginator=paginator),
                        safe=False)
Beispiel #8
0
def positions(request):
    q = request.GET.get('q')
    if q:
        positions = JobPosition.objects.filter(job_title__icontains=q)
    else:
        positions = JobPosition.objects.all()

    if request.GET.get('count') is not None:
        cnt = int(request.GET.get('count'))
        positions = positions[:cnt]

    paginator = pagination.CustomPagination()
    positions = paginator.paginate_queryset(positions, request)
    serialized_positions = JobPositionSerializer(instance=positions,
                                                 many=True).data
    return JsonResponse(create_response(data=serialized_positions,
                                        paginator=paginator),
                        safe=False)
Beispiel #9
0
def company_positions(request):
    body = request.data
    if request.method == "GET":
        company_id = request.GET.get('id')
        q = request.GET.get('q')
        department = request.GET.get('department')
        job_type = request.GET.get('type')
        if q is not None and company_id is not None:
            # jobs = JobPosition.objects.filter(job_title__icontains=q)
            positions = PositionDetail.objects.filter(
                company_id=company_id, is_deleted=False,
                job__icontains=q).order_by("-updated_date")
        elif q is None and company_id is None:
            positions = PositionDetail.objects.filter(
                is_deleted=False).order_by("-updated_date")
        elif q is None and company_id is not None:
            positions = PositionDetail.objects.filter(
                company_id=company_id,
                is_deleted=False).order_by("-updated_date")
        elif q is not None and company_id is None:
            positions = PositionDetail.objects.filter(
                is_deleted=False, job__icontains=q).order_by("-updated_date")

        if department is not None:
            positions = positions.filter(department=department)
        if job_type is not None:
            positions = positions.filter(job_type=job_type)
        paginator = pagination.CustomPagination()
        positions = paginator.paginate_queryset(positions, request)
        serialized_positions = PositionDetailSerializer(instance=positions,
                                                        many=True).data
        return JsonResponse(create_response(data=serialized_positions,
                                            paginator=paginator),
                            safe=False)
    elif request.method == "POST":
        job_title = body["job_title"]
        responsibilities = body["responsibilities"]
        requirements = body["requirements"]
        department = body["department"]
        job_type = body["job_type"]
        city = body["city"]
        company_id = int(body["company_id"])
        #job = JobPosition.objects.get(job_title=job_title)
        job = job_title
        state = State.objects.get(pk=body["state_id"])
        country = Country.objects.get(pk=body["country_id"])
        company = Company.objects.get(pk=body["company_id"])
        new_position = PositionDetail(job=job,
                                      responsibilities=responsibilities,
                                      requirements=requirements,
                                      department=department,
                                      job_type=job_type,
                                      city=city,
                                      country=country,
                                      state=state,
                                      company=company)

        new_position.save()
        return JsonResponse(create_response(data=PositionDetailSerializer(
            instance=new_position, many=False, context={
                'user': request.user
            }).data),
                            safe=False)
    elif request.method == "DELETE":
        position_id = body["position_id"]
        position = PositionDetail.objects.get(pk=position_id)
        position.is_deleted = True
        position.save()
        return JsonResponse(create_response(data=None), safe=False)
    elif request.method == "PATCH":
        position_id = body["position_id"]
        position = PositionDetail.objects.get(pk=position_id)
        job = body.get('job_title')
        responsibilities = body.get('responsibilities')
        requirements = body.get('requirements')
        department = body.get('department')
        job_type = body.get('job_type')
        city = body.get('city')
        state_id = body.get('state_id')
        country_id = body.get('country_id')

        if responsibilities is not None:
            position.responsibilities = responsibilities
        if job is not None:
            position.job = job
        if requirements is not None:
            position.requirements = requirements
        if department is not None:
            position.department = department
        if job_type is not None:
            position.job_type = job_type
        if city is not None:
            position.city = city
        if state_id is not None:
            position.state_id = state_id
        if country_id is not None:
            position.country_id = country_id

        position.save()
        return JsonResponse(create_response(data=PositionDetailSerializer(
            instance=position, many=False, context={
                'user': request.user
            }).data),
                            safe=False)
Beispiel #10
0
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)
Beispiel #11
0
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)
Beispiel #12
0
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)
Beispiel #13
0
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)
Beispiel #14
0
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)