Пример #1
0
 def forget_password(request):
     try:
         response = {}
         email = request.data.pop("email", '')
         users = Users.objects.filter(email=email, is_active=1)
         if users.exists():
             user = users[0]
             current_time = datetime.now()
             expired_date = current_time + timedelta(hours=1)
             reset_code = user.resetpassword_set.filter(
                 already_used=0, expired_at__gt=current_time)
             if reset_code.exists():
                 hash_code = reset_code[0].hash_code
                 ResetPassword.objects.filter(id=reset_code[0].id).update(
                     expired_at=expired_date)
             else:
                 # generate hash code and store
                 key = ''.join(
                     random.choice(string.ascii_letters + string.digits +
                                   string.ascii_letters)
                     for _ in range(10)) + str(datetime.now())
                 key = key.encode('utf-8')
                 hash_code = hashlib.sha224(key).hexdigest()
                 ResetPassword(user=user,
                               hash_code=hash_code,
                               expired_at=expired_date).save()
             # base_url = settings.SITE_URL
             base_url = "http://" + request.get_host()
             mail_template = "mails/reset_password.html"
             context = {'base_url': base_url, 'key': hash_code}
             subject = "Supplementer ::Password Reset"
             to = user.email
             CommonView.sendEmail(request, mail_template, context, subject,
                                  to, user.id)
             response['success'] = True
             response[
                 'message'] = "A reset password email is sent to you with confirmation link"
             return Response(response, status=status.HTTP_200_OK)
         else:
             return Response(
                 {
                     'success': False,
                     'message': "Email doesn't found"
                 },
                 status=status.HTTP_400_BAD_REQUEST)
     except Exception as e:
         LogHelper.efail(e)
         return Response(
             {
                 'success': False,
                 'message': "Something went wrong."
             },
             status=status.HTTP_500_INTERNAL_SERVER_ERROR)
Пример #2
0
 def form_valid(self, form):
     form.save(request=self.request)
     mailTemplate = "mails/user_password_change.html"
     context = {
         "user_full_name": self.object.get_full_name(),
         "password": self.request.POST.get('password')
     }
     subject = "Password Change"
     to = self.object.email
     CommonView.sendEmail(self.request, mailTemplate, context, subject, to,
                          self.object.id)
     return HttpResponseRedirect(self.get_success_url())
Пример #3
0
 def post(self, request, *args, **kwargs):
     form = self.form_class(request.POST, request.FILES)
     working_types = []
     working_components = request.POST.getlist('working_type')
     for component in working_components:
         component_id = component.split("-")[0]
         component_name = component.split("-")[1]
         working_types.append({
             'id': component_id,
             'component': component_name
         })
     if form.is_valid():
         obj = form.save(self, request)
         data = {
             "company_name": request.POST.get('company_name'),
             "telephone_office": request.POST.get('telephone_office'),
             "telephone_mobile": request.POST.get('telephone_mobile'),
             "working_type": working_types,
             "user_id": obj.id,
         }
         CompaniesView.create_worker(request, data)
         mailTemplate = "mails/user_registered.html"
         context = {
             "user_full_name": obj.get_full_name(),
             "password": self.request.POST.get('password'),
             "username": self.request.POST.get('username')
         }
         subject = "Worker Register"
         to = obj.email
         CommonView.sendEmail(self.request, mailTemplate, context, subject,
                              to, obj.id)
         return HttpResponseRedirect('/companies/')
     components = CommonView.get_all_main_component(request)
     return render(
         request, self.template_name, {
             'form': form,
             'components': components,
             'working_types': working_components
         })