def get(self, request):
        context = {}
        if ADMIN_ID in request.session:
            admin_id = request.session[ADMIN_ID]
            client = connection.create()
            my_database = client[ADMIN]
            for doc in my_database:
                pass

            if admin_id in my_database:
                admin = my_database[admin_id]
                context['admin_name'] = admin['name']
                orgs = OrganizationDB.all()
                arr = []
                for org in orgs:
                    if not org['verified']:
                        org['id'] = org['_id']
                        arr.append(org)

                context['orgs'] = arr

                return render(request, "admin/profile.html", context)
            else:
                print("no " + admin_id + " in my_database")

            del request.session[ADMIN_ID]
            request.session[AUTH_RESULT] = LoginError.SOMETHING_ELSE
            return HttpResponseRedirect("/admin/login")
        else:
            return HttpResponseRedirect("/")
示例#2
0
    def post(self, request):
        data = request.POST
        odata = {}
        odata['name'] = data.get('name')
        odata['email'] = data.get('email')
        odata['password'] = data.get('pwd')
        odata['website'] = data.get('website')
        odata['location'] = data.get('location')
        odata['description'] = data.get('description')
        odata['category'] = data.get('category')

        result, org = OrganizationDB.create(odata)
        print(result)
        print(org)

        if result:
            request.session[AUTH_RESULT] = RegisterResult.NONE.value
            return HttpResponseRedirect("/login?r=/profile")

        elif org.error == Organization.Error.EMAIL_ALREADY_EXISTS:
            request.session[
                AUTH_RESULT] = RegisterResult.EMAIL_ALREADY_EXISTS.value
            return HttpResponseRedirect("/register/organization")

        request.session[AUTH_RESULT] = RegisterResult.SOMETHING_ELSE.value
        return HttpResponseRedirect("/register/organization")
    def get(self, request, org=None):
        if org:
            if ADMIN_ID in request.session:
                admin_id = request.session[ADMIN_ID]
                client = connection.create()
                my_database = client[ADMIN]
                for doc in my_database:
                    pass

                if admin_id in my_database:
                    org = OrganizationDB.organization(uno=org)
                    if not org.valid():
                        return HttpResponseRedirect("/admin/profile")
                    org['verified'] = True
                    OrganizationDB.update(org)

        return HttpResponseRedirect("/admin/profile")
示例#4
0
    def organization(self):
        from db.organization_database import OrganizationDB
        if 'organization' in self:
            org = OrganizationDB.organization(uno=self['organization'])
        else:
            org = Organization()

        return org
    def organization(self):
        if self._org:
            return self._org

        if 'organization' in self:
            org = OrganizationDB.organization(uno=self['organization'])
            self._org = org
        else:
            org = Organization()

        return org
def users_data(request):
    if LOGIN_ID not in request.session \
    or LOGIN_TYPE not in request.session:
        return JsonResponse("")

    if request.session[LOGIN_TYPE] != LoginType.ORG \
    or not 'name' in request.GET:
        return JsonResponse("")

    name = request.GET['name']
    name = name.lower()

    if name == "":
        return JsonResponse("")

    org_id = request.session[LOGIN_ID]
    org = OrganizationDB.organization(uno=org_id)
    if not org.valid():
        return JsonResponse("")

    users = UserDB.all()

    josn_string = '{"users_data":['

    nou = 0

    for user in users:
        if user['name'].lower().startswith(name):
            nou += 1
            if nou != 1:
                josn_string += ', '
            josn_string += '{"id":"'+user['_id']+'", "name":"'+user['name']+'", "email":"'+user['email']+'"'
            if user['organization']:
                josn_string += ', "available": false'
            else:
                josn_string += ', "available": true'
            josn_string += '}'

    josn_string += '], "no_of_users":'+str(nou)+'}'

    if nou == 0:
        return JsonResponse("")
    else:
        return JsonResponse(josn_string, safe=False)
