Skip to content

Commit

Permalink
Fixed Map Model, forms, crud handlers;
Browse files Browse the repository at this point in the history
Also removed obsolete routes
  • Loading branch information
fccoelho committed Jun 7, 2013
1 parent 23286e4 commit 180925d
Show file tree
Hide file tree
Showing 7 changed files with 152 additions and 195 deletions.
5 changes: 5 additions & 0 deletions justice/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,9 @@ class SimulationForm(BaseForm):
spread = fields.FileField(_('Spread tree file'),[validators.Optional()])
model = fields.TextAreaField(_('Model Source'),[validators.Length(min=10,max=10000)])

class MapForm(BaseForm):
name = fields.StringField(_('Name'),[validators.Required(), validators.Length(min=3, max=FIELD_MAXLENGTH), validators.regexp(utils.ALPHANUMERIC_REGEXP, message=_('Invalid name.'))])

description = fields.TextAreaField(_('Description'),[validators.Optional(), validators.Length(min=10,max=512)])
search_term = fields.StringField(_('Search Expression'))

155 changes: 65 additions & 90 deletions justice/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1024,132 +1024,107 @@ def form(self):

class LibraryHandler(BaseHandler):
"""
Handler for simulation library
Handler for map library
"""

def get(self):
"""
Returns a list of available simulations
"""
q = models.Map.query().order(models.Map.name,-models.Map.date_created)
sims = q.fetch()
maps = q.fetch()
# q2 = ndb.gql("select name, description, creator, date_created from Map")
# sims = q2.fetch()
# maps = q2.fetch()
params = {
'sims': sims,
'maps': maps,
}

return self.render_template('maps.html',**params)


class SimulationHandler(BaseHandler):
class MapHandler(BaseHandler):
"""
Handler for simulation viewing
Handler for Map viewing
"""

def get(self, simulation_id):
@user_required
def get(self, map_id):
"""
Returns a simulation object
Returns a map object
"""
user_info = models.User.get_by_id(long(self.user_id))
if simulation_id == "new": # Create a new Simulation entry
sim = models.Simulation(name=_("New Model"))
sim.owner = user_info.key
sim.put()
simulation_id = sim.key.id()
if map_id == "new": # Create a new Simulation entry
Mapa = models.Map(name=_("New Map"))
Mapa.creator = user_info.key
Mapa.search_term = ""
Mapa.put()
map_id = Mapa.key.id()
else:
# sim = memcache.get('simulation_%s'%simulation_id)
# if sim is None:
sim = models.Simulation.get_by_id(int(simulation_id))
memcache.add('simulation_%s'%simulation_id, sim,60)
# Map = memcache.get('simulation_%s'%map_id)
# if Map is None:
Mapa = models.Map.get_by_id(int(map_id))
memcache.add('map_%s'%map_id, Mapa,60)

if self.user:
user_info = models.User.get_by_id(long(self.user_id))
self.form.name.data = sim.name
self.form.description.data = sim.description
self.form.model.data = sim.model
if sim.epg is not None:
epg = sim.epg.decode('unicode-escape').splitlines()
self.form.name.data = Mapa.name
self.form.description.data = Mapa.description
self.form.search_term.data = Mapa.search_term
if Mapa.search_term is not None:
search = Mapa.search_term.decode('unicode-escape')
else:
epg = []
search = ""
params = {
# 'simulation': s,
'sid': simulation_id,
'name': sim.name,
'epg': epg,
'model': sim.model,
'mid': map_id,
'name': Mapa.name,
'search': search,
'description': Mapa.description,
'form': self.form
}

return self.render_template('sim_view.html',**params)
return self.render_template('map_view.html',**params)

def post(self,simulation_id):
def post(self, map_id):
"""
Handle updates of the simulation record
"""
# if not self.form.validate():
# return self.get(simulation_id)
# return self.get(map_id)
user_info = models.User.get_by_id(long(self.user_id))
sim = models.Simulation.get_by_id(int(simulation_id))
Mapa = models.Map.get_by_id(int(map_id))

