def render(self, request, context): redirect = False # Redirect users to the read only version if they've got no write # permission. if not context["can_edit"]: redirect = True else: # Try to acquire a write lock try: models.LockStrategy().acquire_lock(context["thing"], models.Thing.get_or_create_user_thing(request.user)) except models.LockException: redirect = True if redirect: return shortcuts.redirect("admin list read", context["thing"]) grouped_subjects = models.Subjects.group_for_part_drill_down( models.Subjects.all_subjects()) context["subjects"] = models.Subjects.to_json(grouped_subjects) context["type_choices"] = models.Event.get_type_choices_dict() return shortcuts.render(request, "administrator/timetableList/write.html", context)
def test_get_status(self): time = Time(START_TIME) hal = models.Thing.objects.get(fullpath="user/hal") asncp1 = models.Thing.objects.get(fullpath="tripos/asnc/I") expiry_short = time.now() + datetime.timedelta(minutes=2) expiry_long = time.now() + datetime.timedelta(hours=2) lockstrategy = models.LockStrategy(now=time.now) # set short lock only lock = models.ThingLock.objects.create(thing=asncp1, owner=hal, expires=expiry_short, name="short") lock.save() status = lockstrategy.get_status(["tripos/asnc/I"]) self.assertTrue(status["tripos/asnc/I"] == False) # no full lock from only setting short lock # set long lock lock = models.ThingLock.objects.create(thing=asncp1, owner=hal, expires=expiry_long, name="long") lock.save() status = lockstrategy.get_status(["tripos/asnc/I"]) self.assertDictEqual( status["tripos/asnc/I"], {"name": "hal"}) # both locks set so should get details of user
def get_permissions(self, username, thing): can_edit = thing.can_be_edited_by(username) if not can_edit: lock_holder = None else: lock_holder = models.LockStrategy().get_holder(thing) return (can_edit, lock_holder)
def _create_locker(self): # Make our time start from START_TIME time = Time(START_TIME) # The thing we'll lock against thing = models.Thing.objects.get(fullpath="tripos/asnc/I") # Create a LockStrategy instance to perform the locking locker = models.LockStrategy(now=time.now, timeout_lock_timeout=self.SHORT_TIMEOUT, edit_lock_timeout=self.LONG_TIMEOUT) return time, thing, locker
def locks_status_view(request): """ Returns JSON feed containing the lock status of the specified things. Things should be timetables (others may be passed in but this makes no sense). Things whose status are required are contained in the POST data. """ # get the timetables data timetables = request.POST.getlist("timetables[]") # pass through models.LockStrategy.get_status() lockstrategy = models.LockStrategy() locks_status = lockstrategy.get_status(timetables) return HttpResponse(json.dumps(locks_status), content_type="application/json")
def refresh_lock(request, thing=None): thing = shortcuts.get_object_or_404( models.Thing, pathid=models.Thing.hash(thing)) user = models.Thing.get_or_create_user_thing(request.user) is_editing = request.POST.get("editing", "").lower() == "true" try: models.LockStrategy().refresh_lock(thing, user, is_editing) response = { "refreshed": True, "message": None } except models.LockException as e: response = { "refreshed": False, "message": e.message } return http.HttpResponse(json.dumps(response), content_type="application/json")