def process_register(request): """Register a new user""" c = {} c.update(csrf(request)) if request.method == 'POST': reg_key = request.POST.get('reg_key', '') # We only want folks we know and trust with our key to create new AB accounts if not reg_key or reg_key != INTERNAL['REG_KEY']: return HttpResponseRedirect(reverse('landing')) user_reg_form = UserRegForm(request.POST, prefix = "a") org_form = OrganizationFormRegistration(request.POST, prefix = "b") branch_form = BranchForm(request.POST, prefix = "c") if user_reg_form.is_valid() and org_form.is_valid() and branch_form.is_valid(): new_user = user_reg_form.save() new_org = org_form.save(commit=False) new_branch = branch_form.save(commit=False) new_org.user = new_user new_org.save() new_branch.organization = new_org new_branch.save() new_user.backend='django.contrib.auth.backends.ModelBackend' auth.login(request, new_user) return HttpResponseRedirect(reverse('landing')) else: c.update({'user_reg_form': user_reg_form, 'org_form': org_form, 'branch_form': branch_form}) c = RequestContext(request, c) return render_to_response('register.html', c) else: user_reg_form = UserRegForm(prefix = "a") org_form = OrganizationFormRegistration(prefix = "b") branch_form = BranchForm(prefix = "c") c.update({'user_reg_form': user_reg_form, 'org_form': org_form, 'branch_form': branch_form}) c = RequestContext(request, c) return render_to_response("register.html", c)
def branch(request): """Users can add branches from here""" if not request.user.is_authenticated(): return HttpResponseRedirect(reverse('auth_login')) org = Organization.objects.get(user=request.user) if request.method == 'POST': submitted_form = BranchForm(request.POST, ) if submitted_form.is_valid(): branch = submitted_form.save(commit=False) branch.organization = org branch.save() return HttpResponseRedirect(reverse('control_branch')) else: branches = Branch.objects.filter(organization=org) context = { 'user': request.user, 'organization': org, 'branches': branches, 'form': submitted_form, } context = RequestContext(request, context) return render_to_response('control-branch.html', context) else: form = BranchForm() branches = Branch.objects.filter(organization=org) context = { 'user': request.user, 'organization': org, 'branches': branches, 'form': form, } context = RequestContext(request, context) return render_to_response('control-branch.html', context)
def branch(request): """Users can add branches from here""" if not request.user.is_authenticated(): return HttpResponseRedirect(reverse('auth_login')) org = Organization.objects.get(user=request.user) if request.method == 'POST': submitted_form = BranchForm(request.POST,) if submitted_form.is_valid(): branch = submitted_form.save(commit=False) branch.organization = org branch.save() return HttpResponseRedirect(reverse('control_branch')) else: branches = Branch.objects.filter(organization=org) context = { 'user': request.user, 'organization': org, 'branches': branches, 'form': submitted_form, } context = RequestContext(request, context) return render_to_response('control-branch.html', context) else: form = BranchForm() branches = Branch.objects.filter(organization=org) context = { 'user': request.user, 'organization': org, 'branches': branches, 'form': form, } context = RequestContext(request, context) return render_to_response('control-branch.html', context)
def branch_edit(request): """Users can edit branches here""" if not request.user.is_authenticated(): return HttpResponseRedirect(reverse('auth_login')) org = Organization.objects.get(user=request.user) if request.method == 'POST': submitted_form = BranchForm(request.POST, ) branch = Branch.objects.get(organization=org, id=request.POST.get('branch-id')) if submitted_form.is_valid(): form = BranchForm(request.POST, instance=branch) form.save() return HttpResponseRedirect(reverse('control_branch')) else: context = { 'user': request.user, 'organization': org, 'form': submitted_form, 'branch': branch, } context = RequestContext(request, context) return render_to_response('control-branch-edit.html', context) else: branch = Branch.objects.get(id=request.GET.get('branch-id'), organization=org) form = BranchForm(instance=branch) context = { 'user': request.user, 'organization': org, 'form': form, 'branch': branch } context = RequestContext(request, context) return render_to_response('control-branch-edit.html', context)
def process_register(request): """Register a new user""" c = {} c.update(csrf(request)) if request.method == 'POST': reg_key = request.POST.get('reg_key', '') # We only want folks we know and trust with our key to create new AB accounts if not reg_key or reg_key != INTERNAL['REG_KEY']: return HttpResponseRedirect(reverse('landing')) user_reg_form = UserRegForm(request.POST, prefix="a") org_form = OrganizationFormRegistration(request.POST, prefix="b") branch_form = BranchForm(request.POST, prefix="c") if user_reg_form.is_valid() and org_form.is_valid( ) and branch_form.is_valid(): new_user = user_reg_form.save() new_org = org_form.save(commit=False) new_branch = branch_form.save(commit=False) new_org.user = new_user new_org.save() new_branch.organization = new_org new_branch.save() new_user.backend = 'django.contrib.auth.backends.ModelBackend' auth.login(request, new_user) return HttpResponseRedirect(reverse('landing')) else: c.update({ 'user_reg_form': user_reg_form, 'org_form': org_form, 'branch_form': branch_form }) c = RequestContext(request, c) return render_to_response('register.html', c) else: user_reg_form = UserRegForm(prefix="a") org_form = OrganizationFormRegistration(prefix="b") branch_form = BranchForm(prefix="c") c.update({ 'user_reg_form': user_reg_form, 'org_form': org_form, 'branch_form': branch_form }) c = RequestContext(request, c) return render_to_response("register.html", c)
def branch_delete(request): """Users are presented with the option to delete a branch here""" if not request.user.is_authenticated(): return HttpResponseRedirect(reverse('auth_login')) org = Organization.objects.get(user=request.user) if request.method == 'POST': submitted_form = BranchForm(request.POST, ) branch = Branch.objects.get(organization=org, id=request.POST.get('branch-id')) try: transfer_branch = Branch.objects.get( organization=org, id=request.POST.get('transfer-branch')) except Branch.DoesNotExist: transfer_branch = None if transfer_branch: Item.objects.filter(branch=branch).update(branch=transfer_branch) branch.delete() return HttpResponseRedirect(reverse('control_branch')) else: branch = Branch.objects.get(id=request.GET.get('branch-id'), organization=org) transfer_branches = Branch.objects.filter(organization=org).exclude( id=branch.id) context = { 'user': request.user, 'organization': org, 'branch': branch, 'transfer_branches': transfer_branches, } context = RequestContext(request, context) return render_to_response('control-branch-delete.html', context)
def branch_edit(request): """Users can edit branches here""" if not request.user.is_authenticated(): return HttpResponseRedirect(reverse('auth_login')) org = Organization.objects.get(user=request.user) if request.method == 'POST': submitted_form = BranchForm(request.POST,) branch = Branch.objects.get(organization=org, id=request.POST.get('branch-id')) if submitted_form.is_valid(): form = BranchForm(request.POST, instance=branch) form.save() return HttpResponseRedirect(reverse('control_branch')) else: context = { 'user': request.user, 'organization': org, 'form': submitted_form, 'branch': branch, } context = RequestContext(request, context) return render_to_response('control-branch-edit.html', context) else: branch = Branch.objects.get(id=request.GET.get('branch-id'), organization=org) form = BranchForm(instance=branch) context = { 'user': request.user, 'organization': org, 'form': form, 'branch': branch } context = RequestContext(request, context) return render_to_response('control-branch-edit.html', context)