示例#7
0
    def post(self, request):

        data = request.POST
        redirect = data.get('r', '/profile')
        email = data.get('email')
        pwd = data.get('pwd')

        if not email or not pwd:
            request.session[AUTH_RESULT] = LoginResult.SOMETHING_ELSE
            return HttpResponseRedirect("/login?r=" + redirect)

        if data.get('org'):
            org = OrganizationDB.organization(email=email, password=pwd)

            if not org.valid():
                request.session[
                    AUTH_RESULT] = LoginResult.WRONG_USERNAME_OR_PASSWORD.value
                return HttpResponseRedirect("/login?r=" + redirect)

            if not org.verified():
                request.session[
                    AUTH_RESULT] = LoginResult.ORGANIZATION_NOT_VERIFIED.value
                return HttpResponseRedirect("/login?r=" + redirect)

            request.session[LOGIN_ID] = org['_id']
            request.session[LOGIN_TYPE] = LoginType.ORG
            return HttpResponseRedirect(redirect)

        else:
            user = UserDB.user(email=email, password=pwd)

            if not user.valid():
                request.session[
                    AUTH_RESULT] = LoginResult.WRONG_USERNAME_OR_PASSWORD.value
                return HttpResponseRedirect("/login?r=" + redirect)

            request.session[LOGIN_ID] = user['_id']
            request.session[LOGIN_TYPE] = LoginType.EMP
            return HttpResponseRedirect(redirect)
def profile(request):

    context = {}

    session = request.session

    if not session.get(LOGIN_ID) or not session.get(LOGIN_TYPE):
        return HttpResponseRedirect("/login?r=/profile")

    user_id = session[LOGIN_ID]
    user_type = session[LOGIN_TYPE]

    if user_type == LoginType.ORG:
        org = OrganizationDB.organization(uno=user_id)

        if not org.valid():
            session[AUTH_RESULT] = LoginResult.SOMETHING_ELSE.value
            return HttpResponseRedirect("/login?r=/profile")

        add_result = session.get(ADD_EMP_RESULT)
        remove_result = session.get(REMOVE_EMP_RESULT)
        if add_result:
            context['alert'] = add_result

            del request.session[ADD_EMP_RESULT]

        elif remove_result:
            context['alert'] = remove_result

            del request.session[REMOVE_EMP_RESULT]

        employees = {}

        user = org.employees(OrganizationData.EmpType.HR)
        if user.valid():
            user['id'] = user['_id']
            employees[OrganizationData.EmpType.HR.name] = user

        user = org.employees(OrganizationData.EmpType.SU)
        if user.valid():
            user['id'] = user['_id']
            employees[OrganizationData.EmpType.SU.name] = user

        user = org.employees(OrganizationData.EmpType.PS)
        if user.valid():
            user['id'] = user['_id']
            employees[OrganizationData.EmpType.PS.name] = user

        user = org.employees(OrganizationData.EmpType.GN)
        for u in user:
            if u.valid():
                u['id'] = u['_id']
        employees[OrganizationData.EmpType.GN.name] = user

        org['employees'] = employees

        context['org'] = org

        return render(request, "home/orgprofile.html", context)

    else:
        user = UserDB.user(uno=user_id)

        if not user.valid():
            session[AUTH_RESULT] = LoginResult.SOMETHING_ELSE.value
            return HttpResponseRedirect("/login?r=/profile")

        org = user.organization()
        if org.valid():
            org['id'] = org['_id']
            user['organization'] = org

        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

        return render(request, "home/userprofile.html", context)
