def add_vehicle_car(request): main_user = request.user.house_user other = main_user.get_other_users() if request.method == 'POST': in_form = AddVehicleCarForm(request.POST) operate_formset = OperateFormSet(request.POST) if operate_formset.is_valid() and in_form.is_valid(): vehicle_type = VehicleType.objects.get(vehicle_type='Car') new_vehicle = VehicleGeneric.objects.create( vehicle_type=vehicle_type, date_registered=date.today(), date_purchased=in_form.cleaned_data['date_purchased'] ) new_car = VehicleCar.objects.create( vehicle=new_vehicle, year_produced=in_form.cleaned_data['year_produced'], make=in_form.cleaned_data['make'], model=in_form.cleaned_data['model'], license_plate=in_form.cleaned_data['license_plate'].upper(), milage_purchased=in_form.cleaned_data['milage_purchased'], milage_registered=in_form.cleaned_data['milage_registered'], vin=in_form.cleaned_data['vin'], owned_by=main_user ) for operate_form in operate_formset: user_pk = int(operate_form.cleaned_data['user_id']) can_operate = operate_form.cleaned_data['can_operate'] operate_user = HouseUser.objects.get(pk=user_pk) operate_map = VehicleCarUserPermission.objects.create( car=new_car, user=operate_user, can_operate=can_operate, ) return HttpResponseRedirect("/vehicle/") else: template = loader.get_template('base/err_template.html') return HttpResponse(template.render({'form':in_form})) template = "vehicle/add_edit_vehicle.html" in_form = AddVehicleCarForm() init_formset_data = [] for person in other: ######################### # First field is made by VoidWidget to display username AS part of form ######################### init_form_data={ 'id':None, 'user_name':person.user, # This is for display purposes 'user_id':person.user.pk, 'can_operate':True} init_formset_data.append(init_form_data) operate_formset = OperateFormSet(initial=init_formset_data) context = {'form':in_form,'operate_formset':operate_formset} context['action'] = 'add' context['username'] = request.session['user_name'] return render(request,template,context)
def edit_vehicle_car(request,in_vehicle_id): main_user = request.user.house_user other = main_user.get_other_users() edit_vehicle = VehicleCar.objects.get(pk=in_vehicle_id) if request.method == 'POST': in_form = AddVehicleCarForm(request.POST) operate_formset = OperateFormSet(request.POST) if operate_formset.is_valid() and in_form.is_valid(): edit_vehicle.year_produced = in_form.cleaned_data['year_produced'] edit_vehicle.make = in_form.cleaned_data['make'] edit_vehicle.model = in_form.cleaned_data['model'] edit_vehicle.license_plate = in_form.cleaned_data['license_plate'].upper() edit_vehicle.milage_purchased = in_form.cleaned_data['milage_purchased'] edit_vehicle.milage_registered = in_form.cleaned_data['milage_registered'] edit_vehicle.vin = in_form.cleaned_data['vin'] edit_vehicle.save() for operate_form in operate_formset: map_pk = int(operate_form.cleaned_data['id']) can_operate = operate_form.cleaned_data['can_operate'] operate_map = VehicleCarUserPermission.objects.get(pk=map_pk) operate_map.can_operate=can_operate operate_map.save() return HttpResponseRedirect("/vehicle/") else: template = loader.get_template('base/err_template.html') return HttpResponse(template.render({'form':in_form})) template = "vehicle/add_edit_vehicle.html" in_form = AddVehicleCarForm(instance=edit_vehicle, initial={'date_purchased':edit_vehicle.vehicle.date_purchased}) init_formset_data = [] for person in other: map_obj = VehicleCarUserPermission.objects.filter(car=edit_vehicle).get(user_id=person.pk) init_form_data={ 'id':map_obj.id, 'user_name':map_obj.user, # This is for display purposes 'can_operate':map_obj.can_operate} init_formset_data.append(init_form_data) operate_formset = OperateFormSet(initial=init_formset_data) context = {'form':in_form,'operate_formset':operate_formset} context['username'] = request.session['user_name'] return render(request,template,context)
def add_person(request): house_user = request.user.house_user ######################## # This should be changed to all accounts for this household accounts = Account.objects.filter(created_by=house_user) cars = VehicleCar.objects.filter(owned_by=house_user) if request.method == 'POST': in_form = AddEditUserForm(request.POST) account_access_formset = AccessFormSet(request.POST) car_access_formset = OperateFormSet(request.POST) if in_form.is_valid() and access_formset.is_valid() and car_access_formset.is_valid(): email = in_form.cleaned_data['email'] username = email ########################## # First time password password = '******' auth_user = User.objects.create_user( username, email, password ) auth_user.is_active = in_form.cleaned_data['can_login'] auth_user.save() ssn=in_form.cleaned_data['complete_ssn'] new_user = HouseUser.objects.create( auth_user = auth_user, created_by = house_user.auth_user.id, dob = in_form.cleaned_data['dob'], sex = in_form.cleaned_data['sex'], ssn_13 = ssn[0:3], ssn_45 = ssn[4:6], ssn_69 = ssn[7:11], first_name = in_form.cleaned_data['first_name'], last_name = in_form.cleaned_data['last_name'], email = email, title = in_form.cleaned_data['title'], ) if 'suffix' in in_form.cleaned_data: suffix = in_form.cleaned_data['suffix'] new_user.save() household = house_user.user_map.household map_obj = MapUserHousehold.objects.create( household = household, user = new_user ) if 'hh_superuser' in in_form.cleaned_data: map_obj.hh_superuser = in_form.cleaned_data['hh_superuser'] map_obj.save() for acct_access_form in account_access_formset: acct_pk = int(acct_access_form.cleaned_data['account_id']) can_edit = acct_access_form.cleaned_data['can_edit'] can_view = acct_access_form.cleaned_data['can_view'] can_manage = acct_access_form.cleaned_data['can_manage'] access_account = Account.objects.get(pk=acct_pk) acct_access_map = AccountUserPermission.objects.create( account = access_account, user = new_user, can_view = can_view, can_manage = can_manage, can_edit =can_edit ) for car_access_form in car_access_formset: car_pk = int(car_access_form.cleaned_data['vehicle_id']) can_operate = car_access_form.cleaned_data['can_operate'] access_car = VehicleCar.objects.get(pk=car_pk) car_access_map = VehicleCarUserPermission.objects.create( user = new_user, car = access_car, can_operate = can_operate ) return HttpResponseRedirect("/people/") else: template = loader.get_template('base/err_template.html') return HttpResponse(template.render({'form':in_form})) template = "people/add_edit_user.html" in_form = AddEditUserForm(initial={'login_enabled':True}) account_init_formset_data = [] for account in accounts: init_form_data = { 'account_id':account.pk, 'account_name':account.acct_name, 'can_view':True, 'can_manage':True, 'can_edit':False} account_init_formset_data.append(init_form_data) account_access_formset = AccessFormSet(initial=account_init_formset_data) car_init_formset_data = [] for car in cars: init_form_data = { 'car_name':car.make+' '+car.model, 'vehicle_id':car.pk, 'can_operate':True } car_init_formset_data.append(init_form_data) car_access_formset = OperateFormSet(initial=car_init_formset_data) context = {'form':in_form,'account_access_formset':account_access_formset} context['car_access_formset'] = car_access_formset context['username'] = request.session['user_name'] return render(request,template,context)
def edit_person(request,in_user_id): house_user = request.user.house_user edit_auth_user = User.objects.get(pk=in_user_id) edit_house_user = edit_auth_user.house_user map_obj = edit_house_user.user_map # map_obj = MapUserHousehold.objects.get(pk=edit_house_user) ######################## # This should be changed to all accounts for this household accounts = Account.objects.filter(created_by=house_user) cars = VehicleCar.objects.filter(owned_by=house_user) if request.method == 'POST': in_form = AddEditUserForm(request.POST) account_access_formset = AccessFormSet(request.POST) car_access_formset = OperateFormSet(request.POST) if in_form.is_valid() and access_formset.is_valid() and car_access_formset.is_valid(): edit_auth_user.email = in_form.cleaned_data['email'] edit_auth_user.username = in_form.cleaned_data['email'] edit_auth_user.is_active = in_form.cleaned_data['can_login'] edit_auth_user.save() ssn=in_form.cleaned_data['complete_ssn'] edit_house_user.dob = in_form.cleaned_data['dob'] edit_house_user.sex = in_form.cleaned_data['sex'] edit_house_user.ssn_13 = ssn[0:3] edit_house_user.ssn_45 = ssn[4:6] edit_house_user.ssn_69 = ssn[7:11] edit_house_user.first_name = in_form.cleaned_data['first_name'] edit_house_user.last_name = in_form.cleaned_data['last_name'] edit_house_user.email = in_form.cleaned_data['email'] edit_house_user.title = in_form.cleaned_data['title'] if 'suffix' in in_form.cleaned_data: edit_house_user.suffix = in_form.cleaned_data['suffix'] household_user.save() if 'hh_superuser' in in_form.cleaned_data: map_obj.hh_superuser = in_form.cleaned_data['hh_superuser'] map_obj.save() for acct_access_form in account_access_formset: access_map_pk = int(acct_access_form.cleaned_data['id']) can_edit = acct_access_form.cleaned_data['can_edit'] can_view = acct_access_form.cleaned_data['can_view'] can_manage = acct_access_form.cleaned_data['can_manage'] access_map = AccountUserPermission.objects.get(pk=access_map_pk) access_map.can_view = can_view access_map.can_manage = can_manage access_map.can_edit = can_edit access_map.save() for car_access_form in car_access_formset: access_map_pk = int(car_access_form.cleaned_data['id']) can_operate = car_access_form.cleaned_data['can_operate'] access_map = VehicleCarUserPermission.objects.get(pk=access_map_pk) access_map.can_operate = can_operate access_map.save() return HttpResponseRedirect("/people/") else: template = loader.get_template('base/err_template.html') return HttpResponse(template.render({'form':in_form})) template = "people/add_edit_user.html" in_form = AddEditUserForm(instance=edit_house_user, initial={'can_login':edit_auth_user.is_active, 'complete_ssn':edit_house_user.get_complete_ssn(clear=False), 'hh_superuser':map_obj.hh_superuser}) account_init_formset_data = [] for account in accounts: access_map = AccountUserPermission.objects.filter(account=account).get(user=edit_house_user) init_form_data={ 'id':access_map.pk, 'account_name':account.acct_name, 'can_view':access_map.can_view, 'can_manage':access_map.can_manage, 'can_edit':access_map.can_edit} account_init_formset_data.append(init_form_data) account_access_formset = AccessFormSet(initial=account_init_formset_data) car_init_formset_data = [] for car in cars: access_map = VehicleCarUserPermission.objects.filter(car=car).get(user=edit_house_user) init_form_data={ 'id':access_map.pk, 'can_operate':access_map.can_operate } car_init_formset_data.append(init_form_data) car_access_formset = OperateFormSet(initial=car_init_formset_data) context = {'form':in_form,'account_access_formset':account_access_formset} context['car_access_formset'] = car_access_formset context['username'] = request.session['user_name'] return render(request,template,context)