Exemplo n.º 1
0
def get_edmProperties_from_entry(entry):
    category = entry.category[0].term
    i = category.rfind(".") + 1
    class_name = category[i:]
    entityClass = entityFactory.get_class_by_name(class_name)

    dic = {}

    for property in entry.content.extension_elements[0].extension_elements:
        NULL_ATTRIBUTE_KEY = "{%s}null" % EDMX_METADATA_NAMESPACE
        if NULL_ATTRIBUTE_KEY in property.attributes and property.attributes[NULL_ATTRIBUTE_KEY] == "true":
            value = None
        else:
            field_name = property.tag

            # keys are not settable/copiable, and therefore cannot be considered as properties
            if field_name == "key" or field_name == "key_name":
                continue

            type_attribute_key = "{%s}type" % EDMX_METADATA_NAMESPACE
            edm_type = (
                property.attributes[type_attribute_key] if type_attribute_key in property.attributes else "Edm.String"
            )
            value = property.text
            db_type = getattr(entityClass, field_name).__class__

            value = TYPE_TRANSFORM_FUNCTIONS[db_type](value)

            dic[field_name] = value
    return dic
Exemplo n.º 2
0
def get_edmProperties_from_entry(entry):
    category = entry.category[0].term
    i = category.rfind('.') + 1
    class_name = category[i:]
    entityClass = entityFactory.get_class_by_name(class_name)

    dic = {}

    for property in entry.content.extension_elements[0].extension_elements:
        NULL_ATTRIBUTE_KEY = '{%s}null' % EDMX_METADATA_NAMESPACE
        if NULL_ATTRIBUTE_KEY in property.attributes and property.attributes[
                NULL_ATTRIBUTE_KEY] == 'true':
            value = None
        else:
            field_name = property.tag

            # keys are not settable/copiable, and therefore cannot be considered as properties
            if field_name == 'key' or field_name == 'key_name':
                continue

            type_attribute_key = '{%s}type' % EDMX_METADATA_NAMESPACE
            edm_type = property.attributes[
                type_attribute_key] if type_attribute_key in property.attributes else 'Edm.String'
            value = property.text
            db_type = getattr(entityClass, field_name).__class__

            value = TYPE_TRANSFORM_FUNCTIONS[db_type](value)

            dic[field_name] = value
    return dic
Exemplo n.º 3
0
	def merge(self):
		request_url = urllib.unquote(self.request.path).replace('/%s/' % core.BASE_SVC_URL, '')
		dic = core.parse_request_url(request_url)
		class_name = dic['class_name']
		key = dic['key']

		entityClass = entityFactory.get_class_by_name(class_name)
		o = entityClass.get(key)

		entry = atom.core.parse(self.request.body, atom.data.Entry)
		core.update_entity_from_atomEntry(o, entry)

		o.put()
Exemplo n.º 4
0
    def merge(self):
        request_url = urllib.unquote(self.request.path).replace(
            '/%s/' % core.BASE_SVC_URL, '')
        dic = core.parse_request_url(request_url)
        class_name = dic['class_name']
        key = dic['key']

        entityClass = entityFactory.get_class_by_name(class_name)
        o = entityClass.get(key)

        entry = atom.core.parse(self.request.body, atom.data.Entry)
        core.update_entity_from_atomEntry(o, entry)

        o.put()
Exemplo n.º 5
0
    def get(self):
        request_url = urllib.unquote(self.request.path).replace(
            '/%s/' % core.BASE_SVC_URL, '')
        dic = core.parse_request_url(request_url)

        class_name = dic['class_name']
        key = dic['key']
        deferred_entity = dic['deferred_entity']

        entityClass = entityFactory.get_class_by_name(dic['class_name'])
        query = entityClass.all()

        results = []

        if key and len(key) > 0:
            if deferred_entity and len(deferred_entity) > 0:
                parent = entityClass.get(key)
                results = [getattr(parent, deferred_entity)]
            else:
                results = [entityClass.get(key)]
        else:
            # $orderby operator
            if ('$orderby' in self.request.str_params):
                orderby_params = self.request.str_params['$orderby'].split(',')
                for orderby_param in orderby_params:
                    parts = orderby_param.split(' ')
                    field = parts[0]
                    order = '' if (len(parts) == 1
                                   or parts[1] == 'asc') else '-'
                    query = query.order('%s%s' % (order, field))

# $filter operator
            if ('$filter' in self.request.str_params):
                filter = self.request.str_params['$filter']
                filter = filter.replace('(', '').replace(')', '')

                if '(' in filter or ')' in filter:  #FIXME : impossible
                    self.error(400)
                    self.response.out.write(
                        "Grouping operators '(' and ')' are not supported\n")
                    self.response.out.write("Functions are not supported\n")
                    return
                elif ' or ' in filter:
                    self.error(400)
                    self.response.out.write(
                        "Only the 'and' operator is supported by the App Engine Datastore"
                    )
                    return
                else:
                    import re
                    filter_params = filter.split(' and ')
                    for filter_param in filter_params:
                        prop, op, val = filter_param.split(' ')

                        if prop == 'key':
                            prop = '__key__'
                        elif prop.endswith('__key__'):
                            prop = prop[:-7]
                            val = db.Model.get(core.TYPE_TRANSFORM_FUNCTIONS[
                                db.StringProperty](val)).key()
                        else:
                            val = core.TYPE_TRANSFORM_FUNCTIONS[getattr(
                                entityClass, prop).__class__](val)

                        try:
                            op = op.lower()
                            OPERATOR_MAPPING = {
                                'eq': '=',
                                'ne': '!=',
                                'gt': '>',
                                'ge': '>=',
                                'lt': '<',
                                'le': '<=',
                            }

                            query.filter(
                                '%s %s' % (prop, OPERATOR_MAPPING[op]), val)
                        except:
                            import sys

                            self.error(400)
                            self.response.out.write(
                                "Unable to understand expression '%s' : " %
                                filter_param)
                            self.response.out.write(str(sys.exc_info()[1]))
                            return

