Exemple #1
0
def unit_edit(request, city_tag, bldg_tag, unit_tag=''):
    #TODO: probably many instances where this is not right
    #city = City.objects.filter(tag=city_tag)
    cities = City.objects.filter(tag=city_tag)
    city = None
    if cities.count():
        city = cities[0]
    buildings = Building.objects.filter(city=city).filter(tag=bldg_tag)
    if buildings.count():
        building = buildings[0]
        units = building.units.filter(tag=unit_tag)
        if units.count():
            unit = units[0]
        else:
            unit = None
            #raise 404
            pass
        
    else:
        building = None
        unit = None

    if request.method == 'POST':
        form = UnitForm(request.POST, instance=unit)

        if form.is_valid(): # All validation rules pass
            updated = form.save(commit=False)

            #print json.dumps(updated.diff)
            
            #print updated.diff
            changes = ChangeDetails()
            changes.ip_address = get_client_ip(request)
            changes.user = request.user
            changes.diffs = json.dumps(updated.diff)
            changes.building = building
            #not required
            changes.unit = updated
            changes.save()
            
            #now it's ok to save the building details:
            updated.save()

            #now that we've saved the unit,
            #update the averages for the whole building:
            building.update_utility_averages()
            building.update_rent_details()

            #redirect to unit details page with an edit message
            messages.add_message(request, messages.INFO, 'Saved changes to unit.')
            if updated.tag:
                finished_url = reverse('building.views.unit_details', kwargs={'city_tag':city.tag, 'bldg_tag':building.tag, 'unit_tag':updated.tag})
            else:
                finished_url = reverse('building.views.unit_details', kwargs={'city_tag':city.tag, 'bldg_tag':building.tag})
                
            #args=(updated.building.tag, city.tag, updated.tag)
            return redirect(finished_url)
    else:
        print unit
        form = UnitForm(instance=unit)
        
    context = { 'building': building,
                'unit': unit,
                'user': request.user,
                'form': form,
                }
    return render(request, 'unit_edit.html', context)
Exemple #2
0
def edit(request, bldg_tag, city_tag):
    city = City.objects.filter(tag=city_tag)
    buildings = Building.objects.filter(city=city).filter(tag=bldg_tag)
    if buildings.count():
        building = buildings[0]

    else:
        building = None

    if request.method == 'POST':
        form = BuildingForm(request.POST, instance=building)

        if form.is_valid(): # All validation rules pass
            updated = form.save(commit=False)

            #update any summary boolean fields here
            #(this should help with searching)
            if updated.energy_saving_details or updated.energy_saving_other :
                updated.energy_saving_features = True
            else:
                updated.energy_saving_features  = False
            
            if updated.renewable_energy_details or updated.renewable_energy_other :
                updated.renewable_energy = True
            else:
                updated.renewable_energy = False
            
            if updated.garden_details or updated.garden_other:
                updated.garden = True
            else:
                updated.garden = False
            
            if updated.bike_friendly_details or updated.bike_friendly_other :
                updated.bike_friendly = True
            else:
                updated.bike_friendly = False
            
            if updated.walk_friendly_details or updated.walk_friendly_other :
                updated.walk_friendly = True
            else:
                updated.walk_friendly = False
            
            if updated.transit_friendly_details or updated.transit_friendly_other :
                updated.transit_friendly = True
            else:
                updated.transit_friendly = False
            
            if updated.parking_options:
                updated.parking = True
            else:
                updated.parking = False
            
            if updated.pets_options or updated.pets_other :
                updated.pets = True
            else:
                updated.pets = False

            #print json.dumps(updated.diff)
            
            #print updated.diff
            changes = ChangeDetails()
            changes.ip_address = get_client_ip(request)
            changes.user = request.user
            changes.diffs = json.dumps(updated.diff)
            changes.building = updated
            #not required
            #changes.unit =
            changes.save()
            
            #now it's ok to save the building details:
            updated.save()

            #redirect to building details with an edit message
            messages.add_message(request, messages.INFO, 'Saved changes to building.')
            finished_url = reverse('building.views.details', args=(updated.tag, updated.city.tag))
            
            return redirect(finished_url)
    else:
        form = BuildingForm(instance=building)
        
    context = { 'building': building,
                'user': request.user,
                'form': form,

                }
    return render(request, 'building-edit.html', context)
