Commit b0566599 authored by Mathieu Rodic's avatar Mathieu Rodic

[BUGFIX] Resources - Debugged the duplicata check when appending resource to node

parent b609b1d7
...@@ -11,7 +11,7 @@ from parsing.Caches import LanguagesCache, NgramsExtractorsCache, NgramsCaches ...@@ -11,7 +11,7 @@ from parsing.Caches import LanguagesCache, NgramsExtractorsCache, NgramsCaches
from parsing.FileParsers import * from parsing.FileParsers import *
from time import time from time import time
from collections import defaultdict from collections import defaultdict
from hashlib import md5 import hashlib
from gargantext_web.settings import MEDIA_ROOT from gargantext_web.settings import MEDIA_ROOT
...@@ -88,25 +88,29 @@ class Node(CTENode): ...@@ -88,25 +88,29 @@ class Node(CTENode):
def __str__(self): def __str__(self):
return self.name return self.name
def get_resources(self):
return Resource.objects.select_related('node_resource').filter(node_resource__node = self)
def add_resource(self, **kwargs): def add_resource(self, **kwargs):
# only for tests # only for tests
# resource = Resource(guid=str(time()), digest=str(time()), **kwargs ) # resource = Resource(guid=str(time()), digest=str(time()), **kwargs )
# Compute the hash resource = Resource(**kwargs)
m = md5() # User
m.update(open(str(resource.file)).read()) if 'user' not in kwargs and 'user_id' not in kwargs:
resource.digest = m.hexdigest() resource.user = self.user
resource.save() # Compute the digest
h = hashlib.md5()
f = open(str(resource.file), 'rb')
h.update(f.read())
f.close()
resource.digest = h.hexdigest()
# check if a resource on this node already has this hash # check if a resource on this node already has this hash
try: tmp_resource = self.get_resources().filter(digest = resource.digest).first()
node_resource = Node_Resource.objects.get( if tmp_resource:
node__id = self.id, return tmp_resource
resource__digest = resource.digest else:
) resource.save()
resource = node_resource.resource
except:
pass
# link with the resource # link with the resource
node_resource = Node_Resource( node_resource = Node_Resource(
node = self, node = self,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment