aboutsummaryrefslogtreecommitdiff
path: root/lib/d2/app/adapters/forge.py
blob: 792725fa354c5e9cec740e08a428a4313dc26694 (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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
from d2.app.model.static import StaticData
from d2.app.adapters import BaseAdapter
from d2.config import Config
from d2.db import Forge
from d2.db import OccupantType
from datetime import datetime
from sqlalchemy import or_

class ForgeAdapter(BaseAdapter):
    """
    1. There should always be an empty_occupant record for each plot.
    2. We will not mark a plot as empty unless there are no occupants in the plot
    """

    def __init__(self, db, log, static, forge):
        self._db = db
        self._log = log
        self._static = static
        self._forge = forge

    @classmethod
    def load(cls, config=None, static=None):
        config = config or Config.load()
        static = static or StaticData.load(config.db)()
        return cls(config.db, config.log, static, Forge)

    def get(self, id):
        return self._db.session.query(self._forge).filter(
                "id=:id").param(id=id).first()

    def add_empty_user(self, plot_id):
        """ putting an empty user to a given plot
        """
        empty = dict(occupant_id = self._static.occupant.empty,
                     plot_id = plot_id,
                     date_start = datetime.now(),
                     date_end = None,
                     date_created = datetime.now(),
                     date_override = None)
        obj_empty = Forge.from_dict(empty)
        self._db.session.add(obj_empty)
        self._db.session.commit()

    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,
                                    self._static.occupant.empty, date_start)
            self._set_override(empty_record)
            self._set_end(empty_record, date_end)

        date_start= self.build_date(date_start)
        data = dict(
                plot_id=plot_id,
                occupant_id=occupant_id,
                date_start=date_start,
                date_end=date_end,
                date_created=datetime.now(),
                date_override=None
                )
        obj = self._forge.from_dict(data)
        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,
                            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
        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):
        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)
        return records

    def get_all_by_plot_ids(self, plot_ids, date_start=None):
        date_start = self.build_date(date_start)
        records = self._fetch_by_plot_ids(plot_ids, date_start)
        return records

    def _set_override(self, forge_obj):
        forge_obj.date_override = datetime.now()
        self._db.session.commit()

    def _set_end(self, forge_obj, date_end=None):
        date_end = self.build_date(date_end)
        data = forge_obj.as_dict()
        del(data['id'])
        data['date_end'] = date_end
        data['date_created'] = datetime.now()
        data['date_override'] = None
        obj = self._forge.from_dict(data)
        self._db.session.add(obj)
        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
        """
        is_empty = True
        records = self._fetch_forge_by_plot_id(plot_id, date_start)

        if not records:
            raise Exception("plot_id {0} does not exist".format(plot_id))

        if len(records) > 1:
            is_empty = False
        else:
            if records[0].occupant_id != self._static.occupant.empty:
                is_empty = False

        return is_empty

    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
            ).filter(self._forge.plot_id==plot_id
            ).filter(self._forge.occupant_id==occupant_id
            ).filter(self._forge.date_override==None
            ).filter(self._forge.date_start<=date_start
            ).filter(or_(self._forge.date_end>date_start,
                self._forge.date_end==None)
        ).first()

    def _fetch_forge_by_plot_id(self, plot_id, date_start):
        date_start = self.build_date(date_start)
        return self._db.session.query(self._forge
            ).filter(self._forge.plot_id==plot_id
            ).filter(self._forge.date_override==None
            ).filter(self._forge.date_start<=date_start
            ).filter(or_(self._forge.date_end>date_start,
                self._forge.date_end==None)
        ).all()

    def _fetch_forge_by_occupant_id(self, occupant_id, date_start):
        date_start = self.build_date(date_start)
        return self._db.session.query(self._forge
            ).filter(self._forge.occupant_id==occupant_id
            ).filter(self._forge.date_override==None
            ).filter(self._forge.date_start<=date_start
            ).filter(or_(self._forge.date_end>date_start,
                self._forge.date_end==None)
        ).all()

    def _fetch_all(self, date_start):
        date_start = self.build_date(date_start)
        return self._db.session.query(self._forge
            ).filter(self._forge.date_override==None
            ).filter(self._forge.date_start<=date_start
            ).filter(or_(self._forge.date_end>date_start,
                self._forge.date_end==None)
        ).all()

    def _fetch_by_plot_ids(self, plot_ids, date_start):
        date_start = self.build_date(date_start)
        return self._db.session.query(self._forge
            ).filter(self._forge.plot_id.in_(plot_ids)
            ).filter(self._forge.date_override==None
            ).filter(self._forge.date_start<=date_start
            ).filter(or_(self._forge.date_end>date_start,
                self._forge.date_end==None)
        ).all()