def upload_simple(request, city_tag=None, bldg_tag=None, unit_tag=None):
    (city, building, unit) = find_by_tags(city_tag, bldg_tag, unit_tag)

    (provider_names, utility_providers) = make_provider_names(city)
    
    if request.method == 'POST':
        #print "form posted"
        form = UploadShortForm(request.POST, request.FILES)
        form.fields['utility_provider'].choices = provider_names

        #print form.fields['file'].data
        #print form.fields['file'].cleaned_data
        #print form.fields['file'].cleaned_data

        #if meta.is_valid() and form.is_valid(): 
        if form.is_valid(): 

            errors = False

            if request.FILES.has_key("file"):
                #blob_key = request.FILES["blobkey"].blobstore_info._BlobInfo__key
                blob_key = request.FILES['file'].blobstore_info.key()

                #print "BLOBKEY: ", blob_key
                #obj.blobstore_key = blob_key
                statement = Statement()
                statement.blob_key = blob_key

                statement.original_filename = request.FILES['file'].name
                
                statement.unit = unit

                statement.ip_address = get_client_ip(request)

                #print request.user
                #print dir(request.user)
                if request.user and not request.user.is_anonymous():
                    statement.user = request.user
                
                (provider, company_name) = parse_form_providers(form)
                #if form.cleaned_data.has_key('vendor'):
                #statement.vendor = form.cleaned_data['vendor']

                #should set one of these
                if provider:
                    statement.provider = provider
                else:
                    statement.vendor = company_name

                #if form.cleaned_data.has_key('utility_type'):
                #if meta.cleaned_data['utility_type'] == 'other':
                #if form.cleaned_data['utility_type'] == 'other':
                    #if form.cleaned_data.has_key('alt_type'):
                    #statement.type = form.cleaned_data['alt_type']
                #else:
                statement.type = form.cleaned_data['utility_type']
                                
                statement.save()
                #print statement
                #form.save()
                #return HttpResponseRedirect(view_url)
                #return redirect(view_url, permanent=True)
                #in chrome, the original post url stays in the address bar...
                finished_url = reverse('utility.views.thank_you')
                return redirect(finished_url)

            else:
                print "NO BLOBKEY!!!", str(request)
                print dir(request)
                print request.FILES
                if request.FILES.has_key('file'):
                    print request.FILES['file']
                    print dir(request.FILES['file'])
                    print request.FILES['file'].blobstore_info.key()
                print 

        else:
            #print dir(form)
            #print form.errors
            #print "form did not validate"
            pass

    else:
        #form = UploadShortForm()
        #meta = MetaUtilityForm(prefix='meta')

        form = UploadShortForm()
        form.fields['utility_provider'].choices = provider_names
        
    #view_url = reverse('utility.views.upload_handler')
    view_url = request.path
    #upload_url, upload_data = prepare_upload(request, view_url)
    upload_url = create_upload_url(view_url)
    upload_data = {}
    
    context = {
        'city': city.name,
        'bldg': building,
        'unit': unit,
        #'form': form,
        'meta': form,
        'upload_url': upload_url,
        'providers': json.dumps(utility_providers),        
        }

    return render(request, 'upload_simple.html', context )