try:
map = self.request.POST.multi['map'].file.read()
search = self.request.POST.multi['search_term'].file.read()
except AttributeError:
map = False
try:
series = self.request.POST.multi['series'].file.read()
except AttributeError:
series = False
try:
epg = self.request.POST.multi['epg'].file.read()
except AttributeError:
epg = False
try:
network = self.request.POST.multi['network'].file.read()
except AttributeError:
network = False
try:
spread = self.request.POST.multi['spread'].file.read()
except AttributeError:
spread = False
search = False

# sim.owner = user_info.key
if self.form.name.data:
sim.name = self.form.name.data
Mapa.name = self.form.name.data
if self.form.description.data:
sim.description = self.form.description.data
# sim.put()
if map:
sim.map = map
if epg:
sim.epg = epg
Mapa.description = self.form.description.data
# sim.put()
if series:
sim.series = series
if network:
sim.network = network
if spread:
sim.spread = spread
if self.form.model.data:
sim.model = self.form.model.data
sim.put()
if search:
Mapa.search_term = search
Mapa.put()

self.redirect_to('sim-view',simulation_id=simulation_id)
self.redirect_to('map-view', map_id=map_id)

@webapp2.cached_property
def form(self):
return forms.SimulationForm(self)
return forms.MapForm(self)

class SimulationDelete(BaseHandler):

class MapDelete(BaseHandler):
"""
handles the deletion of simulations
handles the deletion of mapsS
"""
def get(self,simulation_id):
def get(self, map_id):
user_info = models.User.get_by_id(long(self.user_id))
sim = models.Simulation.get_by_id(int(simulation_id))
sim = models.Map.get_by_id(int(map_id))
if sim.owner == user_info.key:
sim.key.delete()
self.redirect_to('simulations')
self.redirect_to('maps')