def find_problem_sets(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:
        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)

        if user['role'] == GENERAL_MEMBER:
            response['result'] = "error"
            response['message'] = "Permission Not Granted"
            return JsonResponse(json.dumps(response, indent=4, sort_keys=True),
                                safe=False)

        org = user.organization()

    else:
        org_id = session[LOGIN_ID]
        org = OrganizationDB.organization(uno=org_id)

    s_id = data.get('id')
    s_name = data.get('name')
    s_category = data.get('category')
    s_public = data.get('public')

    s_sort_with = data.get('sort_with')
    s_group_with = data.get('group_with')

    if s_public == "true":
        s_public = True
    else:
        s_public = False

    if s_sort_with != "name" and s_sort_with != "category" and s_sort_with:
        response['result'] = "error"
        response['message'] = "Unknown Sort Type"
        return JsonResponse(json.dumps(response, indent=4, sort_keys=True),
                            safe=False)

    if s_group_with != "category" and s_group_with:
        response['result'] = "error"
        response['message'] = "Unknown Group Type"
        return JsonResponse(json.dumps(response, indent=4, sort_keys=True),
                            safe=False)

    try:
        s_from = int(data.get('from', 1))
    except Exception as e:
        response['result'] = "error"
        response['message'] = "value of parameter 'from' is not an integer"
        return JsonResponse(json.dumps(response, indent=4, sort_keys=True),
                            safe=False)

    try:
        s_max = int(data.get('max', 6))
    except Exception as e:
        response['result'] = "error"
        response['message'] = "value of parameter 'max' is not an integer"
        return JsonResponse(json.dumps(response, indent=4, sort_keys=True),
                            safe=False)

    if s_from <= 0:
        response['result'] = "error"
        response['message'] = "value of parameter 'from' is less than 1"
        return JsonResponse(json.dumps(response, indent=4, sort_keys=True),
                            safe=False)

    if s_max <= 0:
        response['result'] = "error"
        response['message'] = "value of parameter 'max' is less than 1"
        return JsonResponse(json.dumps(response, indent=4, sort_keys=True),
                            safe=False)

    # print("id = "+str(s_id))
    # print("from = "+str(s_from))
    # print("max = "+str(s_max))
    # print("name = "+str(s_name))
    # print("category = "+str(s_category))
    # print("sort_with = "+str(s_sort_with))
    # print("group_with = "+str(s_group_with))

    if s_id:
        pset = ProblemSetDB.problem_set(uno=s_id)
        if not pset.valid():
            response['result'] = "error"
            response['message'] = "Problem Set Does not exist"
            return JsonResponse(json.dumps(response, indent=4, sort_keys=True),
                                safe=False)

        response['result'] = "success"
        response['length'] = 1
        response['organization'] = org['_id']
        response['problem_sets'] = [pset]
        return JsonResponse(json.dumps(response, indent=4, sort_keys=True),
                            safe=False)

    if s_public:
        psets = ProblemSetDB.all()
        pset_list = [
            pset for pset in psets
            if pset['public'] or pset['organization'] == org['_id']
        ]
    else:
        pset_list = org.problem_sets()

    count, pset_list = problem_sets(pset_list, s_name, s_category, s_from,
                                    s_max)

    if s_sort_with:
        pset_list = sort(pset_list, s_sort_with)

    if s_group_with:
        pset_list = group(pset_list, s_group_with)

    if not pset_list or count == 0:
        response['result'] = "success"
        response['length'] = 0
        return JsonResponse(json.dumps(response, indent=4, sort_keys=True),
                            safe=False)

    for ps in pset_list:
        ps['id'] = ps['_id']
        del ps['_id']
        ps['organization'] = ps.organization()
        ps['organization']['id'] = ps['organization'].get('_id')

    response['result'] = "success"
    response['length'] = count
    response['organization'] = org['_id']
    response['problem_sets'] = pset_list

    return JsonResponse(json.dumps(response, indent=4, sort_keys=True),
                        safe=False)
 def organization(self):
     from db.organization_database import OrganizationDB
     return OrganizationDB.organization(uno=self.get('organization'))
