def approve_pending_edit(request, pending_edit_id): pend, model = get_tree_pend_or_plot_pend_by_id_or_404_not_found(pending_edit_id) pend.approve_and_reject_other_active_pends_for_the_same_field(request.user) if model == 'Tree': change_reputation_for_user(pend.submitted_by, 'edit tree', pend.tree, change_initiated_by_user=pend.updated_by) updated_plot = Plot.objects.get(pk=pend.tree.plot.id) else: # model == 'Plot' change_reputation_for_user(pend.submitted_by, 'edit plot', pend.plot, change_initiated_by_user=pend.updated_by) updated_plot = Plot.objects.get(pk=pend.plot.id) return plot_to_dict(updated_plot, longform=True)
def update_plot_and_tree(request, plot_id): response = HttpResponse() try: plot = Plot.objects.get(pk=plot_id) except Plot.DoesNotExist: response.status_code = 400 response.content = simplejson.dumps({"error": "No plot with id %s" % plot_id}) return response request_dict = json_from_request(request) flatten_plot_dict_with_tree_and_geometry(request_dict) plot_field_whitelist = ['plot_width','plot_length','type','geocoded_address','edit_address_street', 'address_city', 'address_street', 'address_zip', 'power_lines', 'sidewalk_damage'] # The Django form that creates new plots expects a 'plot_width' parameter but the # Plot model has a 'width' parameter so this dict acts as a translator between request # keys and model field names plot_field_property_name_dict = {'plot_width': 'width', 'plot_length': 'length', 'power_lines': 'powerline_conflict_potential'} # The 'auth.change_user' permission is a proxy for 'is the user a manager' user_is_not_a_manager = not request.user.has_perm('auth.change_user') should_create_plot_pends = settings.PENDING_ON and plot.was_created_by_a_manager and user_is_not_a_manager plot_was_edited = False for plot_field_name in request_dict.keys(): if plot_field_name in plot_field_whitelist: if plot_field_name in plot_field_property_name_dict: new_name = plot_field_property_name_dict[plot_field_name] else: new_name = plot_field_name new_value = request_dict[plot_field_name] if not compare_fields(getattr(plot, new_name), new_value): if should_create_plot_pends: plot_pend = PlotPending(plot=plot) plot_pend.set_create_attributes(request.user, new_name, new_value) plot_pend.save() else: setattr(plot, new_name, new_value) plot_was_edited = True # TODO: Standardize on lon or lng if 'lat' in request_dict or 'lon' in request_dict or 'lng' in request_dict: new_geometry = Point(x=plot.geometry.x, y=plot.geometry.y) if 'lat' in request_dict: new_geometry.y = request_dict['lat'] if 'lng' in request_dict: new_geometry.x = request_dict['lng'] if 'lon' in request_dict: new_geometry.x = request_dict['lon'] if plot.geometry.x != new_geometry.x or plot.geometry.y != new_geometry.y: if should_create_plot_pends: plot_pend = PlotPending(plot=plot) plot_pend.set_create_attributes(request.user, 'geometry', new_geometry) plot_pend.save() else: plot.geometry = new_geometry plot_was_edited = True if plot_was_edited: plot.last_updated = datetime.datetime.now() plot.last_updated_by = request.user plot.save() change_reputation_for_user(request.user, 'edit plot', plot) tree_was_edited = False tree_was_added = False tree = plot.current_tree() tree_field_whitelist = ['species','dbh','height','canopy_height', 'canopy_condition', 'condition'] if tree is None: should_create_tree_pends = False else: should_create_tree_pends = settings.PENDING_ON and tree.was_created_by_a_manager and user_is_not_a_manager for tree_field in Tree._meta.fields: if tree_field.name in request_dict and tree_field.name in tree_field_whitelist: if tree is None: import_event, created = ImportEvent.objects.get_or_create(file_name='site_add',) tree = Tree(plot=plot, last_updated_by=request.user, import_event=import_event) tree.plot = plot tree.last_updated_by = request.user tree.save() tree_was_added = True if tree_field.name == 'species': try: if (tree.species and tree.species.pk != request_dict[tree_field.name]) \ or (not tree.species and request_dict[tree_field.name]): if should_create_tree_pends: tree_pend = TreePending(tree=tree) tree_pend.set_create_attributes(request.user, 'species_id', request_dict[tree_field.name]) tree_pend.save() else: tree.species = Species.objects.get(pk=request_dict[tree_field.name]) tree_was_edited = True except Exception: response.status_code = 400 response.content = simplejson.dumps({"error": "No species with id %s" % request_dict[tree_field.name]}) return response else: # tree_field.name != 'species' if not compare_fields(getattr(tree, tree_field.name), request_dict[tree_field.name]): if should_create_tree_pends: tree_pend = TreePending(tree=tree) tree_pend.set_create_attributes(request.user, tree_field.name, request_dict[tree_field.name]) tree_pend.save() else: setattr(tree, tree_field.name, request_dict[tree_field.name]) tree_was_edited = True if tree_was_edited: tree.last_updated = datetime.datetime.now() tree.last_updated_by = request.user if tree_was_added or tree_was_edited: tree.save() # You cannot get reputation for both adding and editing a tree in one action # so I use an elif here if tree_was_added: change_reputation_for_user(request.user, 'add tree', tree) elif tree_was_edited: change_reputation_for_user(request.user, 'edit tree', tree) full_plot = Plot.objects.get(pk=plot.id) return_dict = plot_to_dict(full_plot, longform=True,user=request.user) response.status_code = 200 response.content = simplejson.dumps(return_dict) return response
if '__all__' in form.errors: response.content = simplejson.dumps({"error": form.errors['__all__']}) else: response.content = simplejson.dumps({"error": form.errors}) return response try: new_plot = form.save(request) except ValidationError, ve: response.status_code = 400 response.content = simplejson.dumps({"error": form.error_class(ve.messages)}) return response new_tree = new_plot.current_tree() if new_tree: change_reputation_for_user(request.user, 'add tree', new_tree) else: change_reputation_for_user(request.user, 'add plot', new_plot) response.status_code = 201 new_plot = plot_to_dict(Plot.objects.get(pk=new_plot.id),longform=True,user=request.user) response.content = json.dumps(new_plot) return response @require_http_methods(["GET"]) @api_call() @login_optional def get_plot(request, plot_id): return plot_to_dict(Plot.objects.get(pk=plot_id),longform=True,user=request.user) def compare_fields(v1,v2):