Ejemplo n.º 1
0
 def guess_delivery_date(self, error_collector: ErrorCollector):
     date_like = re.search("""\d+[/\-]\d+""", self.comments)
     if date_like is not None:
         return utils.parse_date(date_like.group(0), error_collector)
     else:
         return self.notification_date + timedelta(
             days=NUM_FUTURE_DAYS_GUESS)
Ejemplo n.º 2
0
    def load_from_request(cls, request) -> "StandardRequestParams":
        if request.GET:
            params = request.GET
        else:
            params = request.POST

        start_date = params.get("start_date")
        end_date = params.get("end_date")

        err_collector = ErrorCollector()
        # Python defaults to Monday. Subtract one extra day to get us to Sunday
        default_start = datetime.today() + timedelta(weeks=1) - timedelta(
            days=datetime.today().weekday() + 1)
        default_end = default_start + timedelta(days=6)
        start_date = (parse_date(start_date, err_collector)
                      or default_start).date()
        end_date = (parse_date(end_date, err_collector) or default_end).date()

        if params.get("rollup") in {"mayoral", "", None}:
            rollup_fn = mayoral_rollup
        else:
            rollup_fn = lambda x: x

        if params.get("supply"):
            supply_components = {
                AggColumn(col)
                for col in params.get("supply").split(",")
            }
        else:
            supply_components = AggColumn.all()

        print(f"Parsed request as {start_date}->{end_date}")
        if len(err_collector) > 0:
            err_collector.dump()
        return StandardRequestParams(
            start_date=start_date,
            end_date=end_date,
            rollup_fn=rollup_fn,
            supply_components=supply_components,
        )
Ejemplo n.º 3
0
 def __call__(self, names):
     opts = [name for name in names if re.match(self.patt, name)]
     if len(opts) == 1:
         return opts[0]
     if len(opts) > 1 and self.take_latest:
         date_strs = [
             (opt, re.search(self.patt, opt).group(1)) for opt in opts
         ]  # [(opt, ('4-23',)), ...]
         parsed_dates = [
             (opt, parse_date(date_str, ErrorCollector()))
             for (opt, date_str) in date_strs
         ]
         sorted_by_date = sorted(parsed_dates, key=lambda opt_date: opt_date[1])
         # We want to highest date
         print(sorted_by_date)
         return sorted_by_date[-1][0]
     return None
Ejemplo n.º 4
0
    def to_objects(self, error_collector: ErrorCollector):
        errors = self.sanity()
        if errors:
            error_collector.report_error(
                f"Refusing to generate a data model for: {self}. Errors: {errors}"
            )
            return []
        purchase = models.Purchase(
            item=self.item,
            quantity=self.quantity,
            vendor=self.vendor,
            raw_data=self.raw_data,
            order_type=OrderType.Make,
        )
        dates = []
        if self.delivery_date:
            dates.append(self.delivery_date)
        elif "weekly" in self.raw_date:
            import re

            date_str = re.sub(r"[a-zA-Z ]+", "", self.raw_date).strip()
            end_date = parse_date(date_str, error_collector)
            if end_date:
                dates = []
                while end_date > datetime.today():
                    dates.append(end_date)
                    end_date -= timedelta(weeks=1)

        delivery = [
            models.ScheduledDelivery(
                purchase=purchase, delivery_date=date, quantity=self.quantity,
            )
            for date in dates
        ]

        return [purchase, *delivery]
Ejemplo n.º 5
0
def date_or_pending(s: str, error_collector: ErrorCollector):
    if isinstance(s, str) and "pending" in s.lower():
        return None
    else:
        utils.parse_date(s, error_collector)