def save_response(request):
    response = {}
    found_sub = None
    data = request.GET
    session = request.session
    test_id = data.get('id')
    pset_id = data.get('category')
    ques_no = data.get('question')
    answer = data.get('response')
    if not test_id or not pset_id or not ques_no:
        response['result'] = "error"
        response['message'] = "Wrong Data Provided"
        return JsonResponse(json.dumps(response, indent=4, sort_keys=True),
                            safe=False)

    if not answer or answer == "":
        answer = None

    test = TestModuleDB.test_module(uno=test_id)
    if not test.valid():
        response['result'] = "error"
        response['message'] = "Test Doesn't Exist"
        return JsonResponse(json.dumps(response, indent=4, sort_keys=True),
                            safe=False)

    login_id = request.session.get(LOGIN_ID)
    login_type = request.session.get(LOGIN_TYPE)

    if not login_id or not login_type:
        response['result'] = "error"
        response['message'] = "Login Required"
        return JsonResponse(json.dumps(response, indent=4, sort_keys=True),
                            safe=False)

    if login_type == LoginType.ORG:
        org = OrganizationDB.organization(uno=login_id)
        if not org.valid():
            response['result'] = "error"
            response['message'] = "Something Looks Wrong with the Login"
            return JsonResponse(json.dumps(response, indent=4, sort_keys=True),
                                safe=False)

    elif login_type == LoginType.EMP:
        user = UserDB.user(uno=login_id)
        if not user.valid():
            response['result'] = "error"
            response['message'] = "Something Looks Wrong with the Login"
            return JsonResponse(json.dumps(response, indent=4, sort_keys=True),
                                safe=False)

        if not user['role'] or not user['organization']:
            if not test.is_live():
                response['result'] = "error"
                response['message'] = "Test is Not Live"
                return JsonResponse(json.dumps(response,
                                               indent=4,
                                               sort_keys=True),
                                    safe=False)

            submission = user.submission(test=test_id)
            if submission.valid():
                found_sub = submission

    psets = test.problem_sets()
    if pset_id not in [p['_id'] for p in psets]:
        response['result'] = "error"
        response['message'] = "Category Doesn't Exist"
        return JsonResponse(json.dumps(response, indent=4, sort_keys=True),
                            safe=False)

    pset = {}
    for p in psets:
        if p['_id'] == pset_id:
            pset = p

    questions = pset['questions']
    if len(questions) < int(ques_no) or int(ques_no) < 0:
        response['result'] = "error"
        response['message'] = "Question Doesn't Exist"
        return JsonResponse(json.dumps(response, indent=4, sort_keys=True),
                            safe=False)

    if found_sub:
        found_sub.save_response(pset_id, ques_no, answer)
        SubmissionDB.update(found_sub)

    response['result'] = "success"
    return JsonResponse(json.dumps(response, indent=4, sort_keys=True),
                        safe=False)