def upload(request, state=None, city_name=None, bldg_tag=None, unit_tag=None):
    results = ''
    
    if request.method == 'POST':
        form = UploadForm(request.POST, request.FILES)

        if form.is_valid(): # All validation rules pass
            #need to do a specialized validation here..
            #alt_city and alt_state only required if city == other

            errors = False

            if request.FILES.has_key("file"):
                #blob_key = request.FILES["blobkey"].blobstore_info._BlobInfo__key

                blob_key = request.FILES['file'].blobstore_info.key()

                #print "BLOBKEY: ", blob_key
                #obj.blobstore_key = blob_key
                statement = StatementUpload()
                statement.blob_key = blob_key
                
                statement.city_tag = to_tag(city_name + " " + state)

                if bldg_tag:
                    statement.building_address = bldg_tag
                else:
                    #if form.cleaned_data.has_key('email'):
                    statement.building_address = form.cleaned_data['address']
                statement.unit_number = unit_tag

                statement.ip_address = get_client_ip(request)
                #if form.cleaned_data.has_key('email'):
                statement.person_email = form.cleaned_data['email']

                #print request.user
                #print dir(request.user)
                if request.user and not request.user.is_anonymous():
                    statement.user = request.user
                
                #if form.cleaned_data.has_key('vendor'):
                statement.vendor = form.cleaned_data['vendor']

                #if form.cleaned_data.has_key('utility_type'):
                if form.cleaned_data['utility_type'] == 'other':
                    #if form.cleaned_data.has_key('alt_type'):
                    statement.type = form.cleaned_data['alt_type']
                else:
                    statement.type = form.cleaned_data['utility_type']

                #if form.cleaned_data.has_key('move_in'):
                statement.move_in = form.cleaned_data['move_in']

                #if form.cleaned_data.has_key('energy_options'):
                #statement.energy_options = form.cleaned_data['energy_options']
                options = form.cleaned_data['energy_options']
                if 'other' in options:
                    options.remove('other')
                    if form.cleaned_data['alt_energy']:
                        options.append(form.cleaned_data['alt_energy'])
                
                statement.energy_sources = options

                statement.unit_details = { 'bedrooms': form.cleaned_data['bedrooms'], 'sqft': form.cleaned_data['sqft'], }
                                
                statement.save()
                #print statement
                #form.save()
                #return HttpResponseRedirect(view_url)
                #return redirect(view_url, permanent=True)
                #in chrome, the original post url stays in the address bar...
                finished_url = reverse('utility.views.thank_you')
                return redirect(finished_url)

            ## else:
            ##     print "NO BLOBKEY!!!", str(request)
            ##     print dir(request)
            ##     print request.FILES
            ##     if request.FILES.has_key('file'):
            ##         print request.FILES['file']
            ##         print dir(request.FILES['file'])
            ##         print request.FILES['file'].blobstore_info.key()
            ##     print 

    else:
        form = UploadForm()
        
    #view_url = reverse('utility.views.upload_handler')
    view_url = request.path
    #upload_url, upload_data = prepare_upload(request, view_url)
    upload_url = create_upload_url(view_url)
    upload_data = {}
    
    #print form['utility_type'].errors
    #print form['utility_type'].label
    #print form['utility_type']
    #print dir(form['energy_options'])
    #print form['energy_options']

    context = {
        'city': city_name,
        'state': state,
        'bldg': bldg_tag,
        'form': form,
        'results': results,
        'upload_url': upload_url, 
        }

    return render(request, 'upload_generic.html', context )
