Esempio n. 1
0
	def list_entities(self, klass, order_by = None, owns = {}, order_desc = False, **filtr):
		if isinstance(klass, basestring):
			klass = get_entity_class(klass)
		entities = self.__cache.list_entities(klass)
		
		if order_by is None and entities and hasattr(entities[0], "commonName"):
			order_by = "commonName"
		
		for k, v in filtr.iteritems():
			#entities = [ e for e in entities if getattr(e, k) == v ]
			entities = filter(lambda e: getattr(e, k) == v, entities)
			
		for k, v in owns.iteritems():
			#entities = [ e for e in entities if v in getattr(e, k) ]
			entities = filter(lambda e: v in getattr(e, k), entities)
		
		if order_by and order_by[0] != "_":
			try:
				entities.sort(key = lambda x: getattr(x, order_by))
			except AttributeError:
				self.logger.exception("Error sorting result")
			else:
				if order_desc:
					entities.reverse()
		
		return tuple(entities)
Esempio n. 2
0
	def _handle_request(self, request):
		klass = get_entity_class(self._get_param("entity_type"))
		
		try:
			page = int(self._get_param("page")) - 1
		except (TypeError, ValueError, MissingParameter):
			page = 0
			
		try:
			rows = int(self._get_param("rows"))
		except (TypeError, ValueError, MissingParameter):
			rows = 10
			
		repo = get_repo()
		filterargs = request.REQUEST.getlist("owns[]")
		owns = {}
		for f in filterargs:
			key, _, valuestr = f.partition("=")
			valklass, _, valid = valuestr.split("-")
			if key and valklass and valuestr:
				try:
					value = repo.get_entity(valklass, valid)
				except NoEntityFound:
					self.logger.exception("error filtering result according to %s", f)
					continue
				owns[key] = value
				
		if klass.__name__ == "ResourceSpec":
			try:
				ptm_id = request.REQUEST["ptm"]
				ptm = repo.get_entity( Ptm, id = ptm_id)
#				entities = ptm.resourceSpecs#needs sorting
				ptm_info = ptm.describedByPtmInfo
				owns["supportedBy"] = ptm_info
			except KeyError:
				pass
		
		entities = repo.list_entities(klass, order_by = request.REQUEST.get("sidx"), order_desc = request.REQUEST.get("sord", "").lower() == "desc", owns = owns)
		records = len(entities)
		
		offset = rows * page
		if offset >= records:
			offset = 0
					
		rowdata = [ {
					"id": str(e.id),
					"cell": [ self._convert_value(getattr(e, f.name)) for f in get_grid_fields(klass )],
				} for e in entities[offset:offset + rows] ]

		result = {
			"total": str(int(ceil(float(records) / float(rows)))),
			"records": str(records),
			"page": str(page + 1),
			"rows": rowdata
		}
				
		return dumps(result)
Esempio n. 3
0
	def get_entity(self, klass, id):
		if isinstance(klass, basestring):
			klass = get_entity_class(klass)
		id = int(id)
		try:
			return self._get_entity(klass, id)
		except NoEntityFound:
			self.logger.debug("No entity found for %s-%s. retrying after refresh." % (klass.__name__, id))
			self.refresh()
			return self._get_entity(klass, id)
Esempio n. 4
0
	def list(self, request, typename = None):
		typename = typename or self._get_param(request, "entity_type")
		klass = get_entity_class(typename)
		self.perms.enforce_can_list(request, klass)

		return {
			"typename": typename,
			"can_add": self.perms.can_add(request, klass),
			"grid": mark_safe(self._render_grid(klass)),
			"entities": self.repo.list_entities(klass, order_by = "commonName"),
		}
Esempio n. 5
0
	def delete(self, request):
		type = self._get_param("entity_type")
		klass = get_entity_class(type)
		id = self._get_param("id")
		entity = self.repo.get_entity(klass, id)
		
		self.perms.enforce_can_delete(request, entity)
		
		self.repo.delete_entity(entity)
		
		return redirect(reverse("repogui", args= ["list"]) + "?entity_type=" + type)
Esempio n. 6
0
	def _get_add_target(self, request):
		try:
			val = request.REQUEST["add_to"]
		except KeyError:
			return (None, None, None)
		
		val, _, attr = val.partition(".")
		
		typename, _, id = val.rpartition("-")
		
		klass = get_entity_class(typename)
		
		assert id and attr
	
		return (klass, id, attr)
Esempio n. 7
0
	def edit(self, request):
		try:
			form = request.session.pop("validated_form")
		except KeyError:
			entity_type = self._get_param(request, "entity_type")
			add_to = request.REQUEST.get("add_to")
			klass = get_entity_class(entity_type)
			form = self.forms.get_form(klass)()
			try:
				id = int(request.REQUEST["id"])
			except (KeyError, TypeError, ValueError):
				entity = id = None
				form.remove_field("id")
			else:
				entity = self.repo.get_entity(klass, id)

