def setUp(self): # Don't use CAS for authentication during unit tests if "django_cas.backends.CASBackend" in settings.AUTHENTICATION_BACKENDS: settings.AUTHENTICATION_BACKENDS = settings.AUTHENTICATION_BACKENDS[:-1] if "django_cas.middleware.CASMiddleware" in settings.MIDDLEWARE_CLASSES: settings.MIDDLEWARE_CLASSES = settings.MIDDLEWARE_CLASSES[:-1] self.client = Client() self.auth_user = m.User.objects.create_user("dss", "*****@*****.**", "asdf5!") self.auth_user.is_staff = True self.auth_user.save() # create the user self.u = User( first_name="dss", # "Test" last_name="account", # "User" pst_id=3251 # , username = self.auth_user.username ) self.u.save() self.u.addRole("Administrator") self.u.addRole("Staff") self.client.login(username="******", password="******") self.sess = create_sesshun() self.fdata = {"session": self.sess.id, "date": "2009-06-01", "time": "00:00", "duration": 1.0, "backup": True} # create the dss period self.period = Period() adapter = PeriodHttpAdapter(self.period) adapter.init_from_post(self.fdata, "UTC") self.period.save()
def update(self, request, *args, **kws): tz = args[0] id = int(args[1]) o = self.dbobject.objects.get(id=id) adapter = PeriodHttpAdapter(o) adapter.update_from_post(request.POST, tz) revision.comment = self.get_rev_comment(request, o, "update") return HttpResponse(json.dumps({"success": "ok"}), mimetype="text/plain")
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")
def test_windowed_period_init(self): self.s.session_type = Session_Type.objects.get(type = 'windowed') self.s.save() window = Window(session = self.s , total_time = 10) window.save() windowRange = WindowRange(window = window , start_date = datetime(2011, 10, 12, 12) , duration = 7) windowRange.save() adapter = PeriodHttpAdapter(Period()) data = {'handle': '%s (%s) %s' % (self.s.name, self.s.project.pcode, self.s.id) , 'window_id': str(window.id) } adapter.init_from_post(data, 'UTC') # Make sure the period starts on the last date of the window (default) self.assertEqual(adapter.period.start.date(), window.last_date())
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" )