Exemplo n.º 1
0
    def create_worker(self, request, *args, **kws):
        o = self.dbobject()
        tz = args[0]
        adapter = PeriodHttpAdapter(o)
        adapter.init_from_post(request.POST, tz)
        # Query the database to insure data is in the correct data type
        o = self.dbobject.objects.get(id=o.id)

        revision.comment = self.get_rev_comment(request, o, "create_worker")

        return HttpResponse(json.dumps(adapter.jsondict(tz)), mimetype="text/plain")
Exemplo n.º 2
0
    def read(self, request, *args, **kws):

        tz = args[0]
        # one or many?
        if len(args) == 1:
            # we are getting periods from within a range of dates
            sortField = jsonMap.get(request.GET.get("sortField", "start"), "start")
            order = "-" if request.GET.get("sortDir", "ASC") == "DESC" else ""

            # Either filter by date, or by something else.
            filterWnd = request.GET.get("filterWnd", None)

            # make sure we have defaults for dates
            defStart = datetime.now().strftime("%Y-%m-%d") if filterWnd is None else None
            defDays = "1" if filterWnd is None else None

            # Filtering by date involves a pair of keywords
            filterWnd = request.GET.get("filterWnd", None)
            filterElc = request.GET.get("filterElc", None)

            # make sure we have defaults for dates
            defStart = datetime.now().strftime("%Y-%m-%d") if filterWnd is None and filterElc is None else None
            defDays = "1" if filterWnd is None and filterElc is None else None

            startPeriods = request.GET.get("startPeriods", defStart)
            daysPeriods = request.GET.get("daysPeriods", defDays)

            if startPeriods is not None and daysPeriods is not None:
                if startPeriods is None:
                    startPeriods = datetime.now().strftime("%Y-%m-%d")
                if daysPeriods is None:
                    daysPeriods = "1"
                dt = TimeAgent.str2dt(startPeriods)
                start = dt if tz == "UTC" else TimeAgent.est2utc(dt)
                duration = int(daysPeriods) * 24 * 60
                periods = Period.get_periods(start, duration)
            else:
                # filter by something else
                query_set = Period.objects

                # window id
                # filterWnd = request.GET.get("filterWnd", None)
                if filterWnd is not None:
                    wId = int(filterWnd)
                    query_set = query_set.filter(window__id=wId)

                # elective id
                # filterElc = request.GET.get("filterElc", None)
                if filterElc is not None:
                    eId = int(filterElc)
                    query_set = query_set.filter(elective__id=eId)

                periods = query_set.order_by(order + sortField)

            return HttpResponse(
                json.dumps(
                    dict(total=len(periods), periods=[PeriodHttpAdapter(p).jsondict(tz) for p in periods], success="ok")
                ),
                content_type="application/json",
            )
        else:
            # we're getting a single period as specified by ID
            p_id = int(args[1])
            # p       = Period.objects.get(id = p_id)
            p = get_object_or_404(Period, id=p_id)
            adapter = PeriodHttpAdapter(p)
            return HttpResponse(
                json.dumps(dict(period=adapter.jsondict(tz), success="ok")), content_type="application/json"
            )