class SimulationMapHandler(BaseHandler):
"""
Expand Down Expand Up @@ -1189,30 +1164,30 @@ def get(self,simulation_id):

return self.render_template('sim_spread.html',**params)

class SimulationSeriesHandler(BaseHandler):
def get(self,simulation_id):
sim = memcache.get('simulation_%s'%simulation_id)
if sim is None:
sim = models.Simulation.get_by_id(int(simulation_id))
memcache.add('simulation_%s'%simulation_id, sim,60)
series = json.loads(sim.series,object_hook=convert)
labels = ['x'] + series.keys() #localities
class MapDataHandler(BaseHandler):
def get(self, map_id):
Mapa = memcache.get('map_%s'%map_id)
if Mapa is None:
Mapa = models.Model.get_by_id(int(map_id))
memcache.add('map_%s'%map_id, Mapa,60)
data = json.loads(Mapa.map,object_hook=convert)
labels = ['x'] + data.keys() #localities
# labels = [str(s) for s in labels] #removing unicode objects
vars = series.values()[0].keys() #variables
vars = data.values()[0].keys() #variables
# vars = [str(v) for v in vars]
n = len(series[labels[1]][vars[0]])
data = {}
n = len(data[labels[1]][vars[0]])
dataex = {}
for v in vars:
arr = [range(n)] + [series[l][v] for l in labels[1:]]
data[v] = [list(t) for t in zip(*arr)]
arr = [range(n)] + [data[l][v] for l in labels[1:]]
dataex[v] = [list(t) for t in zip(*arr)]
# print data[v]
params = {
'name':sim.name,
'id': simulation_id,
'name': Mapa.name,
'id': map_id,
'localities': labels,
'variables': vars,
'data': data,
'nplots':len(vars),
'data': dataex,
'nplots': len(vars),
}
return self.render_template('sim_series.html',**params)

Expand Down
3 changes: 2 additions & 1 deletion justice/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,8 @@ class Map(ndb.Model):
date_created = ndb.DateProperty(auto_now_add=True)
name = ndb.StringProperty(required=True)
description = ndb.TextProperty()
map = ndb.JsonProperty(compressed=True) # map in GeoJson Format
search_term = ndb.StringProperty(required=True) # Search expression filtering the data.
map = ndb.JsonProperty(compressed=True) # map in GeoJson Format


@classmethod
Expand Down
11 changes: 4 additions & 7 deletions justice/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,10 @@
RedirectRoute('/resend/<user_id>/<token>', handlers.ResendActivationEmailHandler, name='resend-account-activation', strict_slash=True),
RedirectRoute('/contact/', handlers.ContactHandler, name='contact', strict_slash=True),
RedirectRoute('/maps/', handlers.LibraryHandler, name='maps', strict_slash=True),
RedirectRoute('/simulations/delete/<simulation_id>/', handlers.SimulationDelete, name='sim-del', strict_slash=True),
RedirectRoute('/simulations/view/<simulation_id>/', handlers.SimulationHandler, name='sim-view', strict_slash=True),
RedirectRoute('/simulations/view/<simulation_id>/map/', handlers.SimulationMapHandler, name='sim-map', strict_slash=True),
RedirectRoute('/simulations/view/<simulation_id>/series/', handlers.SimulationSeriesHandler, name='sim-series', strict_slash=True),
RedirectRoute('/simulations/view/<simulation_id>/network/', handlers.SimulationNetHandler, name='sim-net', strict_slash=True),
RedirectRoute('/simulations/view/<simulation_id>/spread/', handlers.SimulationSpreadHandler, name='sim-spread', strict_slash=True),
RedirectRoute('/simulations/upload/',handlers.UploadHandler, name='carga', strict_slash=True),
RedirectRoute('/maps/delete/<map_id>/', handlers.MapDelete, name='map-del', strict_slash=True),
RedirectRoute('/maps/view/<map_id>/', handlers.MapHandler, name='map-view', strict_slash=True),
RedirectRoute('/maps/view/<map_id>/data/', handlers.MapDataHandler, name='map-data', strict_slash=True),
RedirectRoute('/simulations/upload/', handlers.UploadHandler, name='carga', strict_slash=True),
RedirectRoute('/settings/profile', handlers.EditProfileHandler, name='edit-profile', strict_slash=True),
RedirectRoute('/settings/password', handlers.EditPasswordHandler, name='edit-password', strict_slash=True),
RedirectRoute('/settings/email', handlers.EditEmailHandler, name='edit-email', strict_slash=True),
Expand Down
71 changes: 71 additions & 0 deletions justice/templates/map_view.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
{% extends base_layout %}

{% block title %}
{% trans %}Maps View{% endtrans %} - {{app_name}}
{% endblock %}
{% block header_title_lead %}
{% endblock %}
{% block content %}
<p class="lead">
{{ name }}
</p>
<p>
{%trans%}Navigate using the bar below{%endtrans%}
</p>
<div class="navbar">
<div class="navbar-inner">
<a class="brand" href="{{ uri_for("map-view",map_id=mid) }}">{%trans%}Simulation Details{%endtrans%}</a>
<ul class="nav">
<li {% if path == uri_for("map-data",map_id=mid) %}class="active" {% endif %}><a href="{{ uri_for("map-data",map_id=mid) }}">{%trans%}Series{%endtrans%}</a></li>

</ul>
</div>
</div>
<div class="well-large">
<ul class="nav nav-tabs">
<li><a href="#Record" data-toggle="tab">{% trans %}Record{% endtrans %}</a></li>
<li><a href="#EPG" data-toggle="tab">{% trans %}EPG file{% endtrans %}</a></li>
<li><a href="#Model" data-toggle="tab">{% trans %}Model{% endtrans %}</a></li>
<li><a href="#Statistics" data-toggle="tab">{% trans %}Statistics{% endtrans %}</a></li>
</ul>
<div class="tab-content">
<div class="tab-pane active" id="Record">
<form method="POST" action="{{ url|safe }}" enctype="multipart/form-data" class="well form-horizontal">
<fieldset>
<input type="hidden" name="_csrf_token" value="{{ csrf_token() }}">
<div>{{ form.name.label }}: {{ form.name(class="input-xlarge focused required") }}</div>
{% if form.name.errors %}
<ul class="errors">{% for error in form.name.errors %}<li>{{ error }}</li>{% endfor %}</ul>
{% endif %}

<div>{{ form.description.label }}: {{ form.description(class="input-xlarge focused required") }}</div>
{% if form.description.errors %}
<ul class="errors">{% for error in form.description.errors %}<li>{{ error }}</li>{% endfor %}</ul>
{% endif %}
<div>{{ form.search_term.label }}: {{ form.search_term(class="input-xlarge focused") }}</div>
{% if form.search_term.errors %}
<ul class="errors">{% for error in form.search_term.errors %}<li>{{ error }}</li>{% endfor %}</ul>
{% endif %}

<div class="form-actions">
<button type="submit" class="btn btn-primary">{% trans %}Update!{% endtrans %}</button>
</div>
</fieldset>
</form>
</div>
<div class="tab-pane" id="EPG">
<code>
{% for l in epg %}
<p>{{ l }}</p>
{% endfor %}
</code>
</div>
<div class="tab-pane" id="Model">
{{ model }}
</div>
<div class="tab-pane" id="Statistics">

</div>
</div>
</div>
{% endblock %}
10 changes: 5 additions & 5 deletions justice/templates/maps.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<div class="navbar-inner">
<a class="brand" href="#">{%trans%}Manage{%endtrans%}</a>
<ul class="nav">
<li {% if path == "/simulations/view/new/" %}class="active" {% endif %}><a href="/simulations/view/new/">{% trans %}Create new map{% endtrans %}</a></li>
<li {% if path == "/maps/view/new/" %}class="active" {% endif %}><a href="/maps/view/new/">{% trans %}Create new map{% endtrans %}</a></li>
<li> <form class="navbar-search pull-left">
<input type="text" class="search-query" placeholder="Search">
</form></li>
Expand All @@ -28,18 +28,18 @@
<tr><th>{%trans%}Name{%endtrans%}</th><th>{%trans%}created on{%endtrans%}</th><th>{%trans%}Description{%endtrans%}</th><th>{%trans%}Creator{%endtrans%}</th><th></th></tr>
</thead>
<tbody>
{%for s in sims %}
{%for m in maps %}
<tr>
<td><a href="{{ uri_for('sim-view',simulation_id=s.key.id()) }}">{{ s.name }}</a></td><td>{{ s.date_uploaded }}</td><td>{{ s.description }}</td><td>{{ s.owner.get().name }}</td>{% if user_id|int == s.owner.id() %}<td><i class="icon-trash" rel="tooltip" title="Delete simulation" onclick="delete_simulation({{ s.key.id() }})"></i></td>{% endif %}
<td><a href="{{ uri_for('map-view',map_id=m.key.id()) }}">{{ m.name }}</a></td><td>{{ m.date_created }}</td><td>{{ m.description }}</td><td>{{ m.creator.get().name }}</td>{% if user_id|int == m.creator.id() %}<td><i class="icon-trash" rel="tooltip" title="Delete map" onclick="delete_map({{ m.key.id() }})"></i></td>{% endif %}
</tr>
{% endfor %}
</tbody>
</table>
<script type="text/javascript">
function delete_simulation(id){
function delete_map(id){
$.ajax({
url: '/simulations/delete/'+id,
url: '/maps/delete/'+id,
type: 'GET',
success: function (result){}
});
Expand Down
Loading

0 comments on commit 180925d

Please sign in to comment.