def update(request): flag = 'month_date_month' in request.GET and 'month_date_year' in request.GET if request.method == 'GET' and flag: month_date_month = int(request.GET['month_date_month']) month_date_year = int(request.GET['month_date_year']) if month_date_month and month_date_year: record_date = datetime.date(month_date_year, month_date_month, 1) #(YYYY, MM, DD) eco_profiles_for_user = EcoProfile.objects.filter(user = request.user) try: eco_profile = eco_profiles_for_user.get(current_month = record_date) form = UserUpdateForm(initial = getInitialDataFor(eco_profile, request.user)) message = "Step 2: Update your record from " + str(month_date_month) + "/" + str(month_date_year) return render(request, 'registration/update.html', {'message': message, 'form': form, 'form_method': 'POST', },) except exceptions.ObjectDoesNotExist: eco_profile = EcoProfile() eco_profile.current_month = record_date form = UserUpdateForm(initial = getInitialDataFor(eco_profile, request.user)) message = "Step 2: Create a record for " + str(month_date_month) + "/" + str(month_date_year) return render(request, 'registration/update.html', {'message': message, 'form': form, 'form_method': 'POST', },) elif request.method == 'POST': sub_form = UserUpdateForm(request.POST) if sub_form.is_valid(): data = sub_form.cleaned_data if recordExistsFor(request.user, sub_form.cleaned_data): updateData(request.user, sub_form.cleaned_data, 'update') else: updateData(request.user, sub_form.cleaned_data, 'new') return render(request, 'registration/update.html', {'form': RecordRetrieveForm(), 'message': 'Submission Recorded Successfully! \ Choose another month or visit your profile to view more info', 'form_method': 'GET', }) else: return render(request, 'registration/update.html', {'form': RecordRetrieveForm(), 'message': 'There was an error processing your form. Please try again', 'form_method': 'GET', }) return render(request, 'registration/update.html', {'form': RecordRetrieveForm(), 'message': "Step 1: Select a month/year combination", 'form_method': 'GET', })
def save(self, usr): data = self.cleaned_data ecp = EcoProfile() ecp.user = usr ecp.water_this_month = data['water_bill'] ecp.gas_this_month = int(data['gas']) ecp.heating_oil = data['heating_oil'] ecp.natural_gas = int(data['natural_gas']) ecp.pounds_of_trash = data['trash_weight'] ecp.current_month = data['date_on_bills'] ecp.electricity_this_month = data['electric_bill'] ecp.save() profile = UserProfile.objects.get(user = usr) profile.house_square_footage = data['house_sq'] profile.number_of_people = data['occupants'] profile.save() return ecp
def save(self, usr): data = self.cleaned_data ecp = EcoProfile() ecp.user = usr ecp.water_this_month = data['water_bill'] ecp.gas_this_month = int(data['gas']) ecp.heating_oil = data['heating_oil'] ecp.natural_gas = int(data['natural_gas']) ecp.pounds_of_trash = data['trash_weight'] ecp.current_month = data['date_on_bills'] ecp.electricity_this_month = data['electric_bill'] ecp.save() profile = UserProfile.objects.get(user=usr) profile.house_square_footage = data['house_sq'] profile.number_of_people = data['occupants'] profile.save() return ecp
def updateData(usr, data, method): record_date = data['date_on_bills'] usp = UserProfile.objects.get(user=usr) if method == 'update': assert(recordExistsFor(usr, data)) record = EcoProfile.objects.filter(user = usr).get(current_month = record_date) elif method == 'new': record = EcoProfile(user = usr) record.current_month = record_date record.water_this_month = data['water_bill'] record.gas_this_month = data['gas'] record.electricity_this_month = data['electric_bill'] record.pounds_of_trash = data['trash_weight'] record.natural_gas = data['natural_gas'] record.heating_oil = data['heating_oil'] usp.house_square_footage = data['house_sq'] usp.number_of_people = data['occupants'] usp.save() record.save()
def updateData(usr, data, method): record_date = data['date_on_bills'] usp = UserProfile.objects.get(user=usr) if method == 'update': assert (recordExistsFor(usr, data)) record = EcoProfile.objects.filter(user=usr).get( current_month=record_date) elif method == 'new': record = EcoProfile(user=usr) record.current_month = record_date record.water_this_month = data['water_bill'] record.gas_this_month = data['gas'] record.electricity_this_month = data['electric_bill'] record.pounds_of_trash = data['trash_weight'] record.natural_gas = data['natural_gas'] record.heating_oil = data['heating_oil'] usp.house_square_footage = data['house_sq'] usp.number_of_people = data['occupants'] usp.save() record.save()
def update(request): flag = 'month_date_month' in request.GET and 'month_date_year' in request.GET if request.method == 'GET' and flag: month_date_month = int(request.GET['month_date_month']) month_date_year = int(request.GET['month_date_year']) if month_date_month and month_date_year: record_date = datetime.date(month_date_year, month_date_month, 1) #(YYYY, MM, DD) eco_profiles_for_user = EcoProfile.objects.filter( user=request.user) try: eco_profile = eco_profiles_for_user.get( current_month=record_date) form = UserUpdateForm( initial=getInitialDataFor(eco_profile, request.user)) message = "Step 2: Update your record from " + str( month_date_month) + "/" + str(month_date_year) return render( request, 'registration/update.html', { 'message': message, 'form': form, 'form_method': 'POST', }, ) except exceptions.ObjectDoesNotExist: eco_profile = EcoProfile() eco_profile.current_month = record_date form = UserUpdateForm( initial=getInitialDataFor(eco_profile, request.user)) message = "Step 2: Create a record for " + str( month_date_month) + "/" + str(month_date_year) return render( request, 'registration/update.html', { 'message': message, 'form': form, 'form_method': 'POST', }, ) elif request.method == 'POST': sub_form = UserUpdateForm(request.POST) if sub_form.is_valid(): data = sub_form.cleaned_data if recordExistsFor(request.user, sub_form.cleaned_data): updateData(request.user, sub_form.cleaned_data, 'update') else: updateData(request.user, sub_form.cleaned_data, 'new') return render( request, 'registration/update.html', { 'form': RecordRetrieveForm(), 'message': 'Submission Recorded Successfully! \ Choose another month or visit your profile to view more info', 'form_method': 'GET', }) else: return render( request, 'registration/update.html', { 'form': RecordRetrieveForm(), 'message': 'There was an error processing your form. Please try again', 'form_method': 'GET', }) return render( request, 'registration/update.html', { 'form': RecordRetrieveForm(), 'message': "Step 1: Select a month/year combination", 'form_method': 'GET', })