Exemplo n.º 1
0
def createSeller(request):
  from apps.admin.views.forms import AccountCreateForm
  from django.db import IntegrityError
  from apps.seller.views.account import create as createSeller
  context = {}

  if request.method == 'POST':
    name        = request.POST.get('name')
    phone       = request.POST.get('phone')
    password    = request.POST.get('password')
    password    = processPassword(password) if password else None

    if not phone and phone.isdigit():
      context = {'incorrect': "bad phone"}
    elif not password:
      context = {'incorrect': "bad password"}

    else:
      try:
        account = Account(phone=phone, password=password)
        account.name = name if name else None
        account.save()

        if createSeller(account):
          login(request)
          return redirect('seller:edit')
        else:
          e = Exception("createSeller() returned False")
          ExceptionHandler(e, 'in account.createSeller')
          messages.error(request, 'Error creating seller account.')
          account.delete()

      except IntegrityError:
        messages.warning(request, 'An account with this phone already exists.')
        context = {'incorrect': "bad phone"}

      except Exception as e:
        ExceptionHandler(e, "in account.createSeller")
        messages.error(request, e)

  context['form'] = AccountCreateForm()
  return render(request, 'account/create_seller.html', context)
Exemplo n.º 2
0
def createAdmin(request):
  from apps.admin.views.forms import AccountCreateForm
  from django.db import IntegrityError

  if request.method == 'POST':

    name        = request.POST.get('name')
    phone       = request.POST.get('phone')
    password    = request.POST.get('password')
    password    = processPassword(password) if password else None
    admin_type  = request.POST.get('admin_type')

    if not phone:
      context = {'incorrect': "bad phone"}
    elif not password:
      context = {'incorrect': "bad password"}
    elif not admin_type:
      context = {'incorrect': "bad admin_type"}

    else:
      try:
        account = Account(phone=phone, password=password, admin_type=admin_type)
        account.name = name if name else None
        account.save()
        return redirect('admin:edit account', account.id)

      except IntegrityError:
        messages.warning(request, 'An account with this phone already exists.')
        context = {'incorrect': "bad phone"}

      except Exception as e:
        ExceptionHandler(e, "in account.createAdmin")
        messages.error(request, e)

  else:
    context = {}

  context['form'] = AccountCreateForm()
  return render(request, 'account/create_admin.html', context)