def create_from_form(cls, form, user, *args, **kwargs): obj = super(DocumentController, cls).create_from_form(form, user, *args, **kwargs) if type(obj.object).ACCEPT_FILES: private_files = form.cleaned_data.get("pfiles", []) if any(pf.creator != user for pf in private_files): raise PermissionError("Not your file") for pf in private_files: doc_file = models.DocumentFile.objects.create( filename=pf.filename, size=pf.size, file=pf.file.path, document=obj.object) generate_thumbnail.delay(doc_file.id) # django < 1.2.5 deletes the file when pf is deleted pf.file = "" pf.delete() template = form.cleaned_data["template"] if not private_files and template: if template.type == obj.type and template.is_official: obj.copy_files(template) else: raise ValueError("invalid template") for df in obj.files: obj.handle_added_file(df) return obj
def add_file(self, f, update_attributes=True, thumbnail=True): """ Adds file *f* to the document. *f* should be a :class:`~django.core.files.File` with an attribute *name* (like an :class:`UploadedFile`). If *update_attributes* is True (the default), :meth:`handle_added_file` will be called with *f* as parameter. :return: the :class:`.DocumentFile` created. :raises: :exc:`.PermissionError` if :attr:`_user` is not the owner of :attr:`object` :raises: :exc:`.PermissionError` if :attr:`object` is not editable. :raises: :exc:`ValueError` if the file size is superior to :attr:`settings.MAX_FILE_SIZE` """ self.check_permission("owner") self.check_editable() if settings.MAX_FILE_SIZE != -1 and f.size > settings.MAX_FILE_SIZE: raise ValueError("File too big, max size : %d bytes" % settings.MAX_FILE_SIZE) f.name = f.name.encode("utf-8") doc_file = models.DocumentFile.objects.create(filename=f.name, size=f.size, file=models.docfs.save(f.name, f), document=self.object) self.save(False) # set read only file os.chmod(doc_file.file.path, 0400) self._save_histo("File added", "file : %s" % f.name) if update_attributes: self.handle_added_file(doc_file) if thumbnail: generate_thumbnail.delay(doc_file.id) return doc_file
def checkin(self, doc_file, new_file, update_attributes=True, thumbnail=True): """ Updates *doc_file* with data from *new_file*. *doc_file*.thumbnail is deleted if it is present. :exceptions raised: * :exc:`ValueError` if *doc_file*.document is not self.object * :exc:`ValueError` if the file size is superior to :attr:`settings.MAX_FILE_SIZE` * :exc:`plmapp.exceptions.UnlockError` if *doc_file* is locked but *doc_file.locker* is not the current user * :exc:`.PermissionError` if :attr:`_user` is not the owner of :attr:`object` * :exc:`.PermissionError` if :attr:`object` is not editable. :param doc_file: :type doc_file: :class:`.DocumentFile` :param new_file: file with new data, same parameter as *f* in :meth:`add_file` :param update_attributes: True if :meth:`handle_added_file` should be called """ self.check_permission("owner") self.check_editable() if doc_file.document.pk != self.object.pk: raise ValueError("Bad file's document") if doc_file.filename != new_file.name: raise ValueError("Checkin document and document already in plm have different names") if settings.MAX_FILE_SIZE != -1 and new_file.size > settings.MAX_FILE_SIZE: raise ValueError("File too big, max size : %d bytes" % settings.MAX_FILE_SIZE) if doc_file.locked: self.unlock(doc_file) os.chmod(doc_file.file.path, 0700) os.remove(doc_file.file.path) doc_file.filename = new_file.name doc_file.size = new_file.size doc_file.file = models.docfs.save(new_file.name, new_file) os.chmod(doc_file.file.path, 0400) if doc_file.thumbnail: doc_file.thumbnail.delete(save=False) doc_file.save() self._save_histo("Check-in", doc_file.filename) if update_attributes: self.handle_added_file(doc_file) if thumbnail: generate_thumbnail.delay(doc_file.id)
def create_from_form(cls, form, user, *args, **kwargs): obj = super(DocumentController, cls).create_from_form(form, user, *args, **kwargs) if type(obj.object).ACCEPT_FILES: private_files = form.cleaned_data.get("pfiles", []) if any(pf.creator != user for pf in private_files): raise PermissionError("Not your file") for pf in private_files: doc_file = models.DocumentFile.objects.create( filename=pf.filename, size=pf.size, file=pf.file.path, document=obj.object) obj.handle_added_file(doc_file) generate_thumbnail.delay(doc_file.id) # django < 1.2.5 deletes the file when pf is deleted pf.file = "" pf.delete() return obj
def add_file(self, f, update_attributes=True, thumbnail=True): """ Adds file *f* to the document. *f* should be a :class:`~django.core.files.File` with an attribute *name* (like an :class:`UploadedFile`). If *update_attributes* is True (the default), :meth:`handle_added_file` will be called with *f* as parameter. :return: the :class:`.DocumentFile` created. :raises: :exc:`.PermissionError` if :attr:`_user` is not the owner of :attr:`object` :raises: :exc:`.PermissionError` if :attr:`object` is not editable. :raises: :exc:`ValueError` if the file size is superior to :attr:`settings.MAX_FILE_SIZE` :raises: :exc:`ValueError` if we try to add a native file while a relate standar file locked is present in the Document """ self.check_edit_files() if settings.MAX_FILE_SIZE != -1 and f.size > settings.MAX_FILE_SIZE: raise ValueError("File too big, max size : %d bytes" % settings.MAX_FILE_SIZE) f.name = f.name.encode("utf-8") if self.has_standard_related_locked(f.name): raise ValueError("Native file has a standard related locked file.") doc_file = models.DocumentFile(filename=f.name, size=f.size, file=models.docfs.save(f.name, f), document=self.object) doc_file.no_index = getattr(self.object, "no_index", False) doc_file.save() self.save(False) # set read only file os.chmod(doc_file.file.path, 0400) self._save_histo("added file to ", "file : %s added" % f.name) if update_attributes: self.handle_added_file(doc_file) if thumbnail: generate_thumbnail.delay(doc_file.id) return doc_file
def create_from_form(cls, form, user, *args, **kwargs): obj = super(DocumentController, cls).create_from_form(form, user, *args, **kwargs) if type(obj.object).ACCEPT_FILES: private_files = form.cleaned_data.get("pfiles", []) if any(pf.creator != user for pf in private_files): raise PermissionError("Not your file") for pf in private_files: doc_file = models.DocumentFile.objects.create(filename=pf.filename, size=pf.size, file=pf.file.path, document=obj.object) generate_thumbnail.delay(doc_file.id) # django < 1.2.5 deletes the file when pf is deleted pf.file = "" pf.delete() template = form.cleaned_data["template"] if not private_files and template: if template.type == obj.type and template.is_official: obj.copy_files(template) else: raise ValueError("invalid template") for df in obj.files: obj.handle_added_file(df) return obj
def checkin(self, doc_file, new_file, update_attributes=True, thumbnail=True): """ Updates *doc_file* with data from *new_file*. *doc_file*.thumbnail is deleted if it is present. :exceptions raised: * :exc:`ValueError` if *doc_file*.document is not self.object * :exc:`ValueError` if the file size is superior to :attr:`settings.MAX_FILE_SIZE` * :exc:`plmapp.exceptions.UnlockError` if *doc_file* is locked but *doc_file.locker* is not the current user * :exc:`.PermissionError` if :attr:`_user` is not the owner of :attr:`object` * :exc:`.PermissionError` if :attr:`object` is not editable. :param doc_file: :type doc_file: :class:`.DocumentFile` :param new_file: file with new data, same parameter as *f* in :meth:`add_file` :param update_attributes: True if :meth:`handle_added_file` should be called """ self.check_edit_files() if doc_file.document.pk != self.object.pk: raise ValueError("Bad file's document") if doc_file.filename != new_file.name: raise ValueError( "Checkin document and document already in plm have different names" ) if settings.MAX_FILE_SIZE != -1 and new_file.size > settings.MAX_FILE_SIZE: raise ValueError("File too big, max size : %d bytes" % settings.MAX_FILE_SIZE) if doc_file.deprecated: raise ValueError("File is deprecated") doc_file.no_index = True if doc_file.locked: self.unlock(doc_file) now = timezone.now() previous_revision = doc_file.previous_revision doc_file.previous_revision = None doc_file.save() new_file.name = new_file.name.encode("utf-8") deprecated_df = models.DocumentFile.objects.create( document=self.object, deprecated=True, size=doc_file.size, filename=doc_file.filename, file=models.docfs.save(new_file.name, doc_file.file), thumbnail=None, ctime=doc_file.ctime, revision=doc_file.revision, end_time=now, previous_revision=previous_revision, last_revision=doc_file) if doc_file.thumbnail: path = models.thumbnailfs.save("%d.png" % deprecated_df.id, doc_file.thumbnail) deprecated_df.no_index = True deprecated_df.thumbnail = os.path.basename(path) deprecated_df.save() # update the doc_file doc_file.file = models.docfs.save(new_file.name, new_file) doc_file.size = new_file.size doc_file.previous_revision = deprecated_df doc_file.revision += 1 doc_file.ctime = now os.chmod(doc_file.file.path, 0400) doc_file.no_index = False doc_file.save() # delete "old" files (not the document file) self._delete_old_files(doc_file, ON_CHECKIN_SELECTORS) self._save_histo("checked-in ", doc_file.filename) if update_attributes: self.handle_added_file(doc_file) if thumbnail: generate_thumbnail.delay(doc_file.id)
def checkin(self, doc_file, new_file, update_attributes=True, thumbnail=True): """ Updates *doc_file* with data from *new_file*. *doc_file*.thumbnail is deleted if it is present. :exceptions raised: * :exc:`ValueError` if *doc_file*.document is not self.object * :exc:`ValueError` if the file size is superior to :attr:`settings.MAX_FILE_SIZE` * :exc:`plmapp.exceptions.UnlockError` if *doc_file* is locked but *doc_file.locker* is not the current user * :exc:`.PermissionError` if :attr:`_user` is not the owner of :attr:`object` * :exc:`.PermissionError` if :attr:`object` is not editable. :param doc_file: :type doc_file: :class:`.DocumentFile` :param new_file: file with new data, same parameter as *f* in :meth:`add_file` :param update_attributes: True if :meth:`handle_added_file` should be called """ self.check_edit_files() if doc_file.document.pk != self.object.pk: raise ValueError("Bad file's document") if doc_file.filename != new_file.name: raise ValueError("Checkin document and document already in plm have different names") if settings.MAX_FILE_SIZE != -1 and new_file.size > settings.MAX_FILE_SIZE: raise ValueError("File too big, max size : %d bytes" % settings.MAX_FILE_SIZE) if doc_file.deprecated: raise ValueError("File is deprecated") doc_file.no_index = True if doc_file.locked: self.unlock(doc_file) now = timezone.now() previous_revision = doc_file.previous_revision doc_file.previous_revision = None doc_file.save() new_file.name = new_file.name.encode("utf-8") deprecated_df = models.DocumentFile.objects.create( document=self.object, deprecated=True, size=doc_file.size, filename=doc_file.filename, file=models.docfs.save(new_file.name, doc_file.file), thumbnail=None, ctime=doc_file.ctime, revision=doc_file.revision, end_time=now, previous_revision=previous_revision, last_revision=doc_file) if doc_file.thumbnail: path = models.thumbnailfs.save("%d.png" % deprecated_df.id, doc_file.thumbnail) deprecated_df.no_index = True deprecated_df.thumbnail = os.path.basename(path) deprecated_df.save() # update the doc_file doc_file.file = models.docfs.save(new_file.name, new_file) doc_file.size = new_file.size doc_file.previous_revision = deprecated_df doc_file.revision += 1 doc_file.ctime = now os.chmod(doc_file.file.path, 0400) doc_file.no_index = False doc_file.save() # delete "old" files (not the document file) self._delete_old_files(doc_file, ON_CHECKIN_SELECTORS) self._save_histo("checked-in ", doc_file.filename) if update_attributes: self.handle_added_file(doc_file) if thumbnail: generate_thumbnail.delay(doc_file.id)