From b0a67d8900e5ce40d491af2efe755881ae3d51f3 Mon Sep 17 00:00:00 2001 From: Six Date: Tue, 24 May 2011 21:59:35 -0400 Subject: bug fixes --- lib/d2/app/adapters/detail.py | 11 ++++++++--- lib/d2/app/adapters/forge.py | 38 +++++++++++++++++++++++++------------- lib/d2/app/adapters/plot.py | 9 +++++++-- lib/d2/app/controllers/index.py | 28 +++++++++++----------------- templates/index.html | 10 +++++----- 5 files changed, 56 insertions(+), 40 deletions(-) diff --git a/lib/d2/app/adapters/detail.py b/lib/d2/app/adapters/detail.py index 738230b..ba2906d 100644 --- a/lib/d2/app/adapters/detail.py +++ b/lib/d2/app/adapters/detail.py @@ -351,10 +351,11 @@ class DetailAdapter(BaseAdapter): def details(self, plot_id=None, occupant_id=None): """ Returns a named tuple with complete details. """ - plot_id, occupant_id = self._validate(plot_id, occupant_id) - plot = self.details_to_dict(self.fetch_all_plot_details(plot_id)) + (plot_id_, occupant_id_) = self._validate(plot_id, occupant_id) + + plot = self.details_to_dict(self.fetch_all_plot_details(plot_id_)) occupant = self.details_to_dict(self.fetch_all_occupant_details( - occupant_id)) + occupant_id_)) if plot and occupant: occupant[u'plot_id'] = plot[u'plot_id'] @@ -368,6 +369,7 @@ class DetailAdapter(BaseAdapter): for key, val in plot.items(): occupant[key] = val + return self._details_dict_to_tuple(occupant) if plot: @@ -376,6 +378,9 @@ class DetailAdapter(BaseAdapter): if occupant: return self._details_dict_to_tuple(occupant) + make_ = namedtuple("Detail", "label plot_id occupant_id") + return make_(None, None, None) + def _validate(self, plot_id, occupant_id, start_date=None): """Validates that the given plot_id and occupant_id are valid. Also checks forge diff --git a/lib/d2/app/adapters/forge.py b/lib/d2/app/adapters/forge.py index 3590d23..792725f 100644 --- a/lib/d2/app/adapters/forge.py +++ b/lib/d2/app/adapters/forge.py @@ -41,12 +41,12 @@ class ForgeAdapter(BaseAdapter): self._db.session.add(obj_empty) self._db.session.commit() - def add_occupant_to_plot(self, plot_id, occupant_id, + def add_occupant_to_plot(self, plot_id, occupant_id, date_start=None, date_end=None): """ add an occupant to a plot """ if self._is_empty_plot(plot_id, date_start): - empty_record = self._fetch_forge_by_plot_and_occupant_id(plot_id, + empty_record = self._fetch_forge_by_plot_and_occupant_id(plot_id, self._static.occupant.empty, date_start) self._set_override(empty_record) self._set_end(empty_record, date_end) @@ -64,30 +64,42 @@ class ForgeAdapter(BaseAdapter): self._db.session.add(obj) self._db.session.commit() return obj - + def remove_occupant_from_plot(self, plot_id, occupant_id, date_start=None, date_end=None): """ remove an occupant from a plot """ - forge_record = self._fetch_forge_by_plot_and_occupant_id(plot_id, + forge_record = self._fetch_forge_by_plot_and_occupant_id(plot_id, occupant_id, date_start) if forge_record: self._set_override(forge_record) self._set_end(forge_record, date_end) - # if no more occupants in plot_id, mark it as empty + # if no more occupants in plot_id, mark it as empty records = self._fetch_forge_by_plot_id(plot_id, date_start) if not records: self.add_empty_user(plot_id) - + def get_forge_by_occupant_id(self, occupant_id, date_start=None): - date_start = self.build_date(date_start) - return self._fetch_forge_by_occupant_id(occupant_id, date_start) - + if occupant_id: + date_start = self.build_date(date_start) + return self._fetch_forge_by_occupant_id(occupant_id, date_start) + return [] + def get_forge_by_plot_id(self, plot_id, date_start=None): date_start = self.build_date(date_start) return self._fetch_forge_by_plot_id(plot_id, date_start) - + + def get_occupant_other_plots(self, plot_id=None, occupant_id=None, + date_start=None): + out = [] + date_start = self.build_date(date_start) + results = self._fetch_forge_by_occupant_id(occupant_id, date_start) + for row in results: + if not row.plot_id == plot_id: + out.append(row) + return out + def get_all(self, date_start=None): date_start = self.build_date(date_start) records = self._fetch_all(date_start) @@ -114,8 +126,8 @@ class ForgeAdapter(BaseAdapter): self._db.session.commit() def _is_empty_plot(self, plot_id, date_start): - """ return True if and only if one record exists for the given plot_id - and the occupant_id in that record is empty user id + """ return True if and only if one record exists for the given plot_id + and the occupant_id in that record is empty user id """ is_empty = True records = self._fetch_forge_by_plot_id(plot_id, date_start) @@ -131,7 +143,7 @@ class ForgeAdapter(BaseAdapter): return is_empty - def _fetch_forge_by_plot_and_occupant_id(self, plot_id, + def _fetch_forge_by_plot_and_occupant_id(self, plot_id, occupant_id, date_start): date_start = self.build_date(date_start) return self._db.session.query(self._forge diff --git a/lib/d2/app/adapters/plot.py b/lib/d2/app/adapters/plot.py index 451f215..ec6075d 100644 --- a/lib/d2/app/adapters/plot.py +++ b/lib/d2/app/adapters/plot.py @@ -1,7 +1,7 @@ from d2.app.model.static import StaticData from d2.app.adapters import BaseAdapter from d2.config import Config -from d2.db import Plot +from d2.db import Plot class PlotAdapter(BaseAdapter): @@ -9,7 +9,7 @@ class PlotAdapter(BaseAdapter): self._db = db self._log = log self._static = static - self._plot = plot + self._plot = plot @classmethod def load(cls, config=None, static=None): @@ -20,3 +20,8 @@ class PlotAdapter(BaseAdapter): def get_all(self, plot_ids): return self._db.session.query(self._plot).filter( self._plot.id.in_(plot_ids)).all() + + + def get_all_from_forges(self, forges): + hold = [forge.plot_id for forge in forges] + return self.get_all(hold) diff --git a/lib/d2/app/controllers/index.py b/lib/d2/app/controllers/index.py index 1c6ae66..f8756c2 100644 --- a/lib/d2/app/controllers/index.py +++ b/lib/d2/app/controllers/index.py @@ -1,4 +1,4 @@ -from d2.app.controllers import BaseController +from d2.app.controllers import BaseController from d2.app.adapters.search import SearchAdapter from d2.app.adapters.forge import ForgeAdapter from d2.app.adapters.detail import DetailAdapter @@ -31,24 +31,19 @@ class IndexController(BaseController): return self._plot_ def _show_map(self, occupant_id, plot_id): - if plot_id: - forges = self._forge.get_forge_by_plot_id(plot_id) - details = self._detail.details(plot_id, occupant_id) - plots = self._plot.get_all([plot_id]) - else: - forges = self._forge.get_forge_by_occupant_id(occupant_id) - details = self._detail.details(plot_id, occupant_id) - plots = [] - if len(forges) > 0: - plot_ids = [forge.plot_id for forge in forges] - plots = self._plot.get_all(plot_ids) + details = self._detail.details(plot_id, occupant_id) + forges = self._forge.get_forge_by_occupant_id(details.occupant_id) + plots = self._plot.get_all_from_forges(forges) + center_plot = None + for plot in plots: + if plot.id == details.plot_id: + center_plot=plot - self.render('index.html', - mode="map", + self.render('index.html', + mode="map", details=details, plots=plots, - forges=forges) - + center_plot=center_plot) def get(self, type_1=None, id_1=None, type_2=None, id_2=None): if type_1 and id_1: @@ -66,7 +61,6 @@ class IndexController(BaseController): self._show_map(occupant_id, plot_id) else: self.render('index.html', mode="get") - def post(self): name = self.get_argument('name') diff --git a/templates/index.html b/templates/index.html index 9a504a5..5cfefb2 100644 --- a/templates/index.html +++ b/templates/index.html @@ -24,8 +24,8 @@ var map_width = {{plots[0].map.width}}; var map_height = {{plots[0].map.height}}; //These are the coordinates from bottom left - var center_point_x = {{plots[0].x}}; - var center_point_y = {{plots[0].y}}; + var center_point_x = {{center_plot.x}}; + var center_point_y = {{center_plot.y}}; //Initialise the 'map' object function init() @@ -77,8 +77,8 @@ function emphasize() { - {% if (len(forges) > 0) %} - var e = document.getElementById("{{forges[0].occupant_id}}_{{forges[0].plot_id}}"); + {% if details %} + var e = document.getElementById("{{details.occupant_id}}_{{details.plot_id}}"); if (e) { e.setAttributeNS(null, "fill", "red"); @@ -147,7 +147,7 @@ {% else %}
- Sorry, no match found. Please try to broaden your search by entering less in t he search field above. + Sorry, no match found. Please try to broaden your search by entering more in the search field above.
{% end %} {% else %} -- cgit v1.2.3