def sync_google(self, request): g = Google() users = g.get_all_users() if users is not False: for i in users: get_user_model().objects.create(email=i['primaryEmail'], defaults={ 'first_name': i['name']['givenName'], 'last_name': i['name']['familyName'] }) return Response()
def revoke_access(self, request, pk=None): new_hire = self.get_object() ScheduledAccess.objects.filter( new_hire=new_hire, integration=request.data['integration']).delete() if request.data['integration'] == 1: s = Slack() try: s.delete_user(new_hire.email) except PaidOnlyError: return Response({'error': 'paid'}, status=status.HTTP_400_BAD_REQUEST) except: return Response({'error': 'error'}, status=status.HTTP_400_BAD_REQUEST) if request.data['integration'] == 2: g = Google() g.delete_user(new_hire.email) return Response(status=status.HTTP_204_NO_CONTENT)
def create_accounts(): for i in ScheduledAccess.objects.filter(status=0).order_by('-integration'): new_hire_time = i.new_hire.get_local_time() if new_hire_time.date() >= i.new_hire.start_day and new_hire_time.hour >= 7 and new_hire_time.weekday() < 5: if i.integration == 1: codes = AccessToken.objects.filter(integration=1, active=True) if codes.exists(): try: s = Slack() s.add_user(i.email) except Exception: # need to be replaced with a decent error thing pass i.status = 2 i.save() g = Google() if i.integration == 2 and g.exists(): password = get_user_model().objects.make_random_password() userinfo = { "primaryEmail": i.new_hire.email, "name": { "givenName": i.new_hire.first_name, "familyName": i.new_hire.last_name }, "password": password, "changePasswordAtNextLogin": True } try: g.add_user(userinfo) send_access_email(i.new_hire, password, i.email) i.status = 2 i.save() except EmailAddressNotValidError: i.status = 2 i.save() except UnauthorizedError: a = AccessToken.objects.filter(active=True, integration=2).first() a.active = False a.save() google_error_email(get_user_model().objects.filter(role=1).order_by('date_joined').first()) else: i.status = 2 i.save() return
def access(self, request, pk=None): new_hire = self.get_object() if request.method == 'PUT': ScheduledAccess.objects.create( new_hire=new_hire, integration=request.data['integration'], email=request.data['email']) return Response({'status': 'pending'}) s = SlackBot() if request.data['integration'] == 1 else Google() if s.find_by_email(new_hire.email): return Response({'status': 'exists'}) items = ScheduledAccess.objects.filter( new_hire=new_hire, integration=request.data['integration']).exclude(status=1) if items.exists(): return Response({'status': 'pending'}) return Response({'status': 'not_found'})