Skip to content

Commit

Permalink
Added delete tree feature
Browse files Browse the repository at this point in the history
Fixed verify edits user filter
  • Loading branch information
Alsaihn committed Feb 17, 2011
1 parent 8e45be7 commit c1c9847
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 34 deletions.
16 changes: 15 additions & 1 deletion static/treemap.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -1657,7 +1657,7 @@ var tm = {
}, },
cancelDiameters: function() { cancelDiameters: function() {
if (isNaN(parseFloat(tm.currentDiameter))) { if (isNaN(parseFloat(tm.currentDiameter))) {
$("#edit_dbh").html("Click to edit"); $("#edit_dbh").html("Click icon to edit");
} else { } else {
$("#edit_dbh").html(parseFloat(tm.currentDiameter).toFixed(1)); $("#edit_dbh").html(parseFloat(tm.currentDiameter).toFixed(1));
} }
Expand Down Expand Up @@ -1712,6 +1712,20 @@ var tm = {
$("#edit_dbh").html(total.toFixed(1)); $("#edit_dbh").html(total.toFixed(1));
tm.editingDiameter = false; tm.editingDiameter = false;


},

deleteTree: function(tree_id) {
$.ajax({
url: '/trees/' + tree_id + '/delete/',
dataType: 'json',
type: 'POST',
success: function(response) {
window.location = "/map/";
},
error: function(err) {
alert("Error: " + err.status + "\nQuery: " + user_id + " " + rep_total);
}
});
}, },


updateSpeciesFromKey: function(tree_code, tree_cultivar) { updateSpeciesFromKey: function(tree_code, tree_cultivar) {
Expand Down
8 changes: 1 addition & 7 deletions templates/treemap/tree_detail.html
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -296,18 +296,12 @@ <h2>Common attributes</h2>
<td>{{tree.date_planted|date|default:"Missing"}}</td> <td>{{tree.date_planted|date|default:"Missing"}}</td>
<td></td> <td></td>
</tr> </tr>
<tr>
<td class="col1Wide">Tree steward</td>
<td>{{tree.steward_name|default:"Missing"}}</td>
<td></td>
</tr>
<!--
<tr> <tr>
<td class="col1Wide">Data owner</td> <td class="col1Wide">Data owner</td>
<td>{{tree.data_owner}}</td> <td>{{tree.data_owner}}</td>
<td></td> <td></td>
</tr> </tr>
-->
</tbody> </tbody>
</table> </table>


Expand Down
10 changes: 8 additions & 2 deletions templates/treemap/tree_edit.html
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ <h2 id="pageHead">
{% endifequal %} {% endifequal %}
{% ifequal field.type "diameter_field" %} {% ifequal field.type "diameter_field" %}
<tr> <tr>
<td class="col1">{{field.label}} <a href="/resources/#howto" title="Not sure how to measure this?" target="_blank">(?)</a></td> <td class="col1 activeEditRowLabel">{{field.label}} <a href="/resources/#howto" title="Not sure how to measure this?" target="_blank">(?)</a></td>
<td id="edit_{{field.name}}">{{ field.display|add:" in"|default:"Click to edit" }}</td> <td class="activeEditRowValue" id="edit_{{field.name}}">{{ field.display|add:" in"|default:"Click icon to edit" }}</td>
<td style="vertical-align:top;"><a href="#" onclick="tm.editDiameter('#edit_{{field.name}}', {{field.diams|safe}}); return false;"><img src="/static/images/edit.png" class="editOption" /> </a></td> <td style="vertical-align:top;"><a href="#" onclick="tm.editDiameter('#edit_{{field.name}}', {{field.diams|safe}}); return false;"><img src="/static/images/edit.png" class="editOption" /> </a></td>
<script>var val = $("#edit_dbh").html(); if (val) { val = parseFloat(val); } if (val) { $("#edit_dbh").html(val.toFixed(1)) };</script> <script>var val = $("#edit_dbh").html(); if (val) { val = parseFloat(val); } if (val) { $("#edit_dbh").html(val.toFixed(1)) };</script>
</tr> </tr>
Expand Down Expand Up @@ -299,6 +299,12 @@ <h2 id="pageHead">
{% endif %} {% endif %}
</li> </li>
</ul> </ul>
{% if perms.can_edit_condition %}
<ul id="message">
<li id="title"><a href="javascript:void(0)" onclick="tm.deleteTree({{ instance.id }});return false;" >Delete this tree</a></li>
<li>Some warning text here about not undoing and lag time on map tiles</li>
</ul>
{% endif %}
{% comment %} {% comment %}
<ul id="message"> <ul id="message">
<li id="title"><a href="#">Flag this tree</a></li> <li id="title"><a href="#">Flag this tree</a></li>
Expand Down
2 changes: 1 addition & 1 deletion treemap/models.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ class Species(models.Model):


#tree_count should always be set on tree update.. #tree_count should always be set on tree update..
def save(self,*args,**kwargs): def save(self,*args,**kwargs):
self.tree_count = self.tree_set.all().count() self.tree_count = self.tree_set.filter(present=True).count()
if not self.scientific_name: if not self.scientific_name:
if self.genus and self.species: if self.genus and self.species:
self.scientific_name = '%s %s' % (self.genus, self.species) self.scientific_name = '%s %s' % (self.genus, self.species)
Expand Down
1 change: 1 addition & 0 deletions treemap/urls.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
url(r'^trees/(?P<tree_id>\d+)/edit/$', tree_edit, name="treemap_tree_edit"), url(r'^trees/(?P<tree_id>\d+)/edit/$', tree_edit, name="treemap_tree_edit"),
url(r'^trees/(?P<tree_id>\d+)/photos/$', tree_add_edit_photos, name="treemap_add_edit_photos"), url(r'^trees/(?P<tree_id>\d+)/photos/$', tree_add_edit_photos, name="treemap_add_edit_photos"),
(r'^trees/(?P<tree_id>\d+)/edit/choices/(?P<type_>[a-z_]*)/$', tree_edit_choices), (r'^trees/(?P<tree_id>\d+)/edit/choices/(?P<type_>[a-z_]*)/$', tree_edit_choices),
(r'^trees/(?P<tree_id>\d+)/delete/$', tree_delete),
(r'^trees/(?P<tree_id>\d+)/ecosystem/$', trees), (r'^trees/(?P<tree_id>\d+)/ecosystem/$', trees),
url(r'^trees/(?P<tree_id>\d+)/$', trees, name="treemap_tree_detail"), url(r'^trees/(?P<tree_id>\d+)/$', trees, name="treemap_tree_detail"),
(r'^trees/location/$', tree_location_search), (r'^trees/location/$', tree_location_search),
Expand Down
71 changes: 48 additions & 23 deletions treemap/views.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -97,20 +97,28 @@ def result_map(request):


# get enviro attributes for 'selected' trees # get enviro attributes for 'selected' trees
min_year = 1970 min_year = 1970
planted_trees = Tree.objects.exclude(date_planted=None).order_by("date_planted") planted_trees = Tree.objects.exclude(date_planted=None).exclude(present=False).order_by("date_planted")
if planted_trees.count(): if planted_trees.count():
min_year = Tree.objects.exclude(date_planted=None).order_by("date_planted")[0].date_planted.year min_year = Tree.objects.exclude(date_planted=None).exclude(present=False).order_by("date_planted")[0].date_planted.year
current_year = datetime.now().year current_year = datetime.now().year


# TODO: Fix this to include updates to treestatus and treeflag objects
min_updated = 0 min_updated = 0
max_updated = 0 max_updated = 0
updated = Tree.objects.exclude(last_updated=None).order_by("last_updated") updated = Tree.objects.exclude(last_updated=None, present=False).order_by("last_updated")
if updated.exists(): if updated.exists():
min_updated = mktime(updated[0].last_updated.timetuple()) min_updated = mktime(updated[0].last_updated.timetuple())
max_updated = mktime(updated[updated.count()-1].last_updated.timetuple()) max_updated = mktime(updated[updated.count()-1].last_updated.timetuple())


latest_trees = Tree.objects.order_by("-last_updated")[0:3] recent_trees = Tree.objects.filter(present=True).order_by("-last_updated")[0:3]
latest_photos = TreePhoto.objects.order_by("-reported")[0:8] recent_status = TreeStatus.objects.filter(tree__present=True).order_by("-reported")[0:3]
recent_flags = TreeFlags.objects.filter(tree__present=True).order_by("-reported")[0:3]

recent_edits = unified_history(recent_trees, recent_status, recent_flags)

#TODO return the recent_edits instead
latest_trees = Tree.objects.filter(present=True).order_by("-last_updated")[0:3]
latest_photos = TreePhoto.objects.exclude(tree__present=False).order_by("-reported")[0:8]


return render_to_response('treemap/results.html',RequestContext(request,{ return render_to_response('treemap/results.html',RequestContext(request,{
#'top_species' : top_species, #'top_species' : top_species,
Expand Down Expand Up @@ -247,27 +255,22 @@ def favorites(request, username):
return render_to_json(js) return render_to_json(js)


def trees(request, tree_id=''): def trees(request, tree_id=''):
trees = Tree.objects.all() trees = Tree.objects.filter(present=True)
# testing - to match what you get in /location query and in map tiles. # testing - to match what you get in /location query and in map tiles.
favorite = False favorite = False
recent_edits = []
if tree_id: if tree_id:
trees = trees.filter(pk=tree_id) trees = trees.filter(pk=tree_id)


if not trees.exists():
raise Http404

# get the last 5 edits to each tree piece # get the last 5 edits to each tree piece
history = trees[0].history.order_by('-last_updated')[:5] history = trees[0].history.order_by('-last_updated')[:5]
status = TreeStatus.history.all().filter(tree=trees).order_by('-reported')[:5] status = TreeStatus.history.all().filter(tree=trees).order_by('-reported')[:5]
flags = TreeFlags.history.all().filter(tree=trees).order_by('-reported')[:5] flags = TreeFlags.history.all().filter(tree=trees).order_by('-reported')[:5]


# make a unified history recent_edits = unified_history(history, status, flags)
recent_edits = []
for h in history:
recent_edits.append((h.last_updated_by.username, h.last_updated))
for s in status:
recent_edits.append((s.reported_by, s.reported))
for f in flags:
recent_edits.append((f.reported_by, f.reported))
# sort by the date descending
recent_edits = sorted(recent_edits, key=itemgetter(1), reverse=True)


if request.user.is_authenticated(): if request.user.is_authenticated():
favorite = TreeFavorite.objects.filter(user=request.user, favorite = TreeFavorite.objects.filter(user=request.user,
Expand All @@ -289,7 +292,19 @@ def trees(request, tree_id=''):
return render_to_response('treemap/tree_detail_eco_infowindow.html',RequestContext(request,{'tree':first})) return render_to_response('treemap/tree_detail_eco_infowindow.html',RequestContext(request,{'tree':first}))
else: else:
return render_to_response('treemap/tree_detail.html',RequestContext(request,{'favorite': favorite, 'tree':first, 'recent': recent_edits})) return render_to_response('treemap/tree_detail.html',RequestContext(request,{'favorite': favorite, 'tree':first, 'recent': recent_edits}))



def unified_history(trees, status, flags):
recent_edits = []
for t in trees:
recent_edits.append((t.last_updated_by.username, t.last_updated))
for s in status:
recent_edits.append((s.reported_by, s.reported))
for f in flags:
recent_edits.append((f.reported_by, f.reported))
# sort by the date descending
return sorted(recent_edits, key=itemgetter(1), reverse=True)

@login_required @login_required
def tree_edit_choices(request, tree_id, type_): def tree_edit_choices(request, tree_id, type_):
tree = get_object_or_404(Tree, pk=tree_id) tree = get_object_or_404(Tree, pk=tree_id)
Expand Down Expand Up @@ -354,7 +369,7 @@ def batch_edit(request):
@login_required @login_required
def tree_edit(request, tree_id = ''): def tree_edit(request, tree_id = ''):


tree = get_object_or_404(Tree, pk=tree_id) tree = get_object_or_404(Tree, pk=tree_id, present=True)
#if not request.user in (tree.data_owner, tree.tree_owner) and not request.user.is_superuser: #if not request.user in (tree.data_owner, tree.tree_owner) and not request.user.is_superuser:
# return render_to_response("not_allowed.html", {'user' : request.user, "error_message":"You are not the owner of this tree."}) # return render_to_response("not_allowed.html", {'user' : request.user, "error_message":"You are not the owner of this tree."})


Expand Down Expand Up @@ -506,6 +521,16 @@ def tree_edit(request, tree_id = ''):


return render_to_response('treemap/tree_edit.html',RequestContext(request,{ 'instance': tree, 'data': data})) return render_to_response('treemap/tree_edit.html',RequestContext(request,{ 'instance': tree, 'data': data}))


def tree_delete(request, tree_id):
tree = Tree.objects.get(pk=tree_id)
tree.present = False
tree.save()

return HttpResponse(
simplejson.dumps({'success':True}, sort_keys=True, indent=4),
content_type = 'text/plain'
)

from django.contrib.auth.decorators import permission_required from django.contrib.auth.decorators import permission_required


@login_required @login_required
Expand Down Expand Up @@ -891,9 +916,9 @@ def _build_tree_search_result(request):


cur_species_count = species.count() cur_species_count = species.count()
if max_species_count == cur_species_count: if max_species_count == cur_species_count:
trees = Tree.objects.filter(Q(geocoded_accuracy__gte=7)|Q(geocoded_accuracy=None)|(Q(geocoded_accuracy=-1) & Q(owner_geometry__isnull=False)) ) trees = Tree.objects.filter(present=True).filter(Q(geocoded_accuracy__gte=7)|Q(geocoded_accuracy=None)|(Q(geocoded_accuracy=-1) & Q(owner_geometry__isnull=False)) )
else: else:
trees = Tree.objects.filter(species__in=species).filter(Q(geocoded_accuracy__gte=7)|Q(geocoded_accuracy=None)) trees = Tree.objects.filter(species__in=species, present=True).filter(Q(geocoded_accuracy__gte=7)|Q(geocoded_accuracy=None))
#filter by nhbd or zipcode if location was specified #filter by nhbd or zipcode if location was specified


geog_obj = None geog_obj = None
Expand Down Expand Up @@ -1217,8 +1242,8 @@ def clean_key_names(jsonstr):
u = User.objects.filter(username__icontains=request.GET['username']) u = User.objects.filter(username__icontains=request.GET['username'])
trees = trees.filter(last_updated_by__in=u) trees = trees.filter(last_updated_by__in=u)
newtrees = newtrees.filter(last_updated_by__in=u) newtrees = newtrees.filter(last_updated_by__in=u)
treestatus = treestatus.filter(tree__last_updated_by__in=u) treestatus = treestatus.filter(reported_by__in=u)
treeflags = treeflags.filter(tree__last_updated_by__in=u) treeflags = treeflags.filter(reported_by__in=u)
if 'address' in request.GET: if 'address' in request.GET:
trees = trees.filter(address_street__icontains=request.GET['address']) trees = trees.filter(address_street__icontains=request.GET['address'])
newtrees = newtrees.filter(address_street__icontains=request.GET['address']) newtrees = newtrees.filter(address_street__icontains=request.GET['address'])
Expand Down

0 comments on commit c1c9847

Please sign in to comment.