Esempio n. 1
0
def change(request):
    response = {}
    data = request.GET
    session = request.session
    if not session.get(LOGIN_ID) or not session.get(LOGIN_TYPE):
        response['result'] = "error"
        response['message'] = "Login Required"
        return JsonResponse(json.dumps(response, indent=4, sort_keys=True),
                            safe=False)

    if session[LOGIN_TYPE] != LoginType.EMP:
        response['result'] = "error"
        response['message'] = "Not a User"
        return JsonResponse(json.dumps(response, indent=4, sort_keys=True),
                            safe=False)

    user_id = session[LOGIN_ID]
    user = UserDB.user(uno=user_id)
    if not user.valid():
        response['result'] = "error"
        response['message'] = "Login Required"
        return JsonResponse(json.dumps(response, indent=4, sort_keys=True),
                            safe=False)

    u_name = data.get('name')
    u_email = data.get('email')
    u_website = data.get('website')
    u_description = data.get('description')

    if ('name' in data and not u_name) or ('email' in data and not u_email):
        response['result'] = "error"
        response['message'] = "No Input"
        return JsonResponse(json.dumps(response, indent=4, sort_keys=True),
                            safe=False)

    if u_name:
        user['name'] = u_name

    if u_email:
        user['email'] = u_email

    if 'website' in data:
        user['website'] = u_website

    if 'description' in data:
        user['description'] = u_description

    if not user.valid():
        response['result'] = "error"
        response['message'] = user.error.value
        return JsonResponse(json.dumps(response, indent=4, sort_keys=True),
                            safe=False)

    result = UserDB.update(user)

    if not result:
        response['result'] = "error"
        response['message'] = "Something Wrong Happened"
        return JsonResponse(json.dumps(response, indent=4, sort_keys=True),
                            safe=False)

    if u_name:
        name = user['name']
        name_arr = name.split()
        name = ""
        for i in name_arr:
            name += i[0]
        response['user_initials'] = name

    response['result'] = "success"
    response['message'] = "Changed Successfully"
    return JsonResponse(json.dumps(response, indent=4, sort_keys=True),
                        safe=False)
Esempio n. 2
0
    def get(self, request, user=None):
        session = request.session
        if not session.get(LOGIN_ID) or not session.get(LOGIN_TYPE):
            request.session[ADD_EMP_RESULT] = AddResult.SOMETHING_ELSE.value
            return HttpResponseRedirect("/profile")

        if session.get(LOGIN_TYPE) != LoginType.ORG or not user:
            request.session[ADD_EMP_RESULT] = AddResult.SOMETHING_ELSE.value
            return HttpResponseRedirect("/profile")

        org_id = request.session[LOGIN_ID]
        org = OrganizationDB.organization(uno=org_id)
        if not org.valid():
            request.session[AUTH_RESULT] = LoginResult.SOMETHING_ELSE.value
            del request.session[LOGIN_ID]
            del request.session[LOGIN_TYPE]
            return HttpResponseRedirect("/profile")

        if 'mod' not in request.GET:
            request.session[ADD_EMP_RESULT] = AddResult.SOMETHING_ELSE.value
            return HttpResponseRedirect("/profile")

        mod = request.GET['mod']
        user = UserDB.user(uno=user)

        if not user.valid():
            request.session[ADD_EMP_RESULT] = AddResult.EMP_NOT_VALID.value
            return HttpResponseRedirect("/profile")

        if mod == "GN":
            if user.get('organization'):
                request.session[ADD_EMP_RESULT] = AddResult.EMP_NOT_FREE.value
                return HttpResponseRedirect("/profile")

            user['organization'] = org_id
            user['role'] = 'GN'
            org.add(user['_id'])

        else:
            if user['organization'] != org['_id'] \
            or user['role'] != "GN":
                request.session[ADD_EMP_RESULT] = AddResult.EMP_NOT_FREE.value
                return HttpResponseRedirect("/profile")

            if mod == "HR":
                org.set_emp(Organization.EmpType.HR, user['_id'])
                user['role'] = "HR"
            elif mod == "SU":
                org.set_emp(Organization.EmpType.SU, user['_id'])
                user['role'] = "SU"
            elif mod == "PS":
                org.set_emp(Organization.EmpType.PS, user['_id'])
                user['role'] = "PS"
            else:
                request.session[
                    ADD_EMP_RESULT] = AddResult.SOMETHING_ELSE.value
                return HttpResponseRedirect("/profile")

        UserDB.update(user)
        OrganizationDB.update(org)

        request.session[ADD_EMP_RESULT] = AddResult.ADDED.value
        return HttpResponseRedirect("/profile")
