Ejemplo n.º 1
0
    def get(self):
        loc = None
        service_index = None
        logs = []
        if self.request.get("id"):
            loc_id = int(self.request.get("id"))
            loc = Location.get_by_id(loc_id)

            if self.request.get("service_id"):
                service_index = loc.service_index(int(self.request.get("service_id")))

            logs = list(self.backend.find(datamodel.LogItem, log_for=ndb.Key(Location, loc_id)).get_result())
            logs.sort(key=lambda i: i.timestamp, reverse=True)

        all_locs = Location.query().order(Location.name)

        self.write_template(
            "form.html",
            {
                "all_locs": all_locs,
                "loc": loc,
                "service_index": service_index,
                "logs": logs,
                "redirect_to": self.request.get("r"),
            },
        )
Ejemplo n.º 2
0
    def post(self):
        if "save" in self.request.params:
            if self.request.get("id"):
                loc, service = self.update(int(self.request.get("id")))
            else:
                loc, service = self.create()

            redirect_url = self.request.get("r")
            if not redirect_url:
                redirect_url = "/edit?id=%s" % loc.key.id()
                if service:
                    redirect_url = "%s&service_id=%s" % (redirect_url, service.service_id)

            self.redirect(redirect_url)
        else:
            all_locs = Location.query().order(Location.name)

            if self.request.get("id"):
                loc_id = int(self.request.get("id"))
                loc = Location.get_by_id(loc_id)
                logs = list(self.backend.find(datamodel.LogItem, log_for=ndb.Key(Location, loc_id)).get_result())
                logs.sort(key=lambda i: i.timestamp, reverse=True)
            else:
                loc = Location()
                logs = []
            service = self.populate(loc)

            service_index = None
            if service:
                for i, s in enumerate(loc.services):
                    if s is service:
                        service_index = i

            removed_services = [int(s) for s in self.request.get("removed_services").split()]

            if "add_service" in self.request.params:
                service = Service()
                loc.services.append(service)
                service_index = len(loc.services) - 1
            if "remove_service" in self.request.params:
                assert service
                removed_services.append(service_index)
            if "restore_service" in self.request.params:
                assert service
                removed_services.remove(service_index)
            elif "add_time" in self.request.params:
                assert service
                params = self.request.get("add_time").split(",")
                day = DAY_NAMES.index(params[0])
                assert 0 <= day < 7, day
                new_time = models.ServiceTime(day=day)

                if len(params) > 1:
                    prototype = service.times[int(params[1])]
                elif service.times:
                    prototype = service.times[-1]
                else:
                    prototype = None
                service.times.append(
                    models.ServiceTime(
                        day=day, start=prototype and prototype.start or None, end=prototype and prototype.end or None
                    )
                )
            elif "remove_time" in self.request.params:
                assert service
                index = int(self.request.get("remove_time"))
                del service.times[index]
            elif "add_not_date" in self.request.params:
                assert service
                service.not_dates.append(models.ServiceNotDate())
            elif "remove_not_date" in self.request.params:
                assert service
                index = int(self.request.get("remove_not_date"))
                del service.not_dates[index]
            elif "add_phone" in self.request.params:
                loc.phone = models.ServicePhone()
            elif "remove_phone" in self.request.params:
                loc.phone = None
            elif "add_log" in self.request.params:
                log = datamodel.LogItem(
                    type=datamodel.LogItemType.NOTE,
                    note=datamodel.LogItem.Storage.Note(
                        text=self.request.get("log_message"), category=self.request.get("log_category")
                    ),
                )
                self.backend.save(log, log_for=loc.key)
                logs.insert(0, log)

            self.write_template(
                "form.html",
                {
                    "all_locs": all_locs,
                    "loc": loc,
                    "service_index": service_index,
                    "removed_services": removed_services,
                    "logs": logs,
                    "redirect_to": self.request.get("r"),
                    "add_log": "begin_log" in self.request.params,
                },
            )
Ejemplo n.º 3
0
 def update(self, loc_id):
     loc = Location.get_by_id(loc_id)
     service = self.populate(loc, for_put=True)
     loc.put()
     return loc, service
Ejemplo n.º 4
0
 def create(self):
     loc = Location()
     service = self.populate(loc, for_put=True)
     loc.put()
     return loc, service