def find_tests(request):
    response = {}
    data = request.GET
    s_id = data.get('id')
    s_name = data.get('name')
    s_category = data.get('category')
    s_test_type = data.get('test_type')
    s_seperate_types = data.get('seperate_types')

    if s_seperate_types == "true":
        s_seperate_types = True
    else:
        s_seperate_types = False

    if not s_test_type:
        s_test_type = "all"
    if s_test_type != "all" and s_test_type != "live" \
    and s_test_type != "upcomming" and s_test_type != "completed":
        response['result'] = "error"
        response['message'] = "Unknown Test Type"
        return JsonResponse(json.dumps(response, indent=4, sort_keys=True),
                            safe=False)

    s_sort_with = data.get('sort_with')
    if s_sort_with != "date" and s_sort_with != "name" \
    and s_sort_with != "category" and s_sort_with:
        response['result'] = "error"
        response['message'] = "Unknown Sort Type"
        return JsonResponse(json.dumps(response, indent=4, sort_keys=True),
                            safe=False)

    s_group_with = data.get('group_with')
    if s_group_with != "organization" and s_group_with != "category"\
    and s_group_with:
        response['result'] = "error"
        response['message'] = "Unknown Group Type"
        return JsonResponse(json.dumps(response, indent=4, sort_keys=True),
                            safe=False)

    org = data.get('organization')
    if org:
        org = OrganizationDB.organization(uno=org)
        if not org.valid():
            response['result'] = "error"
            response['message'] = "not a valid organization"
            return JsonResponse(json.dumps(response, indent=4, sort_keys=True),
                                safe=False)

    try:
        s_from = int(data.get('from', 1))
    except Exception as e:
        response['result'] = "error"
        response['message'] = "value of parameter 'from' is not an integer"
        return JsonResponse(json.dumps(response, indent=4, sort_keys=True),
                            safe=False)

    try:
        s_max = int(data.get('max', 6))
    except Exception as e:
        response['result'] = "error"
        response['message'] = "value of parameter 'max' is not an integer"
        return JsonResponse(json.dumps(response, indent=4, sort_keys=True),
                            safe=False)

    if s_from <= 0:
        response['result'] = "error"
        response['message'] = "value of parameter 'from' is less than 1"
        return JsonResponse(json.dumps(response, indent=4, sort_keys=True),
                            safe=False)

    if s_max <= 0:
        response['result'] = "error"
        response['message'] = "value of parameter 'max' is less than 1"
        return JsonResponse(json.dumps(response, indent=4, sort_keys=True),
                            safe=False)

    # print("id = "+str(s_id))
    # print("from = "+str(s_from))
    # print("max = "+str(s_max))
    # print("org = "+str(org))
    # print("name = "+str(s_name))
    # print("category = "+str(s_category))
    # print("sort_with = "+str(s_sort_with))
    # print("group_with = "+str(s_group_with))
    # print("seperate_types = "+str(s_seperate_types))

    if s_id:
        test = TestModuleDB.test_module(uno=s_id)
        if not test.valid():
            response['result'] = "error"
            response['message'] = "Test does not exist"
            return JsonResponse(json.dumps(response, indent=4, sort_keys=True),
                                safe=False)

        test['pretty_date'] = test.pretty_date()
        test['pretty_duration'] = test.pretty_duration()
        response['result'] = "success"
        response['length'] = 1
        response['tests'] = [test]
        return JsonResponse(json.dumps(response, indent=4, sort_keys=True),
                            safe=False)

    test_list = []
    if org:
        test_list = org.tests()

    else:
        test_list = TestModuleDB.all()

    if s_sort_with:
        test_list = sort(test_list, s_sort_with)

    count, test_list = tests(test_list, s_name, s_category, s_from, s_max,
                             s_test_type)

    if s_group_with:
        test_list = group(test_list, s_group_with)

    if not test_list or count == 0:
        response['result'] = "success"
        response['length'] = 0
        return JsonResponse(json.dumps(response, indent=4, sort_keys=True),
                            safe=False)

    for t in test_list:
        t['id'] = t['_id']
        t['pretty_date'] = t.pretty_date()
        t['pretty_duration'] = t.pretty_duration()
        if t.is_live():
            t['status'] = "live"
            t['pretty_remaining_time'] = t.pretty_remaining_time(True)
        elif t.is_completed():
            t['status'] = "completed"
        else:
            t['status'] = "coming"
            t['pretty_remaining_time'] = t.pretty_remaining_time()
        del t['_id']

    response['result'] = "success"
    response['length'] = count
    response['tests'] = test_list

    return JsonResponse(json.dumps(response, indent=4, sort_keys=True),
                        safe=False)