#				if not entity.is_persistent:
#					self.logger.warn("Trying to edit non-peristent entity %s. Will persist it first." % (entity, ))
				self.repo.persist(entity)
				self.perms.enforce_can_edit(request, entity)
				
				form.set_values(entity)
		else:
			form.object_factory = self.repo
			klass = form.__klass__
			add_to = request.session.pop("add_to", None)
			
			#entity_type = hasattr(klass, "__display_typename__") and klass.__display_typename__ or klass.__name__
			entity_type = klass.__display_typename__
			try:
				id = form.get_field_value("id")
			except KeyError:
				id = entity = None
			else:
				entity = self.repo.get_entity(klass, id)
			
		cname = hasattr(klass, "__display_typename__") and klass.__display_typename__ or klass.__name__
					
		header = "%s %s %s" % (id is None and "Add" or "Edit", cname, id is not None and id or "")
		
		return {
			"entity": entity,
			"form": form,
			"entity_type": klass.__name__,
			"header": header,
			"add_to": add_to,
			"action": reverse("repoactions", args = ("form", ))
		}
Esempio n. 8
0
	def show(self, request):
		typename = self._get_param(request, "entity_type")			
		entity = self.repo.get_entity(get_entity_class(typename), int(self._get_param(request, "id")))
		template = get_template("repogui-entity-details.html")
		
		self.perms.enforce_can_view(request, entity)
		
		details = self.__render_details(entity.__class__.__name__, entity, template, request)
		
		#self.logger.debug("complex: %s %s" % (values, complex_values))
		
		return {
			"entity": entity,
			"typename": entity.__class__.__name__,
			"details": details, 
			"header_action": "Details"
		}
Esempio n. 9
0
	def _list_op(self, request, operation):
#		try:
		entity = self.repo.get_entity(get_entity_class(self._get_param("entity_type")), int(self._get_param("id")))
	
		addentity, add_to, return_entity = self._parse_listinfo(request)
			
		getattr(add_to, operation)(entity)
			
		self.repo.persist(addentity)	
#		except ValueError:
#			if self._get_param("id") == None:
#				pass
		
		try:
			retpath = unquote(request.REQUEST["return_path"])
		except KeyError:
			retpath = reverse("repogui", args = ("show", )) + "?id=%d&entity_type=%s" % (return_entity.id, return_entity.__class__.__name__)
		
		return redirect(retpath)
Esempio n. 10
0
	def form(self, request):
		type = self._get_param("entity_type")
		klass = get_entity_class(type)
		add_to = None		
		form = self.__forms.get_form(klass)
	
		try:
			id = int(request.REQUEST["id"])
		except KeyError:
			form = form()
			form.remove_field("id")
			entity = None
		else:
			entity = self.repo.get_entity(klass, id)
			form = form(instance = entity)
		
		values = self._validate_form(form, request.REQUEST, klass)
		if values:
			if entity is None:
				addentity, add_to, return_entity = self._parse_listinfo(request)
								
				self.perms.enforce_can_add(request, klass)
					
				if issubclass(klass, Ptm):
					try:
						owner = request.user.teagleuser.organisations[0]
					except (AttributeError, IndexError):
						from django.conf import settings
#						if not getattr(settings, "TEAGLE_PERMISSIVE"):
#							raise
						owner = self.repo.get_entity(Organisation, 1)
					values["provider"] = owner
					values["owner"] = self.repo.get_entity(Person, 1)#root
#				if issubclass(klass, ResourceSpec):
#					values["isInstantiable"] = "true"
				
				entity = self._make_entity(klass, values)
			else:
				self.perms.enforce_can_edit(request, entity)
				for k, v in values.iteritems():
					if k != "id":
						self.logger.debug("Setting value: %s -> %s" % (k, v))
						setattr(entity, k, v)
			
			if add_to is not None:
				add_to.append(entity)
				self.repo.persist(addentity)
			else:
				return_entity = entity
				self.repo.persist(entity)
				
			try:
				return_path = unquote(request.REQUEST["return_path"])
			except KeyError:				
				return_path = reverse("repogui", args = ("show", )) + "?id=%d&entity_type=%s" % (return_entity.id, return_entity.__class__.__name__)
			
			return redirect(return_path)
		
		self.logger.debug("form did not validate")
		request.session["validated_form"] = form
		request.session["add_to"] = request.REQUEST.get("add_to")
		
		try:
			return_path = unquote(request.REQUEST["form_path"])
		except KeyError:
			return_path = reverse("repogui", args = ("edit", ))
		return redirect(return_path)