def validate_file(self, metadata, i, fn, attr_path, attr_hash): if metadata[self.s3_server][fn]['validate']: if isinstance(metadata[self.s3_server][fn]['validate'], str): v = parse_datetime(metadata[self.s3_server][fn]['validate']) else: v = metadata[self.s3_server][fn]['validate'] # if timezone.localtime() - v > timedelta(seconds=30): #print(self.start_time - v) if self.start_time - v < timedelta(days=self.days_validate): return True t_p = self.temp_file_from_object( self.bucket_name, i, fn, attr_path ) if t_p is None: return True if not t_p: return False hash = hash_sha512(t_p) if os.path.exists(t_p): os.remove(t_p) if hash == metadata[self.s3_server][fn][attr_hash]: return True else: print('Documento Sem validação:', i.id, i) return False
def send_file(self, metadata, i, ff, fn, attr_path, attr_hash): if not hasattr(ff, attr_path): return 0 if os.path.exists(getattr(ff, attr_path)): if metadata[self.s3_server][fn][attr_path]: # return 0 t = os.path.getmtime(getattr(ff, attr_path)) date_file = datetime.fromtimestamp(t, timezone.utc) if parse_datetime( metadata[self.s3_server][fn][attr_path]) > date_file: result = self.validate_file(metadata, i, fn, attr_path, attr_hash) if result: return 0 else: print('Arquivo Substituído...', i, attr_path) # return 0 print('Enviando...', i.id, i, attr_path) """self.s3Lc.upload_file( getattr(ff, attr_path), self.bucket_name, ff.original_name if 'original' in attr_path else ff.name, ExtraArgs={ 'ACL': 'private', 'Metadata': { 'pk': f'{i._meta.label_lower}.{i.id}' } } )""" obj = self.s3r.Object( self.bucket_name, ff.original_name if 'original' in attr_path else ff.name, ) with open(getattr(ff, attr_path), "rb") as f: obj.upload_fileobj(f, ExtraArgs={ 'ACL': 'private', 'Metadata': { 'pk': f'{i._meta.label_lower}.{i.id}' } }) metadata[self.s3_server][fn][attr_path] = timezone.localtime() metadata[self.s3_server][fn][attr_hash] = hash_sha512( getattr(ff, attr_path)) metadata[self.s3_server][fn]['validate'] = timezone.localtime() return 1 return 0
def gerar_certidao(cls, user, obj, file_field_name, pk=None): path = getattr(obj, file_field_name).path if not path: return original__path = path.replace('media/sapl', 'media/original__sapl') hash_code = hash_sha512(original__path) cp = CertidaoPublicacao() cp.id = pk cp.content_object = obj cp.hash_code = hash_code cp.owner = user cp.modifier = user cp.field_name = file_field_name if not pk: cp.save() return cp