示例#13
0
    def post(self, request):

        # validating client

        if LOGIN_ID not in request.session or LOGIN_TYPE not in request.session:
            return HttpResponseRedirect("/profile")

        if request.session[LOGIN_TYPE] == LoginType.ORG:
            return HttpResponseRedirect("/profile")

        user_id = request.session[LOGIN_ID]
        user_type = request.session[LOGIN_TYPE]

        user = UserDB.user(uno=user_id)
        if not user.valid():
            return HttpResponseRedirect("/profile")

        if user['role'] != PROBLEM_SETTER:
            return HttpResponseRedirect("/profile")

        # validating request

        data = request.POST

        noq = data.get('noq')
        if not noq:
            request.session[
                ADD_PROBLEM_SET_RESULT] = CreatePSetResult.SOMETHING_ELSE.value
            return HttpResponseRedirect("/create/problem-set")

        if int(noq) <= 0:
            request.session[
                ADD_PROBLEM_SET_RESULT] = CreatePSetResult.NO_QUESTIONS.value
            return HttpResponseRedirect("/create/problem-set")

        ps_name = data.get('ps_name')
        if not ps_name:
            request.session[
                ADD_PROBLEM_SET_RESULT] = CreatePSetResult.NO_NAME.value
            return HttpResponseRedirect("/create/problem-set")

        ps_category = data.get('ps_category')

        pset = {
            "name": ps_name,
            "category": ps_category,
            "public": False,
            "organization": user['organization'],
            "questions": []
        }

        problem_sets = ProblemSetDB.all()

        for pset in problem_sets:
            if ps_name == pset['name'] and user['organization'] == pset[
                    'organization']:
                request.session[
                    ADD_PROBLEM_SET_RESULT] = CreatePSetResult.SAME_NAME_EXISTS.value
                return HttpResponseRedirect("/create/problem-set")

        allques = []

        for q in range(1, int(noq) + 1):
            i = str(q)
            qtype = data['q' + i + 't']

            if qtype == "OBJ":
                qop = []
                qnoo = data.get('q' + i + 'noo')
                for o in range(1, int(qnoo) + 1):
                    j = str(o)
                    qop.append(data['q' + i + 'o' + j])

                ques = {
                    "no": i,
                    "type": "OBJ",
                    "question": data.get('q' + i + 'q'),
                    "no_of_options": data.get('q' + i + 'noo'),
                    "options": qop,
                    "correct": data.get('q' + i + 'co'),
                    "marks": data.get('q' + i + 'marks'),
                    "tag": data.get('q' + i + 'tag')
                }

                allques.append(ques)
                continue

            if qtype == "SUB":
                ques = {
                    "no": i,
                    "type": "SUB",
                    "question": data.get('q' + i + 'q'),
                    "limit": data.get('limit'),
                    "tag": data.get('q' + i + 'tag')
                }

                allques.append(ques)
                continue

            if qtype == "COD":
                ques = {
                    "no": i,
                    "type": "COD",
                    "question": data.get('q' + i + 'q'),
                    "input": data.get('q' + i + 'si'),
                    "output": data.get('q' + i + 'so'),
                    "time_limit": data.get('q' + i + 'tl'),
                    "marks": data.get('q' + i + 'marks'),
                    "tag": data.get('q' + i + 'tag')
                }

                allques.append(ques)
                continue

        # saving problem set

        pset['questions'] = allques

        org = OrganizationDB.organization(uno=user['organization'])
        if not org.valid():
            request.session[
                ADD_PROBLEM_SET_RESULT] = CreatePSetResult.SOMETHING_ELSE.value
            return HttpResponseRedirect("/create/problem-set")

        result, pset = ProblemSetDB.create(pset)

        if not result:
            print("error in creating document in ProblemSets")
            request.session[
                ADD_PROBLEM_SET_RESULT] = CreatePSetResult.SOMETHING_ELSE.value
            return HttpResponseRedirect("/create/problem-set")

        org['problem_sets'].append(pset['_id'])
        OrganizationDB.update(org)

        request.session[ADD_PROBLEM_SET_RESULT] = CreatePSetResult.ADDED.value
        return HttpResponseRedirect("/create/problem-set")
