def __str__(self): try: child_name = CbcEngine.get_engine().decrypt(self.child.name) grade = CbcEngine.get_engine().decrypt(self.grade) except (TypeError, ValueError): child_name = self.child.name grade = self.grade return '{0}, {1}'.format(child_name, grade)
def __str__(self): try: first_name = CbcEngine.get_engine().decrypt(self.user.first_name) last_name = CbcEngine.get_engine().decrypt(self.user.last_name) except (TypeError, ValueError): first_name = self.user.first_name last_name = self.user.last_name return '{0} {1}'.format(str(first_name), str(last_name))
def save(self, **kwargs): cbc_engine = CbcEngine.get_engine() self.id_number = cbc_engine.encrypt(self.id_number) self.age = cbc_engine.encrypt(self.age) self.gender = cbc_engine.encrypt(self.gender) self.name = cbc_engine.encrypt(self.name) super(Child, self).save(**kwargs)
def authenticate(self, request): # Get the username and password username = request.data.get('phone_number', None) password = request.data.get('password', None) if not username or not password: raise exceptions.AuthenticationFailed( detail='No credentials provided.') username = CbcEngine.get_engine().encrypt(username) credentials = { get_user_model().USERNAME_FIELD: username, 'password': password } user = authenticate(**credentials) if user is None: raise exceptions.AuthenticationFailed( detail='Invalid username/password.') if not user.is_active: raise exceptions.AuthenticationFailed( detail='User inactive or deleted.') return user, None # authentication successful
def put(self, request): try: child_id = CbcEngine.get_engine().encrypt(request.data['child_id']) child = Child.objects.get(id_number=child_id) parent = child.parent if parent.user.username != request.user.username: raise exceptions.AuthenticationFailed( detail='Not authorized request.') except KeyError: return Response('One of the fields are missing.', status=status.HTTP_400_BAD_REQUEST) except Child.DoesNotExist: return Response('child does not exist.', status=status.HTTP_400_BAD_REQUEST) request.data['child_id'] = child_id serializer = QuizSerializer(data=request.data) if not serializer.is_valid(): return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) try: serializer.save() except IntegrityError: return Response('Quiz is already exists', status=status.HTTP_400_BAD_REQUEST) return Response('Quiz added successfully!')
def post(self, request): try: id_number = CbcEngine.get_engine().encrypt(request.data['id_number']) child = Child.objects.get(id_number=id_number) parent_username = child.parent.user.username if parent_username != request.user.username: raise exceptions.AuthenticationFailed(detail='Not authorized request.') except KeyError: return Response('id_number field is missing.', status=status.HTTP_400_BAD_REQUEST) except Person.DoesNotExist: return Response('Parent does not exist.', status=status.HTTP_400_BAD_REQUEST) except Child.DoesNotExist: return Response('The child not exist.', status=status.HTTP_400_BAD_REQUEST) serializer = ChildrenSerializer(child, read_only=True) serializer.data['parent']['user'].pop('password') return Response(CbcEngine.get_engine().decrypt_child_json(serializer.data))
def post(self, request): try: phone_number = CbcEngine.get_engine().encrypt(request.data['parent_id']) parent = Person.objects.get(phone_number=phone_number) if parent.user.username != request.user.username: raise exceptions.AuthenticationFailed(detail='Not authorized request.') children_of_parent = Child.objects.filter(parent_id=phone_number) except KeyError: return Response('parent_id field is missing.', status=status.HTTP_400_BAD_REQUEST) except Person.DoesNotExist: return Response('Parent does not exist.', status=status.HTTP_400_BAD_REQUEST) except Child.DoesNotExist: return Response('The parent has no children.', status=status.HTTP_400_BAD_REQUEST) serializer = ChildrenSerializer(children_of_parent, many=True, read_only=True) children_list = [] for child in serializer.data: child['parent']['user'].pop('password') children_list.append(CbcEngine.get_engine().decrypt_child_json(child)) return Response(children_list)
def put(self, request): try: phone_number = CbcEngine.get_engine().encrypt(request.data['parent_id']) parent = Person.objects.get(phone_number=phone_number) if parent.user.username != request.user.username: raise exceptions.AuthenticationFailed(detail='Not authorized request.') except KeyError: return Response('One of the fields are missing.', status=status.HTTP_400_BAD_REQUEST) except Person.DoesNotExist: return Response('parent does not exist.', status=status.HTTP_400_BAD_REQUEST) request.data['parent_id'] = CbcEngine.get_engine().encrypt(request.data['parent_id']) serializer = ChildrenSerializer(data=request.data) if not serializer.is_valid(): return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) try: serializer.save() except IntegrityError: return Response('The child is already exists', status=status.HTTP_400_BAD_REQUEST) return Response('The child added successfully!')
def save(self, **kwargs): cbc_engine = CbcEngine.get_engine() user = self.user user.first_name = cbc_engine.encrypt(user.first_name) user.last_name = cbc_engine.encrypt(user.last_name) user.email = cbc_engine.encrypt(user.email) user.username = cbc_engine.encrypt(user.username) user.save() self.phone_number = cbc_engine.encrypt(self.phone_number) if self.user_id is None: self.user = user super(Person, self).save(**kwargs)
def post(self, request): is_ok = True code_verification_to_check = CbcEngine.get_engine().encrypt( request.data['2fa_pass']) try: two_fa = TwoFactorAuthentication.objects.get(user=request.user) except TwoFactorAuthentication.DoesNotExist as e: return Response( '{0} (probably you try to do 2fa twice sequentially)'.format( e), status=status.HTTP_400_BAD_REQUEST) code_verification = two_fa.code_to_verification two_fa.delete() # if code_verification_to_check != code_verification: # is_ok = False #TODO: Do not forget remove in from comment if not is_ok: raise exceptions.AuthenticationFailed( detail='Two factor authentication failed.') token = Token.objects.get(user=request.user) return Response({'token': token.key})
def post(self, request): random_password = str(randint(100000, 999999)) login_user = request.user two_fa = TwoFactorAuthentication(user=login_user, code_to_verification=random_password) try: two_fa.save() except IntegrityError as e: return Response( '{0} (probably you try to login twice sequentially)'.format(e), status=status.HTTP_400_BAD_REQUEST) user_email_to_send = CbcEngine.get_engine().decrypt(login_user.email) subject = 'Verification Code - Funtestic' body = 'Hi, in order to login into Funtestic App - Please use the following security code: ' + \ random_password try: send_mail(subject, body, user_email_to_send) except SMTPRecipientsRefused: two_fa.delete() raise exceptions.AuthenticationFailed( detail='Invalid email address.') return Response('Login successfully!')
def put(self, request): try: child_id = CbcEngine.get_engine().encrypt( request.data['id_number']) child = Child.objects.get(id_number=child_id) quiz_of_child = Quiz.objects.filter(child=child) except KeyError: return Response('child_id field is missing.', status=status.HTTP_400_BAD_REQUEST) except Child.DoesNotExist: return Response('child does not exist.', status=status.HTTP_400_BAD_REQUEST) except Quiz.DoesNotExist: return Response('The child has no quiz.', status=status.HTTP_400_BAD_REQUEST) request.data['id_number'] = child_id serializer = ReportSerializer(data=request.data) if not serializer.is_valid(): print(serializer.errors) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) try: pdf_name = "{}_report.pdf".format(child.id_number) pdf_dir = "media" if not os.path.exists(pdf_dir): os.makedirs(pdf_dir) try: report = Report.objects.get(child=child) except Report.DoesNotExist as e: report = None print(e) if report is not None: os.unlink('{0}/{1}'.format(pdf_dir, pdf_name)) report.delete() serializer.save() # ____write report to pdf___: pdf = fpdf.FPDF(format='letter') pdf.add_page() pdf.set_font("Arial", "BU", size=24) pdf.set_text_color(0, 0, 128) pdf.write(5, "report:\n\n") pdf.set_font("Arial", size=12) pdf.set_text_color(0, 0, 0) pdf.write( 5, "name: {0}\nage: {1}\ncreated Date: {2}\ngrades: ".format( child, CbcEngine.get_engine().decrypt(child.age), (CbcEngine.get_engine().decrypt( serializer.data['create_at'])).split(' ')[0])) i = 0 for q in quiz_of_child: if i + 1 == len(quiz_of_child): pdf.write( 5, "{0} ".format(CbcEngine.get_engine().decrypt(q.grade))) else: pdf.write( 5, "{0}, ".format(CbcEngine.get_engine().decrypt( q.grade))) i += 1 subject = 'Report for child - Funtestic' body = 'Hi! We sent you a report for your child.' pdf.output('{0}/{1}'.format(pdf_dir, pdf_name)) # ___________________________ send_mail(subject, body, CbcEngine.get_engine().decrypt(child.parent.user.email), pdf_name) except IntegrityError: return Response("The report is already exists", status=status.HTTP_400_BAD_REQUEST) return Response('The report added successfully!')
def save(self, **kwargs): cbc_engine = CbcEngine.get_engine() self.create_at = cbc_engine.encrypt(str(timezone.now())) super(Report, self).save(**kwargs)
def __str__(self): try: name = CbcEngine.get_engine().decrypt(self.name) except (TypeError, ValueError): name = self.name return name
def save(self, **kwargs): cbc_engine = CbcEngine.get_engine() self.code_to_verification = cbc_engine.encrypt(self.code_to_verification) super(TwoFactorAuthentication, self).save(**kwargs)
def save(self, **kwargs): cbc_engine = CbcEngine.get_engine() self.grade = cbc_engine.encrypt(self.grade) super(Quiz, self).save(**kwargs)