aboutsummaryrefslogtreecommitdiff
path: root/lib/d2/app/controllers/index.py
blob: 1c6ae66999abdbeec9859af476463502430ec7c3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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
from d2.app.adapters.plot import PlotAdapter

class IndexController(BaseController):

    @property
    def _search(self):
        if not hasattr(self, '_search_') or not self._search_:
            self._search_ = SearchAdapter.load()
        return self._search_

    @property
    def _forge(self):
        if not hasattr(self, '_forge_') or not self._forge_:
            self._forge_ = ForgeAdapter.load()
        return self._forge_

    @property
    def _detail(self):
        if not hasattr(self, '_detail_') or not self._detail_:
            self._detail_ = DetailAdapter.load()
        return self._detail_

    @property
    def _plot(self):
        if not hasattr(self, '_plot_') or not self._plot_:
            self._plot_ = PlotAdapter.load()
        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)

        self.render('index.html', 
                mode="map", 
                details=details,
                plots=plots,
                forges=forges)


    def get(self, type_1=None, id_1=None, type_2=None, id_2=None):
        if type_1 and id_1:
            if type_2 and id_2:
                occupant_id = id_1
                plot_id = id_2
            else:
                if type_1 == 'occupant':
                    occupant_id = id_1
                    plot_id = None
                else:
                    plot_id = id_1
                    occupant_id = None

            self._show_map(occupant_id, plot_id)
        else:
            self.render('index.html', mode="get")
        

    def post(self):
        name = self.get_argument('name')
        details = self._search.search(name)

        if len(details) == 1:
            self._show_map(details[0].occupant_id,
                           details[0].plot_id)
        else:
            self.render('index.html', mode='post',
                   details=details)