def Register(request): registered = False if request.method == 'POST': user_form = UserForm(data=request.POST,prefix = 'user') patient_form = PatientProfileForm(data = request.POST,prefix = 'patient') if user_form.is_valid() and patient_form.is_valid(): user = user_form.save() user.set_password(user.password) user.save() try: user.groups.add(Group.objects.get(name='Patient')) except Group.DoesNotExist: Group.objects.create(name='Patient') user.groups.add(Group.objects.get(name='Patient')) profile = patient_form.save(commit = False) profile.user = user profile.save() entry = LogEntry(username=user.username, datetime=timezone.now(), description='user registered') entry.save() registered = True else: print(user_form.errors, patient_form.errors) else: user_form = UserForm(prefix = 'user') patient_form = PatientProfileForm(prefix = 'patient') return render(request,'register.html',{'user_form': user_form,'patient_form':patient_form , 'registered': registered})
def RegisterAdmin(request): registered = False if request.method == 'POST': user_form = UserForm(data = request.POST, prefix = 'user') admin_form = AdminForm(data = request.POST, prefix = 'admin') if user_form.is_valid() and admin_form.is_valid(): user = user_form.save() user.set_password(user.password) user.save() profile = admin_form.save() try: user.groups.add(Group.objects.get(name = 'Sys Admin')) except Group.DoesNotExist: Group.objects.create(name='Sys Admin') user.groups.add(Group.objects.get(name = 'Sys Admin')) entry = LogEntry(username = user.username, datetime = timezone.now(), description = 'Sys Admin registered') entry.save() registered = True else: print(user_form.errors) else: user_form = UserForm(prefix = 'user') admin_form = AdminForm(prefix = 'admin') return render(request, 'admin.html', {'user_form' : user_form, 'admin_form' : admin_form, 'registered' : registered})