def seed_users(self): admin = User() admin.email = self.fake.email() admin.name = self.fake.name() admin.role_id = 1 admin.picture = self.fake.image_url() admin.save() for i in range(4): watcher = User() watcher.email = self.fake.email() watcher.name = self.fake.name() watcher.role_id = 2 watcher.picture = self.fake.image_url() watcher.save()
def create(self, validated_data): instance = User() instance.username = validated_data.get('username') instance.email = validated_data.get('email') if (validated_data.get('is_admin')): instance.is_admin = validated_data.get('is_admin') else: instance.is_admin = False if (validated_data.get('is_superuser')): instance.is_superuser = validated_data.get('is_superuser') else: instance.is_superuser = False if (validated_data.get('is_active')): instance.is_active = validated_data.get('is_active') else: instance.is_active = True if (validated_data.get('is_staff')): instance.is_staff = validated_data.get('is_staff') else: instance.is_staff = False instance.person_id = validated_data.get('person_id') instance.set_password(validated_data.get('password')) instance.save() return instance
def test_call_delete(self): ''' Test deleting of a registration of a device from a channel ''' u = User() u.username = "******" u.first_name = "sample_del" u.email = "*****@*****.**" u.set_password("123") u.save() ch1 = Channel() ch1.owner = u ch1.name = "DelSub" ch1.image = 'http://www.google.com' ch1.description = "A channel description" ch1.kind = PUBLIC ch1.hidden = False ch1.subscriptions = 0 ch1.save() sub1 = Subscriber() sub1.sub_type = 'type2' sub1.token = 'token2' sub1.device_id = 'devid6' sub1.save() resp = ask_subscribe_channel(ch1, sub1.device_id) self.assertEqual(resp, SubscribeResponse.SUBSCRIBED) test_user = User.objects.create_superuser('test_user', '*****@*****.**', 'password') self.client.login(username='******', password='******') response = self.client.delete( reverse('browser-get-registration', kwargs={ 'device_id': sub1.device_id, 'channel_name': ch1.name })) self.assertEqual(response.status_code, 200) channels = SubscriberManager().get_device_subscriptions(sub1.device_id) sub_channel = next((x for x in channels if x == ch1.name.lower()), None) self.assertIsNone(sub_channel)
def post(self, request, *args, **kwargs): payload = request.data.copy() payload["user_type"] = User.CUSTOMER serializer = self.serializer(data=payload) if serializer.is_valid(): user = User() user.email = payload.get("email").strip() user.password = make_password(payload.get("password").strip()) user.user_type = payload.get("user_type") user.save() return Response({}, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def test_call_register_channel(self): ''' Test registration to a channel ''' u = User() u.username = "******" u.first_name = "sample_post" u.email = "*****@*****.**" u.set_password("123") u.save() ch1 = Channel() ch1.owner = u ch1.name = "PostSub" ch1.image = 'http://www.google.com' ch1.description = "A channel description" ch1.kind = PUBLIC ch1.hidden = False ch1.subscriptions = 0 ch1.save() sub1 = Subscriber() sub1.sub_type = 'type2' sub1.token = 'token2' sub1.device_id = 'devid5' sub1.save() resp = ask_subscribe_channel(ch1, sub1.device_id) self.assertEqual(resp, SubscribeResponse.SUBSCRIBED) test_user = User.objects.create_superuser('test_user', '*****@*****.**', 'password') self.client.login(username='******', password='******') data = { 'channel': ch1.name, 'token': sub1.token, 'browser': 'chrome', 'device_id': sub1.device_id } response = self.client.post(reverse('browser-registration'), json.dumps(data), sub1.token) self.assertTrue(response.status_code, 200)
def facebook_connect(user_data): try: # Logging in with Facebook user = User.objects.get(facebook_id=user_data['id']) user.tz_offset = user_data['timezone'] user.save() except User.DoesNotExist: # Creating new account with Facebook user = User() user.email = user_data['email'] user.facebook_id = user_data['id'] user.tz_offset = user_data['timezone'] user.set_password(random_string()) user.save() return user
def test_call_register_channel(self): ''' Test registration to a channel ''' u = User() u.username = "******" u.first_name = "sample_post" u.email = "*****@*****.**" u.set_password("123") u.save() ch1 = Channel() ch1.owner = u ch1.name = "PostSub" ch1.image = 'http://www.google.com' ch1.description = "A channel description" ch1.kind = PUBLIC ch1.hidden = False ch1.subscriptions = 0 ch1.save() sub1 = Subscriber() sub1.sub_type = 'type2' sub1.token = 'token2' sub1.device_id = 'devid5' sub1.save() resp = ask_subscribe_channel(ch1, sub1.device_id) self.assertEqual(resp, SubscribeResponse.SUBSCRIBED) test_user = User.objects.create_superuser('test_user', '*****@*****.**', 'password') self.client.login(username='******', password='******') data = { 'channel' : ch1.name, 'token' : sub1.token, 'browser' : 'chrome', 'device_id' : sub1.device_id } response = self.client.post(reverse('browser-registration'), json.dumps(data), sub1.token) self.assertTrue(response.status_code, 200)
def user_create(**new_user_data): new_user = User() new_user.name = new_user_data['name'] new_user.email = new_user_data['email'] if 'password' in new_user_data: new_user.password = encrypt_password(new_user_data['password']) elif 'encrypted_password' in new_user_data: new_user.password = new_user_data['encrypted_password'] else: new_user.password = encrypt_password('Temporary password') new_user.save() return new_user
def test_call_delete(self): ''' Test deleting of a registration of a device from a channel ''' u = User() u.username = "******" u.first_name = "sample_del" u.email = "*****@*****.**" u.set_password("123") u.save() ch1 = Channel() ch1.owner = u ch1.name = "DelSub" ch1.image = 'http://www.google.com' ch1.description = "A channel description" ch1.kind = PUBLIC ch1.hidden = False ch1.subscriptions = 0 ch1.save() sub1 = Subscriber() sub1.sub_type = 'type2' sub1.token = 'token2' sub1.device_id = 'devid6' sub1.save() resp = ask_subscribe_channel(ch1, sub1.device_id) self.assertEqual(resp, SubscribeResponse.SUBSCRIBED) test_user = User.objects.create_superuser('test_user', '*****@*****.**', 'password') self.client.login(username='******', password='******') response = self.client.delete(reverse('browser-get-registration', kwargs={'device_id': sub1.device_id, 'channel_name' : ch1.name})) self.assertEqual(response.status_code, 200) channels = SubscriberManager().get_device_subscriptions(sub1.device_id) sub_channel = next((x for x in channels if x == ch1.name.lower()), None) self.assertIsNone(sub_channel)
def test_call_get(self): ''' Test check if a device is registered to a channel ''' u = User() u.username = "******" u.first_name = "Sample_un" u.email = "*****@*****.**" u.set_password("123") u.save() ch1 = Channel() ch1.owner = u ch1.name = "GetSub" ch1.image = 'http://www.google.com' ch1.description = "A channel description" ch1.kind = PUBLIC ch1.hidden = False ch1.subscriptions = 0 ch1.save() sub1 = Subscriber() sub1.sub_type = 'type2' sub1.token = 'token2' sub1.device_id = 'devid4' sub1.save() resp = ask_subscribe_channel(ch1, 'devid4') self.assertEqual(resp, SubscribeResponse.SUBSCRIBED) test_user = User.objects.create_superuser('test_user', '*****@*****.**', 'password') self.client.login(username='******', password='******') response = self.client.get( reverse('browser-get-registration', kwargs={ 'device_id': 'devid4', 'channel_name': 'GetSub' })) self.assertTrue(response.status_code, 200)
def test_call_get(self): ''' Test check if a device is registered to a channel ''' u = User() u.username = "******" u.first_name = "Sample_un" u.email = "*****@*****.**" u.set_password("123") u.save() ch1 = Channel() ch1.owner = u ch1.name = "GetSub" ch1.image = 'http://www.google.com' ch1.description = "A channel description" ch1.kind = PUBLIC ch1.hidden = False ch1.subscriptions = 0 ch1.save() sub1 = Subscriber() sub1.sub_type = 'type2' sub1.token = 'token2' sub1.device_id = 'devid4' sub1.save() resp = ask_subscribe_channel(ch1, 'devid4') self.assertEqual(resp, SubscribeResponse.SUBSCRIBED) test_user = User.objects.create_superuser('test_user', '*****@*****.**', 'password') self.client.login(username='******', password='******') response = self.client.get(reverse('browser-get-registration', kwargs={'device_id': 'devid4', 'channel_name' : 'GetSub'}) ) self.assertTrue(response.status_code, 200)
def _upsert_user(self, name, email, picture, token, auth_provider_id): generate_def_list = False user = User.objects.filter(email=email).first() if user is None: generate_def_list = True user = User() user.picture = picture user.name = name user.email = email user.role_id = 2 user.save() credential = Credential.objects.filter( auth_provider_id=auth_provider_id, user_id=user.id).first() if credential is None: credential = Credential() credential.token = token credential.auth_provider_id = auth_provider_id credential.user_id = user.id credential.save() # Generate default list? if generate_def_list: def_list = Watchlist() def_list.name = "Mi lista" def_list.is_default_list = True def_list.save() has_list = UserHasWatchlist() has_list.watchlist = def_list has_list.user = user has_list.permission_id = 1 has_list.save() return user
def registration_approved_created_handler(sender, **kwargs): registration = kwargs['registration'] user_doer = User() user_doer.password = make_password(registration.password) user_doer.username = registration.email user_doer.email = registration.email user_doer.first_name = registration.name user_doer.last_Name = '' user_doer.is_doer = True user_doer.save() doer = Doer() doer.user = User.objects.filter(username=registration.email).first() doer.email = registration.email doer.nric_no = registration.nric_no doer.name = registration.name doer.birth_date = registration.birth_date doer.state = registration.state doer.city = registration.city doer.gender = registration.gender doer.race = registration.race doer.level = Level.objects.filter(code='GANGSA').first() doer.save()
def post(self, request): from core.models import User, Employee from employee_dashboard.models.Employee_Extra_Info import Employee_Extra_Info from dateutil.parser import parse from django.core.exceptions import ValidationError from employee_dashboard.models.Section import Section from django.contrib.sites.shortcuts import get_current_site from django.template.loader import render_to_string from django.utils.http import urlsafe_base64_encode from django.utils.encoding import force_bytes from core.tokens import account_activation_token data = request.data user = User() user.email = data['email'] user.first_name = data['first_name'] user.last_name = data['last_name'] user.date_joined = data['hiring_date'] user.username = data['username'] user.position = data['position'] user.national_id = data['national_id'] user.phone_number = data['phone_number'] user.office_phone = data['office_number'] user.date_of_birth = data['date_of_birth'] user.is_employee = True employee = Employee() employee.employer = request.user.employer employee.user = user employee_extra = Employee_Extra_Info() employee_extra.employee = employee employee_extra.nationality = data['nationality'] employee_extra.job_title = data['job_title'] try: employee_extra.direct_manager = Employee.objects.get( user__email=data.get('direct_manager')) if data.get( 'direct_manager') else None employee_extra.supervisor = Employee.objects.get( user__email=data.get('supervisor')) if data.get( 'supervisor') else None except Employee.DoesNotExist: return Response({ 'success': False, 'message': 'Supervisor or Direct Manager Does Not Exist' }) try: employee_extra.section = Section.objects.get( name=data.get('section')) if data.get('section') else None except Section.DoesNotExist: return Response({ 'success': False, 'message': 'The Section {} Does Not Exist'.format(data['section']) }) employee_extra.birth_date = data['date_of_birth'] employee_extra.hiring_date = data['hiring_date'] employee_extra.end_of_contract = data['end_of_contract'] employee_extra.end_of_probation = data['end_of_probation'] employee_extra.marital_status = data['marital_status'] employee_extra.branch = data['branch'] employee_extra.sick_day_balance = 30 employee_extra.base_salary = data['base_salary'] employee_extra.GOSI_salary = data['GOSI_salary'] employee_extra.available_annual_vacation_balance = 30 employee_extra.gender = data['gender'] employee_extra.salutation = data['salutation'] employee_extra.status = 'Active' try: with transaction.atomic(): user.save() employee.save() employee_extra.save() except ValidationError as e: return Response({'success': False, 'message': e.message}) except IntegrityError: return Response({ 'success': False, 'message': 'Email or Username Already Exist' }) current_site = get_current_site(request) subject = 'Activate Employee Account' message = render_to_string( 'core/account_activation_email.html', { 'user': employee, 'domain': current_site.domain, 'uid': urlsafe_base64_encode(force_bytes(employee.pk)), 'token': account_activation_token.make_token(user) }) user.email_user(subject, message, from_email='*****@*****.**') return Response({'success': True})
state.save() selangorCities = ['KLANG', 'HULU LANGAT', 'KUALA LANGAT', 'SEPANG', 'HULU SELANGOR', 'SABAK BERNAM', 'GOMBAK'] for i in range(len(selangorCities)): city = City() city.code = f'S{i}' city.name = selangorCities[i] city.state = State.objects.filter(code='SL').first() city.save() user_staff = User() user_staff.password = '******' user_staff.username = '******' user_staff.email = '*****@*****.**' user_staff.first_name = 'Staff' user_staff.last_Name = '1' user_staff.is_staff = True user_staff.save() staff = Staff() staff.user = User.objects.filter(username='******').first() staff.nric_no = '760607-12-4431' staff.name = 'Rafizi Ramli' staff.save() user_staff = User() user_staff.password = '******' user_staff.username = '******' user_staff.email = '*****@*****.**'