Ejemplo n.º 1
0
    def get(self):
        d = dt.datetime.today()
        start, end = get_dates_month_period(d)

        period = {
            "start_timestamp": start.timestamp(),
            "start": start.isoformat(),
            "end": end.isoformat(),
            "end_timestamp": end.timestamp(),
            "year": end.year
        }
        return period, 200
Ejemplo n.º 2
0
    def resolve_periods(self, info, year=None, month=None):
        token = get_token_from_info(info, require_token=not dev_user
                                    ) or user_client.get_valid_token(dev_user)

        succ, username = user_client.get_username_from_token(token)
        if not succ:
            raise Exception(f"User {username} does not exist")

        if user_client.token_has_privileges(token, ["periods", "economy"]):
            if not year and not month:
                s, e = get_dates_month_period(datetime.date.today())
                return [
                    Period(year=e.year,
                           month=e.month,
                           start=s.isoformat(),
                           end=e.isoformat(),
                           start_timestamp=s.timestamp(),
                           end_timestamp=e.timestamp())
                ]

            start_ends = [(y, m,
                           *get_dates_month_period(datetime.date(y, m, 1)))
                          for y in year for m in month]
            return sorted([
                Period(year=y,
                       month=m,
                       start=s.isoformat(),
                       end=e.isoformat(),
                       start_timestamp=s.timestamp(),
                       end_timestamp=e.timestamp())
                for y, m, s, e in start_ends
            ],
                          key=lambda p: p.start_timestamp)
        else:
            raise Exception(
                "User must have privileges 'periods' and 'economy'")
Ejemplo n.º 3
0
 def get(self):
     args = get_parser.parse_args()
     if not args["year"] and not args["month"]:
         return {"error": "Must specify at least one month."}, 400
     if not args["year"]:
         args["year"] = [dt.datetime.now().year]
     if not args["month"]:
         args["month"] = [dt.datetime.now().month]
     periods = list()
     for y in args["year"]:
         for m in args["month"]:
             start, end = get_dates_month_period(dt.date(y, m, 1))
             periods.append({
                 "year": y,
                 "month": m,
                 "start": start.isoformat(),
                 "start_timestamp": start.timestamp(),
                 "end": end.isoformat(),
                 "end_timestamp": end.timestamp()
             })
     return periods, 200
Ejemplo n.º 4
0
def test_17th_january():
    date = dt.datetime(2020, 1, 17)
    start = dt.datetime(2019, 12, 25, 0, 0, 0)
    end = dt.datetime(2020, 1, 24, 23, 59, 59)
    assert get_dates_month_period(date) == (
        start, end), "17th january got wrong period"
Ejemplo n.º 5
0
def test_31st_october():
    date = dt.datetime(2021, 10, 31)
    start = dt.datetime(2021, 10, 25, 0, 0, 0)
    end = dt.datetime(2021, 11, 24, 23, 59, 59)
    assert get_dates_month_period(date) == (start,
                                            end), "31 october got wrong period"
Ejemplo n.º 6
0
def test_4th_april():
    date = dt.datetime(2021, 4, 4)
    start = dt.datetime(2021, 3, 25, 0, 0, 0)
    end = dt.datetime(2021, 4, 24, 23, 59, 59)
    assert get_dates_month_period(date) == (start,
                                            end), "4th april got wrong period"
Ejemplo n.º 7
0
def test_26th_december():
    date = dt.datetime(2019, 12, 26)
    start = dt.datetime(2019, 12, 25, 0, 0, 0)
    end = dt.datetime(2020, 1, 24, 23, 59, 59)
    assert get_dates_month_period(date) == (
        start, end), "26th december got wrong period"