# fetch data and $top operator and $skip operator
            nbEntities = int(self.request.str_params['$top']) if (
                '$top' in self.request.str_params) else core.MAX_FETCH_COUNT
            skipEntities = int(self.request.str_params['$skip']) if (
                '$skip' in self.request.str_params) else 0
            results = query.fetch(nbEntities, skipEntities)


# format response in json
        response_format = self.request.str_params['$format'] if (
            '$format' in self.request.str_params) else 'atom'
        if response_format == 'atom':
            self.response.headers[
                "Content-Type"] = "application/atom+xml;charset=utf-8"
            self.response.out.write('<?xml version="1.0" standalone="yes"?>')
            self.response.out.write(format_atom(self.request, results))
        elif response_format == 'json':
            self.response.headers[
                "Content-Type"] = "application/json;charset=utf-8"
            self.response.out.write(json.encode(results))
        else:
            self.error(406)
            self.response.out.write('%s format is currently not supported' %
                                    response_format)
Exemplo n.º 6
0
	def get(self):
		request_url = urllib.unquote(self.request.path).replace('/%s/' % core.BASE_SVC_URL, '')
		dic = core.parse_request_url(request_url)

		class_name = dic['class_name']
		key = dic['key']
		deferred_entity = dic['deferred_entity']

		entityClass = entityFactory.get_class_by_name(dic['class_name'])
		query = entityClass.all()
		
		results = []

		if key and len(key)>0:
			if deferred_entity and len(deferred_entity)>0:
				parent = entityClass.get(key)
				results = [getattr(parent, deferred_entity)]
			else:
				results = [entityClass.get(key)]
		else:
# $orderby operator
			if ('$orderby' in self.request.str_params):
				orderby_params = self.request.str_params['$orderby'].split(',')
				for orderby_param in orderby_params:
					parts = orderby_param.split(' ')
					field = parts[0]
					order = '' if (len(parts)==1 or parts[1]=='asc') else '-'
					query = query.order('%s%s' % (order, field))

# $filter operator
			if ('$filter' in self.request.str_params):
				filter = self.request.str_params['$filter']
				filter = filter.replace('(', '').replace(')', '')

				if '(' in filter or ')' in filter:	#FIXME : impossible
					self.error(400)
					self.response.out.write("Grouping operators '(' and ')' are not supported\n")
					self.response.out.write("Functions are not supported\n")
					return
				elif ' or ' in filter:
					self.error(400)
					self.response.out.write("Only the 'and' operator is supported by the App Engine Datastore")
					return
				else:
					import re
					filter_params = filter.split(' and ')
					for filter_param in filter_params:
						prop,op,val = filter_param.split(' ')

						if prop=='key':
							prop = '__key__'
						elif prop.endswith('__key__'):
							prop = prop[:-7]
							val = db.Model.get(core.TYPE_TRANSFORM_FUNCTIONS[db.StringProperty](val)).key()
						else:
							val = core.TYPE_TRANSFORM_FUNCTIONS[getattr(entityClass, prop).__class__](val)

						try:
							op = op.lower()
							OPERATOR_MAPPING = {
								'eq':'=',
								'ne':'!=',
								'gt':'>',
								'ge':'>=',
								'lt':'<',
								'le':'<=',
							}



							query.filter('%s %s'%(prop,OPERATOR_MAPPING[op]), val)
						except:
							import sys

							self.error(400)
							self.response.out.write("Unable to understand expression '%s' : "%filter_param)
							self.response.out.write(str(sys.exc_info()[1]))
							return


# fetch data and $top operator and $skip operator
			nbEntities = int(self.request.str_params['$top']) if ('$top' in self.request.str_params) else core.MAX_FETCH_COUNT
			skipEntities = int(self.request.str_params['$skip']) if ('$skip' in self.request.str_params) else 0
			results = query.fetch(nbEntities, skipEntities)

# format response in json
		response_format = self.request.str_params['$format'] if ('$format' in self.request.str_params) else 'atom'
		if response_format=='atom':
			self.response.headers["Content-Type"] = "application/atom+xml;charset=utf-8"
			self.response.out.write('<?xml version="1.0" standalone="yes"?>')
			self.response.out.write(format_atom(self.request, results))
		elif response_format=='json':
			self.response.headers["Content-Type"] = "application/json;charset=utf-8"
			self.response.out.write(json.encode(results))
		else:
			self.error(406)
			self.response.out.write('%s format is currently not supported' % response_format)