Example #1
0
 def _attachment_from_resource(self,
                               folder,
                               material,
                               resource,
                               base_object=None):
     modified_dt = (getattr(material, '_modificationDS', None)
                    or getattr(base_object, 'startDate', None)
                    or getattr(base_object, '_modificationDS', None)
                    or now_utc())
     data = {
         'id': self._get_id(Attachment),
         'folder_id': folder['id'],
         'user_id': self.janitor_user_id,
         'title': convert_to_unicode(resource.name).strip()
         or folder['title'],
         'description': convert_to_unicode(resource.description),
         'modified_dt': modified_dt
     }
     if resource.__class__.__name__ == 'Link':
         data['type'] = AttachmentType.link
         data['link_url'] = convert_to_unicode(resource.url).strip()
         if not data['link_url']:
             self.print_error(
                 cformat('%{red!}[{}] Skipping link, missing URL').format(
                     data['title']),
                 event_id=base_object.id)
             return None
     else:
         data['type'] = AttachmentType.file
         storage_backend, storage_path, size = self._get_local_file_info(
             resource)
         if storage_path is None:
             self.print_error(
                 cformat('%{red!}File {} not found on disk').format(
                     resource._LocalFile__archivedId),
                 event_id=base_object.id)
             return None
         filename = secure_filename(convert_to_unicode(resource.fileName),
                                    'attachment')
         file_data = {
             'id': self._get_id(AttachmentFile),
             'attachment_id': data['id'],
             'user_id': self.janitor_user_id,
             'created_dt': modified_dt,
             'filename': filename,
             'content_type': mimetypes.guess_type(filename)[0]
             or 'application/octet-stream',
             'size': size,
             'storage_backend': storage_backend,
             'storage_file_id': storage_path
         }
         data['file_id'] = file_data['id']
         self.todo[AttachmentFile].append(file_data)
     tmp = ProtectionTarget()
     protection_from_ac(tmp, resource._Resource__ac)
     self.todo[AttachmentPrincipal] += tmp.make_principal_rows(
         attachment_id=data['id'])
     data['protection_mode'] = tmp.protection_mode
     self.todo[Attachment].append(data)
     return data
Example #2
0
 def _attachment_from_resource(self, folder, material, resource, base_object=None):
     modified_dt = (
         getattr(material, "_modificationDS", None)
         or getattr(base_object, "startDate", None)
         or getattr(base_object, "_modificationDS", None)
         or now_utc()
     )
     data = {
         "id": self._get_id(Attachment),
         "folder_id": folder["id"],
         "user_id": self.janitor_user_id,
         "title": convert_to_unicode(resource.name).strip() or folder["title"],
         "description": convert_to_unicode(resource.description),
         "modified_dt": modified_dt,
     }
     if resource.__class__.__name__ == "Link":
         data["type"] = AttachmentType.link
         data["link_url"] = convert_to_unicode(resource.url).strip()
         if not data["link_url"]:
             self.print_error(
                 cformat("%{red!}[{}] Skipping link, missing URL").format(data["title"]), event_id=base_object.id
             )
             return None
     else:
         data["type"] = AttachmentType.file
         storage_backend, storage_path, size = self._get_local_file_info(resource)
         if storage_path is None:
             self.print_error(
                 cformat("%{red!}File {} not found on disk").format(resource._LocalFile__archivedId),
                 event_id=base_object.id,
             )
             return None
         filename = secure_filename(convert_to_unicode(resource.fileName), "attachment")
         file_data = {
             "id": self._get_id(AttachmentFile),
             "attachment_id": data["id"],
             "user_id": self.janitor_user_id,
             "created_dt": modified_dt,
             "filename": filename,
             "content_type": mimetypes.guess_type(filename)[0] or "application/octet-stream",
             "size": size,
             "storage_backend": storage_backend,
             "storage_file_id": storage_path,
         }
         data["file_id"] = file_data["id"]
         self.todo[AttachmentFile].append(file_data)
     tmp = ProtectionTarget()
     protection_from_ac(tmp, resource._Resource__ac)
     self.todo[AttachmentPrincipal] += tmp.make_principal_rows(attachment_id=data["id"])
     data["protection_mode"] = tmp.protection_mode
     self.todo[Attachment].append(data)
     return data