Exemple #5
0
def upload(request, state=None, city_name=None, bldg_tag=None, unit_tag=None):
    results = ''

    if request.method == 'POST':
        form = UploadForm(request.POST, request.FILES)

        if form.is_valid():  # All validation rules pass
            #need to do a specialized validation here..
            #alt_city and alt_state only required if city == other

            errors = False

            if request.FILES.has_key("file"):
                #blob_key = request.FILES["blobkey"].blobstore_info._BlobInfo__key

                blob_key = request.FILES['file'].blobstore_info.key()

                #print "BLOBKEY: ", blob_key
                #obj.blobstore_key = blob_key
                statement = StatementUpload()
                statement.blob_key = blob_key

                statement.city_tag = to_tag(city_name + " " + state)

                if bldg_tag:
                    statement.building_address = bldg_tag
                else:
                    #if form.cleaned_data.has_key('email'):
                    statement.building_address = form.cleaned_data['address']
                statement.unit_number = unit_tag

                statement.ip_address = get_client_ip(request)
                #if form.cleaned_data.has_key('email'):
                statement.person_email = form.cleaned_data['email']

                #print request.user
                #print dir(request.user)
                if request.user and not request.user.is_anonymous():
                    statement.user = request.user

                #if form.cleaned_data.has_key('vendor'):
                statement.vendor = form.cleaned_data['vendor']

                #if form.cleaned_data.has_key('utility_type'):
                if form.cleaned_data['utility_type'] == 'other':
                    #if form.cleaned_data.has_key('alt_type'):
                    statement.type = form.cleaned_data['alt_type']
                else:
                    statement.type = form.cleaned_data['utility_type']

                #if form.cleaned_data.has_key('move_in'):
                statement.move_in = form.cleaned_data['move_in']

                #if form.cleaned_data.has_key('energy_options'):
                #statement.energy_options = form.cleaned_data['energy_options']
                options = form.cleaned_data['energy_options']
                if 'other' in options:
                    options.remove('other')
                    if form.cleaned_data['alt_energy']:
                        options.append(form.cleaned_data['alt_energy'])

                statement.energy_sources = options

                statement.unit_details = {
                    'bedrooms': form.cleaned_data['bedrooms'],
                    'sqft': form.cleaned_data['sqft'],
                }

                statement.save()
                #print statement
                #form.save()
                #return HttpResponseRedirect(view_url)
                #return redirect(view_url, permanent=True)
                #in chrome, the original post url stays in the address bar...
                finished_url = reverse('utility.views.thank_you')
                return redirect(finished_url)

            ## else:
            ##     print "NO BLOBKEY!!!", str(request)
            ##     print dir(request)
            ##     print request.FILES
            ##     if request.FILES.has_key('file'):
            ##         print request.FILES['file']
            ##         print dir(request.FILES['file'])
            ##         print request.FILES['file'].blobstore_info.key()
            ##     print

    else:
        form = UploadForm()

    #view_url = reverse('utility.views.upload_handler')
    view_url = request.path
    #upload_url, upload_data = prepare_upload(request, view_url)
    upload_url = create_upload_url(view_url)
    upload_data = {}

    #print form['utility_type'].errors
    #print form['utility_type'].label
    #print form['utility_type']
    #print dir(form['energy_options'])
    #print form['energy_options']

    context = {
        'city': city_name,
        'state': state,
        'bldg': bldg_tag,
        'form': form,
        'results': results,
        'upload_url': upload_url,
    }

    return render(request, 'upload_generic.html', context)
def edit(request, bldg_tag, city_tag):
    (city, building, unit) = find_by_tags(city_tag, bldg_tag, unit_tag='')

    #unless we figure out it should be set, keep it None
    unitform = None
    
    if request.method == 'POST':
        buildingform = BuildingForm(request.POST, instance=building,
                                    prefix='building')

        if buildingform.is_valid(): # All validation rules pass
            #https://docs.djangoproject.com/en/dev/topics/forms/modelforms/#the-save-method
            #by passing commit=False, we get a copy of the model before it
            #has been saved. This allows diff to work below
            updated = buildingform.save(commit=False)

            updated.set_booleans()
            #print json.dumps(updated.diff)
            
            #print updated.diff
            changes = ChangeDetails()
            changes.ip_address = get_client_ip(request)
            changes.user = request.user
            changes.diffs = json.dumps(updated.diff)
            changes.building = updated
            #not required
            #changes.unit =
            changes.save()
            
            #now it's ok to save the building details:
            updated.save()

            unit_ok = False
            if building.units.count() == 1:
                unitform = UnitForm(request.POST, instance=unit, prefix='unit')
                if unitform.is_valid(): # All validation rules pass
                    updated_unit = unitform.save(commit=False)
                    updated_unit.save_and_update(request)
                    unit_ok = True
            else:
                unit_ok = True

            if unit_ok:
                #redirect to building details with an edit message
                messages.add_message(request, messages.INFO, 'Saved changes to building.')
                finished_url = reverse('building.views.details', args=(updated.tag, updated.city.tag))
                
                return redirect(finished_url)

    else:
        buildingform = BuildingForm(instance=building, prefix='building')
        buildingform.fields['name'].label = "Building Name"
        if building.units.count() == 1:
            unitform = UnitForm(instance=unit, prefix='unit')

    context = { 'building': building,
                'user': request.user,
                'buildingform': buildingform,
                'unitform': unitform,
                }
    return render(request, 'building-edit.html', context)