Esempio n. 3
0
    def get(self, request, user=None):
        if LOGIN_ID not in request.session \
        or LOGIN_TYPE not in request.session:
            request.session[ADD_EMP_RESULT] = RemoveResult.SOMETHING_ELSE.value
            return HttpResponseRedirect("/profile")

        if request.session[LOGIN_TYPE] != LoginType.ORG \
        or not user:
            request.session[ADD_EMP_RESULT] = RemoveResult.SOMETHING_ELSE.value
            return HttpResponseRedirect("/profile")

        org_id = request.session[LOGIN_ID]
        org = OrganizationDB.organization(uno=org_id)
        if not org.valid():
            request.session[AUTH_RESULT] = LoginResult.SOMETHING_ELSE.value
            del request.session[LOGIN_ID]
            del request.session[LOGIN_TYPE]
            return HttpResponseRedirect("/profile")

        user = UserDB.user(uno=user)
        print("1")

        if not user.valid():
            print("1")
            request.session[ADD_EMP_RESULT] = RemoveResult.EMP_NOT_VALID.value
            return HttpResponseRedirect("/profile")

        if user['organization'] != org['_id']:
            print("1")
            request.session[AUTH_RESULT] = RemoveResult.SOMETHING_ELSE.value
            del request.session[LOGIN_ID]
            del request.session[LOGIN_TYPE]
            return HttpResponseRedirect("/profile")

        if user['role'] == "GN":
            org.remove(user['_id'])
            user['role'] = None
            user['organization'] = None

        else:
            if user['role'] == "HR":
                org.remove_emp(Organization.EmpType.HR)
                org.add(user['_id'])
                user['role'] = "GN"
            elif user['role'] == "SU":
                org.remove_emp(Organization.EmpType.SU)
                org.add(user['_id'])
                user['role'] = "GN"
            elif user['role'] == "PS":
                org.remove_emp(Organization.EmpType.PS)
                org.add(user['_id'])
                user['role'] = "GN"
            else:
                print("1")
                request.session[
                    ADD_EMP_RESULT] = RemoveResult.SOMETHING_ELSE.value
                return HttpResponseRedirect("/profile")

        UserDB.update(user)
        OrganizationDB.update(org)

        request.session[ADD_EMP_RESULT] = RemoveResult.REMOVED.value
        return HttpResponseRedirect("/profile")
Esempio n. 4
0
    def get(self, request, test_id):
        context = {}
        found_sub = None
        login_id = request.session.get(LOGIN_ID)
        login_type = request.session.get(LOGIN_TYPE)

        test = TestModuleDB.test_module(uno=test_id)
        if not test.valid():
            return Http404

        if not test.is_live():
            return HttpResponseRedirect("/test/" + test_id)

        if not login_id or not login_type:
            return HttpResponseRedirect("/login?r=/test/start/" + test_id)

        if login_type == LoginType.ORG:
            org = OrganizationDB.organization(uno=login_id)
            if org.valid():
                context['org'] = org
                context['login'] = True
            else:
                del request.session[LOGIN_ID]
                request.session[AUTH_RESULT] = LoginResult.SOMETHING_ELSE
                return HttpResponseRedirect("/login?r=/test/start/" + test_id)

        elif login_type == LoginType.EMP:
            user = UserDB.user(uno=login_id)
            if user.valid():
                user_name_arr = user['name'].split()
                user_init = ""
                for i in user_name_arr:
                    user_init += i[0]
                user['initial'] = user_init
                context['user'] = user
                context['login'] = True
            else:
                del request.session[LOGIN_ID]
                request.session[AUTH_RESULT] = LoginResult.SOMETHING_ELSE
                return HttpResponseRedirect("/login?r=/test/start/" + test_id)

            if not user['role'] or not user['organization']:
                context['pe'] = True
                submission = user.submission(test=test_id)
                if submission.valid():
                    found_sub = submission
                elif user.register_for_test(test=test):
                    TestModuleDB.update(test)
                    UserDB.update(user)

        test['id'] = test['_id']
        count = 1
        problem_sets = test.problem_sets()
        for pset in problem_sets:
            pset['no'] = count
            pset['id'] = pset['_id']
            count = count + 1
            if found_sub:
                for q in pset['questions']:
                    q['submitted_data'] = submission.response(
                        pset['_id'], q['no'])

        test['problem_sets'] = problem_sets
        test['remaining_time'] = test.pretty_remaining_time(True)
        context['test'] = test
        return render(request, "test/start.html", context)