Example #3
0
 def _folder_from_material(self, material, linked_object):
     folder_obj = AttachmentFolder(id=self._get_id(AttachmentFolder),
                                   title=convert_to_unicode(material.title).strip() or 'Material',
                                   description=convert_to_unicode(material.description),
                                   linked_object=linked_object,
                                   is_always_visible=not getattr(material._Material__ac,
                                                                 '_hideFromUnauthorizedUsers', False))
     folder = _sa_to_dict(folder_obj)
     self.todo[AttachmentFolder].append(folder)
     tmp = ProtectionTarget()
     protection_from_ac(tmp, material._Material__ac)
     self.todo[AttachmentFolderPrincipal] += tmp.make_principal_rows(folder_id=folder['id'])
     folder['protection_mode'] = tmp.protection_mode
     return folder
Example #4
0
 def _folder_from_material(self, material, linked_object):
     folder_obj = AttachmentFolder(id=self._get_id(AttachmentFolder),
                                   title=convert_to_unicode(material.title).strip() or 'Material',
                                   description=convert_to_unicode(material.description),
                                   linked_object=linked_object,
                                   is_always_visible=not getattr(material._Material__ac,
                                                                 '_hideFromUnauthorizedUsers', False))
     folder = _sa_to_dict(folder_obj)
     self.todo[AttachmentFolder].append(folder)
     tmp = ProtectionTarget()
     protection_from_ac(tmp, material._Material__ac)
     self.todo[AttachmentFolderPrincipal] += tmp.make_principal_rows(folder_id=folder['id'])
     folder['protection_mode'] = tmp.protection_mode
     return folder
Example #5
0
 def _attachment_from_resource(self, folder, material, resource, base_object=None):
     modified_dt = (getattr(material, '_modificationDS', None) or getattr(base_object, 'startDate', None) or
                    getattr(base_object, '_modificationDS', None) or now_utc())
     data = {'id': self._get_id(Attachment),
             'folder_id': folder['id'],
             'user_id': self.janitor_user_id,
             'title': convert_to_unicode(resource.name).strip() or folder['title'],
             'description': convert_to_unicode(resource.description),
             'modified_dt': modified_dt}
     if resource.__class__.__name__ == 'Link':
         data['type'] = AttachmentType.link
         data['link_url'] = convert_to_unicode(resource.url).strip()
         if not data['link_url']:
             self.print_error(cformat('%{red!}[{}] Skipping link, missing URL').format(data['title']),
                              event_id=base_object.id)
             return None
     else:
         data['type'] = AttachmentType.file
         storage_backend, storage_path, size = self._get_local_file_info(resource)
         if storage_path is None:
             self.print_error(cformat('%{red!}File {} not found on disk').format(resource._LocalFile__archivedId),
                              event_id=base_object.id)
             return None
         filename = secure_filename(convert_to_unicode(resource.fileName), 'attachment')
         file_data = {'id': self._get_id(AttachmentFile),
                      'attachment_id': data['id'],
                      'user_id': self.janitor_user_id,
                      'created_dt': modified_dt,
                      'filename': filename,
                      'content_type': mimetypes.guess_type(filename)[0] or 'application/octet-stream',
                      'size': size,
                      'storage_backend': storage_backend,
                      'storage_file_id': storage_path}
         data['file_id'] = file_data['id']
         self.todo[AttachmentFile].append(file_data)
     tmp = ProtectionTarget()
     protection_from_ac(tmp, resource._Resource__ac)
     self.todo[AttachmentPrincipal] += tmp.make_principal_rows(attachment_id=data['id'])
     data['protection_mode'] = tmp.protection_mode
     self.todo[Attachment].append(data)
     return data