Ejemplo n.º 1
0
 def save(self, commit=True, owner=None, additional_metadata=None):
     if not additional_metadata:
         additional_metadata = {}
     resource = super(ResourceForm, self).save(commit=False)
     metadata_payload = {
         "name": self.data["name"],
         "description": self.data["description"],
         "author": owner.username,
         "category": self.data["category"],
         "tags": self.data["tags"],
         "semanticAnnotations": [
             {"semanticConcept": {"conceptURI": conceptURI}}
             for conceptURI in self.data["semanticAnnotations"].split(",")
             if str(conceptURI) is not ""
         ],
         "licence": self.data["licence"],
         "localID": self.data["localID"],
         "resourceURL": self.data["resourceURL"],
         "type": self.data["type"],
         "relatedResources": [
             {"relatedResource": {"resourceID": gId}} for gId in self.data.getlist("relatedResources")
         ],
         "linkedTo": self.data["linkedTo"],
     }
     metadata_payload.update(additional_metadata)
     try:
         cache.delete(self.instance.global_id)
         if self.instance.global_id is None:
             raise Exception()
         update_resource_metadata(self.instance.global_id, metadata_payload, metadata_payload["type"])
     except Exception, e:
         if self.instance.global_id is not None:
             raise e
         resource.global_id = set_resource_metadata(metadata_payload, metadata_payload["type"])
         resource.owner = owner
Ejemplo n.º 2
0
 def rate(self, user, rating):
     if Rating.objects.filter(user=user, resource=self).count() > 0:
         Rating.objects.filter(user=user, resource=self).delete()
     Rating.objects.create(rate=rating, user=user, resource=self)
     avg = Rating.objects.filter(resource=self).aggregate(Avg('rate'))['rate__avg']
     update_resource_metadata(self.global_id, {'globalID':self.metadata['globalID'], 'rating': str(avg), 'type': self.metadata['type']}, self.metadata['type'])
     cache.delete(self.global_id)
     return True
Ejemplo n.º 3
0
                r = Resource.objects.get(global_id=global_id['resourceID'])
            except ObjectDoesNotExist:
                continue
            relatedResource.append(( global_id['resourceID'], r.metadata['name']))
        self.metadata['relatedResources'] = relatedResource

    def reset_permissions(self):
        return PrincipalRoleRelation.objects.filter(role__name__in=['Reader', 'Editor', 'Manager'], content_id = self.id).delete()

    def update_views_counter(self):
        metadata = get_resource_metadata(self.global_id)
        try:
            views = int(metadata['views']) + 1
        except ValueError, e:
            views = 1
        update_resource_metadata(self.global_id, {'views': str(views)}, metadata['type'])
        return views

    def delete(self, using=None, delete_metadata = False):
        if delete_metadata:
            delete_resource_metadata(self.global_id)
        return super(Resource, self).delete(using)

    def get_pending_requests_by_resource(self):
        requests = ResourceRequest.objects.filter(resource=self)
        pending_requests = []
        for r in requests:
            if is_request_pending(r):
                pending_requests.append(r)
        return pending_requests