0

I have two models both having FileField like so:

class OriginalFile(models.Model):
    docfile = models.FileField(upload_to=file_path)

class RevisedFile(models.Model):
    newfile = models.FileField(upload_to=revisedfile_path)
    originalfile = models.ForeignKey(OriginalFile, on_delete=models.PROTECT)

As you see, when uploading a file for the first time, it gets uploaded to file_path in the OriginalFile model. However, if a user uploads a revised edition of the same file, it gets saved to revisedfile_path in the RevisedFile model.

The normal user doesn't have the rights to revise a file by herself(which is why I'm not replacing the original file at that moment). If a manager accepts the revised file, then following things need to happen:

  1. Replace the docfile of OriginalFile with newfile of RevisedFile
  2. Delete the related instance of RevisedFile along with its newfile saved in the directory.

The names of docfile and newfile can be assumed to be the same. I have no idea how to approach this problem. Any kind of help woould be much appreciated. Thanks.

Rahul
  • 502
  • 7
  • 16

1 Answers1

0

Remove any user access to your file system. Use FK instead of manipulating files directly. Do not delete old files, but rather no show the old files.

class DocFile(models.Model):
    file = models.FileField(upload_to=file_path)  # Single path for all doc files

class Document(models.Model):
    # Optional name, description fields 
    approved = models.FK(DocFile)
    revisions = models.ManyToManyField(DocFile)

When normal user uploads a file add it to Document.revisions and if it is the only revision set it as approved as well.

Create a permission group for managers and allowed them to edit Document in admin. This way they can select which of the DocFiles associated with the Document is the approved one. No need to transfer files, you do not lose versions of the file and you file system is safe from rm -r. Manager would also be able to download files for review

Lyudmil Nenov
  • 247
  • 2
  • 6
  • By access to file system, I meant the user can upload a file. Not in any way view/change files or access directory. Also, I figure that going the way you suggested, there'll be many orphaned files, if the users frequently upload revised files. That is one of the things I wanted to avoid. Also, as the revised and original files will have the same name, would that cause any problems? – Rahul Jul 30 '15 at 11:12
  • You can still delete the files from admin or add extra logic to delete revisions when approved is changed. Orphaned files are nothing to be afraid of. – Lyudmil Nenov Jul 30 '15 at 11:22
  • You can add upload time to DocFile and have a cronjob delete files if the approved DocFile is was uploaded last of all the revisions. – Lyudmil Nenov Jul 30 '15 at 16:35
  • Thank you. I'll try this.. However, I found an interesting solution which deletes the file along with the instance.. [link](http://stackoverflow.com/questions/5372934/how-do-i-get-django-admin-to-delete-files-when-i-remove-an-object-from-the-datab) – Rahul Jul 30 '15 at 18:29