def response(request, method, auth_type, callback, status_code=status.HTTP_200_OK, *args, **kwargs): ''' This function is used for basic method to return json response. To use this function you need to create callback function to use this function, include the callback function in the parameter. The callback function must return tuplpe consists of response in dict/list format and json safe parameter (for more information please read documentation for JsonResponse Safe Parameter). :param request: HttpRequest :param method: String REST method :param auth_type: int login type :param callback: Function callback :param status_code: int for JsonResponse status :param args: :param kwargs: :return: JsonResponse response ''' if request.method == method: result = None if auth_type == 0: if 'user_login' in request.session: npm = request.session['user_login']['npm'] user = auth_utils.get_or_create_user(npm=npm) result = callback(user) # # using session to identified user # elif auth_utils.check_user_session_existence(request): # sess = auth_utils.get_user_session(request) # sess_data = sess.get_decoded() # print(sess_data) elif auth_type == 1 and 'public_token' in request.GET: token = request.GET['public_token'] access = service_utils.check_token_existance(token=token) if access: acc = service_utils.get_accessor_or_create(token=token) result = callback(acc) # return the json response if result: return JsonResponse(data=result, status=status_code, content_type=RESPONSE_ATTRS['content_type']) return HttpResponseForbidden( reason=RESPONSE_ATTRS['error_reason']['no_access'], content_type=RESPONSE_ATTRS['content_type']) return HttpResponseBadRequest( reason=RESPONSE_ATTRS['error_reason']['no_method'], content_type=RESPONSE_ATTRS['content_type'])
def index(request, data): npm = request.session['user_login']['npm'] user = auth_utils.get_or_create_user(npm=npm) data['number_of_status'] = utils.get_number_of_status(user=user) data['status_form'] = forms.StatusPostForm if data['number_of_status'] > 0: # start from page one data['latest_status'] = utils.get_latest_status(user=user) data['query_of_status'] = utils.get_status_queryset(user=user)[:11]
def callback(user): if 'npm' in request.POST: npm = request.POST['npm'] user_new_friend = auth_utils.get_or_create_user(npm=npm) friendship = friend_utils.insert_new_friend_to_database( user, user_new_friend) result = { 'result': friend_utils.serialize_friendship(friendship=friendship) } return result
def test_auth_utils(self): ser_user = utils.serialize_user(user=self.user) self.assertTrue('full_name' in ser_user) self.assertTrue('username' in ser_user) self.assertTrue('npm' in ser_user) self.assertTrue('expertise' in ser_user) list_permission = utils.get_permission_for_sso_user() self.assertEqual(len(list_permission), 12) group_sso = utils.get_or_create_sso_group() self.assertEqual(group_sso.name, utils.SSO_GROUP_NAME) from django.contrib.auth.models import Group self.assertEqual( Group.objects.filter(name=utils.SSO_GROUP_NAME).count(), 1) self.assertTrue(utils.check_user_existence(npm='1606918055')) d = dict() self.assertFalse(utils.check_user_existence(**d)) same_user = utils.get_or_create_user(npm='1606918055') self.assertEqual(same_user.get_npm(), self.user.get_npm()) create_other = utils.get_or_create_user(npm='160606', username='******', angkatan=2014, role='dewa') self.assertEqual(create_other.npm, '160606') self.assertEqual(create_other.username, 'user') self.assertEqual(create_other.angkatan, 2014) self.assertEqual(create_other.role, 'dewa') self.assertEqual(utils.get_except_user_queryset(npm='1606').count(), 2) self.assertEqual( utils.get_except_user_queryset(angkatan=2014).count(), 1) self.assertEqual( utils.get_except_user_queryset(npm='1606918055').count(), utils.get_user_queryset().count() - 1)
def auth_login(request): form = LoginForm(request.POST or None) if request.method == 'POST' and form.is_valid(): __USERNAME__ = request.POST['username'] __PASSWORD__ = request.POST['password'] try: access_token = CSUIHelper.get_access_token(__USERNAME__, __PASSWORD__) except AuthUtils.SSOException as e: print(e) access_token = None if access_token: verified_user = CSUIHelper.verify_user(access_token=access_token) if __USERNAME__ == verified_user['username']: npm = verified_user['identity_number'] role = verified_user['role'] user_data = CSUIHelper.get_user_data(access_token=access_token, id=npm) user = AuthUtils.get_or_create_user(npm=npm, username=__USERNAME__, role=role, angkatan=user_data['program'][0]['angkatan'] ) # set user session request.session['user_login'] = AuthUtils.serialize_user(user=user) request.session['access_token'] = access_token else: return HttpResponseForbidden(reason='Error: Unexpected behavior') else: messages.error(request, 'Login: Unidentified username or password') else: return HttpResponseBadRequest(reason='Error: Unexpected method') # TODO FINISH WITH REVERSE URL return HttpResponseRedirect(reverse('web:dashboard'))
def get_query_user_history(npm): user = auth_utils.get_or_create_user(npm=npm) hist = __get_riwayat__(user) result = [] for mk in hist: res = dict() res['kode_mk'] = mk.get('kd_mk') res['tahun'] = mk.get('tahun') data_mk = mk.get('kelas') if data_mk: data_mk = data_mk.get('nm_mk_cl') res['nama_mk'] = data_mk.get('nm_mk') res['jml_sks'] = data_mk.get('jml_sks') res['nilai'] = mk.get( 'nilai') if user.is_showing_score else 'Dirahasiakan' result.append(res) return result
def index(request, data): npm = request.session['user_login']['npm'] user = auth_utils.get_or_create_user(npm=npm) data['number_of_friend'] = friend_utils.get_number_of_friend(user=user)
def insert_new_friend_to_database(user, user2npm): user2 = auth_utils.get_or_create_user(npm=user2npm) friendship = Friendship(user1=user, user2=user2) friendship.save() return friendship