示例#1
0
文件: utils.py 项目: mikpanko/grakon
    def wrapper(self, entity, resource, provider=None, *args, **kwargs):
        if not resource:
            return u'Не указан ресурс'

        if self.model.feature not in type(entity).features:
            return u'К модели нельзя прикрепить ресурс'

        if type(entity).entity_name != 'projects':
            if resource not in RESOURCE_DICT.keys():
                return u'Ресурс должен быть в списке разрешенных'

        result = func(self, entity, resource, provider=provider, *args, **kwargs) # само выполнение декорированного метода

        entity.clear_cache()
        if provider:
            provider.clear_cache()

        entity.update_rating()
        if entity.entity_name == 'ideas':
            entity.task.update_rating()

        return result
示例#2
0
文件: models.py 项目: mikpanko/grakon
    def update(self, entity, resources):
        """ метод может работать как просто со списком названий ресурсов, так и со списокм словарей формата   [{'resource': resource, 'description': description}, ] """

        # пустой список ресурсов
        if not len(resources):
            return

        # если пришел список строк -- значит пришли лишь имена ресурсов, без описаний
        if type(resources[0]) is str or type(resources[0]) is unicode:
            full_resources = [{'resource': resource, 'description': ''} for resource in resources]
        # если пришел список dict'ов -- значит это полный список ресурсов, с описанием
        elif type(resources[0]) is dict:
            full_resources = resources
            resources = set([item['resource'] for item in resources])
        else:
            return

        if self.model.feature not in type(entity).features:
            return

        # Filter out resources not from the list
        if type(entity).entity_name != 'projects':
            resources = set(RESOURCE_DICT.keys()) & set(resources)

        entity_resources = list(getattr(entity, self.model.feature).all())
        allowed_resources = resources - set(er.resource for er in entity_resources)
        new_resources = filter(lambda item: item['resource'] in allowed_resources, full_resources)

        for er in entity_resources:
            if er.resource not in resources:
                er.delete()

        # TODO: this can cause IntegrityError
        self.bulk_create([self.model(entity=entity, resource=resource['resource'],
                description=resource['description']) for resource in new_resources])

        entity.clear_cache()