text: from django.utils.http import urlsafe_base64_encode, urlsafe_base64_decode
  from django.contrib.sites.shortcuts import get_current_site
  from django.utils.encoding import force_bytes, force_text
  from django.template.loader import render_to_string
  from django.contrib.auth import get_user_model
  from django.shortcuts import render, redirect
  from django.core.mail import EmailMessage
  from .forms import CreateUserForm, ChangeEmailForm, ChangeUserForm, ChangeUserFormCompany
  from .token import account_activation_token, email_change_token
  
  
  def settings(request):
      if not request.user.is_authenticated:
          return redirect('store:index')
      else:
          if request.method == 'POST':
              form = ChangeUserForm(request.POST)
              form_company = ChangeUserFormCompany(request.POST)
              if form.is_valid():
                  request.user.first_name = form.data.get('first_name')
                  request.user.last_name = form.data.get('last_name')
                  request.user.postal_code = form.data.get('postal_code')
                  request.user.city = form.data.get('city')
                  request.user.address = form.data.get('address')
                  request.user.save()
                  return redirect('account:settings')
              if form_company.is_valid():
                  request.user.nip = form_company.data.get('nip')
                  request.user.company_name = form_company.data.get('company_name')
                  request.user.company_postal_code = form_company.data.get('company_postal_code')
                  request.user.company_city = form_company.data.get('company_city')
                  request.user.company_address = form_company.data.get('company_address')
                  request.user.save()
                  return redirect('account:settings')
          else:
              form = ChangeUserForm(initial={
                  'first_name': request.user.first_name,
                  'last_name': request.user.last_name,
                  'postal_code': request.user.postal_code,
                  'city': request.user.city,
                  'address': request.user.address
              })
              form_company = ChangeUserFormCompany(initial={
                  'nip': request.user.nip,
                  'company_name': request.user.company_name,
                  'company_postal_code': request.user.company_postal_code,
                  'company_city': request.user.company_city,
                  'company_address': request.user.company_address
              })
          return render(request, 'views/settings.html', {'form': form, 'form_company': form_company})
  
  
  def signup(request):
      if not request.user.is_authenticated:
          if request.method == 'POST':
              form = CreateUserForm(request.POST)
              if form.is_valid():
                  user = form.save(commit=False)
                  user.is_active = False
                  user.save()
                  current_site = get_current_site(request)
                  mail_subject = "Link aktywacyjny / Activation link / Lien d'activation"
                  message = render_to_string('email/active_email.html', {
                      'user': user,
                      'domain': current_site.domain,
                      'uid': urlsafe_base64_encode(force_bytes(user.pk)),
                      'token': account_activation_token.make_token(user),
                  })
                  to_email = form.cleaned_data.get('email')
                  email = EmailMessage(
                      mail_subject, message, to=[to_email]
                  )
                  email.send()
                  return redirect('account:activate')
              else:
                  return render(request, 'views/signup.html', {'form': form, 'message': 'Form is not valid.'})
          else:
              form = CreateUserForm()
          return render(request, 'views/signup.html', {'form': form})
      else:
          return redirect('store:index')
  
  
  def activate(request):
      if request.user.is_authenticated:
          return redirect('store:index')
      else:
          return render(request, 'views/activate.html')
  
  
  def activate_done(request):
      if request.user.is_authenticated:
          return redirect('store:index')
      else:
          return render(request, 'views/activate_done.html')
  
  
  def activate_error(request):
      if request.user.is_authenticated:
          return redirect('store:index')
      else:
          return render(request, 'views/activate_error.html')
  
  
  def activate_link(request, uidb64, token):
      if request.user.is_authenticated:
          return redirect('store:index')
      else:
          User = get_user_model()
          try:
              uid = force_text(urlsafe_base64_decode(uidb64))
              user = User.objects.get(pk=uid)
          except(TypeError, ValueError, OverflowError, User.DoesNotExist):
              user = None
          if user is not None and account_activation_token.check_token(user, token):
              user.is_active = True
              user.save()
              return redirect('account:activate_done')
          else:
              return redirect('account:activate_error')
  
  
  def email_change(request):
      if request.user.is_authenticated:
          if request.method == 'POST':
              form = ChangeEmailForm(request.POST)
              if form.is_valid() and request.user.email == form.data.get('current_email'):
                  if form.data.get('new_email') == form.data.get('new_email_confirm'):
                      user = form.save(commit=False)
                      user.save()
                      current_site = get_current_site(request)
                      mail_subject = "Zmiana email"
                      message = render_to_string('email/change_email.html', {
                          'user': user,
                          'domain': current_site.domain,
                          'uid': urlsafe_base64_encode(force_bytes(user.current_email)),
                          'token': email_change_token.make_token(user),
                      })
                      to_email = form.data.get('new_email')
                      email = EmailMessage(
                          mail_subject, message, to=[to_email]
                      )
                      email.send()
                      return redirect('account:email_change_done')
                  else:
                      return render(request, 'views/email_change.html', {'form': form, 'wrong_email': 'You provided different emails!'})
              else:
                  return render(request, 'views/email_change.html', {'form': form, 'wrong_email': 'Your current E-Mail is wrong'})
          else:
              form = ChangeEmailForm()
          return render(request, 'views/email_change.html', {'form': form})
      else:
          return redirect('store:index')
  
  
  def email_change_done(request):
      if not request.user.is_authenticated:
          return redirect('store:index')
      else:
          return render(request, 'views/email_change_done.html')
  
  
  def email_change_error(request):
      if not request.user.is_authenticated:
          return redirect('store:index')
      else:
          return render(request, 'views/email_change_error.html')
  
  
  def email_change_confirm(request):
      return render(request, 'views/email_change_confirm.html')
  
  
  def email_change_link(request, uidb64, token, new_email, current_email):
      if not request.user.is_authenticated:
          return redirect('store:index')
      else:
          User = get_user_model()
          user = User.objects.get(email=current_email)
          if user is not None and user.email == force_text(urlsafe_base64_decode(uidb64)) and email_change_token.check_token(user, token):
              user.email = new_email
              user.save()
              return redirect('account:email_change_confirm')
          elif(TypeError, ValueError, OverflowError, User.DoesNotExist):
              user = None
              return redirect('account:email_change_error')
  