示例#14
0
    def post(self, request):

        # validating client
        session = request.session
        if not session.get(LOGIN_ID) or not session.get(LOGIN_TYPE):
            return HttpResponseRedirect("/profile")

        if session[LOGIN_TYPE] == LoginType.ORG:
            return HttpResponseRedirect("/profile")

        user_id = session[LOGIN_ID]
        user_type = session[LOGIN_TYPE]

        user = UserDB.user(uno=user_id)
        if not user.valid():
            return HttpResponseRedirect("/profile")

        if user['role'] != TEST_SUPERVISOR:
            return HttpResponseRedirect("/profile")

        data = request.POST
        test_name = data.get('test_name')
        if not test_name:
            request.session[
                ADD_TEST_MODULE_RESULT] = CreateTMResult.NO_NAME.value
            return HttpResponseRedirect("/create/test-module")

        org = OrganizationDB.organization(uno=user['organization'])
        if not org.valid():
            request.session[
                ADD_TEST_MODULE_RESULT] = CreateTMResult.SOMETHING_ELSE.value
            return HttpResponseRedirect("/create/test-module")

        test_modules = TestModuleDB.all()

        for doc in test_modules:
            if test_name == doc['name'] and user['organization'] == doc[
                    'organization']:
                request.session[
                    ADD_TEST_MODULE_RESULT] = CreateTMResult.SAME_NAME_EXISTS.value
                return HttpResponseRedirect("/create/test-module")

        if 'no-of-psets' not in data:
            request.session[
                ADD_TEST_MODULE_RESULT] = CreateTMResult.SOMETHING_ELSE.value
            return HttpResponseRedirect("/create/test-module")

        nopset = int(data['no-of-psets'])

        if int(nopset) <= 0:
            request.session[
                ADD_TEST_MODULE_RESULT] = CreateTMResult.NO_PSET.value
            return HttpResponseRedirect("/create/test-module")

        test_category = data.get('test_category')
        test_description = data.get('test_description')

        test = {
            "name": test_name,
            "category": test_category,
            "description": test_description,
            "organization": org['_id'],
            "problem_sets": [],
            "submissions": None,
            "schedule": None,
            "job_offer": None,
        }

        try:
            time_hour = int(str(data.get('time-hour')))
            time_min = int(str(data.get('time-min')))
        except Exception as e:
            print("hello")
            request.session[
                ADD_TEST_MODULE_RESULT] = CreateTMResult.SOMETHING_ELSE.value
            return HttpResponseRedirect("/create/test-module")

        time_am_pm = data.get('time-am-pm')

        if time_am_pm != "PM" and time_am_pm != "AM":
            request.session[
                ADD_TEST_MODULE_RESULT] = CreateTMResult.SOMETHING_ELSE.value
            return HttpResponseRedirect("/create/test-module")

        time_hour -= 1

        if time_am_pm == "PM":
            time_hour += 12

        time = str(time_hour) + ":" + str(time_min)

        schedule = {
            "date": data.get('date'),
            "time": time,
            "duration": data.get('duration')
        }

        job_offer = {
            "position": data.get('position'),
            "type": data.get('type'),
            "duration": data.get('job-duration'),
            "salary": data.get('salary'),
            "currency": data.get('currency'),
            "description": data.get('job-description')
        }

        test['schedule'] = schedule
        test['job_offer'] = job_offer

        all_psets = {doc['_id']: doc for doc in ProblemSetDB.all()}

        for i in range(0, nopset):
            pset_id = data.get('ps' + str(i))
            if pset_id:
                test['problem_sets'].append(pset_id)

        result, test = TestModuleDB.create(test)

        if not result:
            print("error in creating document in Test Modules")
            request.session[
                ADD_TEST_MODULE_RESULT] = CreateTMResult.SOMETHING_ELSE.value
            return HttpResponseRedirect("/create/test-module")

        result, subset = SubmissionSetDB.create({"test": test['_id']})
        if result:
            test['submissions'] = subset['_id']
            TestModuleDB.update(test)

        org['test_modules'].append(test['_id'])
        OrganizationDB.update(org)

        request.session[ADD_TEST_MODULE_RESULT] = CreateTMResult.ADDED.value
        return HttpResponseRedirect("/create/test-module")
示例#15
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")
示例#16
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")