1

When a user uploads images on to the website they are uploaded to a location specified in MEDIA_ROOT. In my case it is a temporary folder (temp) MEDIA_ROOT = '/opt/myenv/temp'. I would like to have these images transferred from /opt/myenv/temp to /opt/myenv/permanent once the user confirms his identity by signing in.

Below are the steps that I am following, I am stuck in Step 2, can some one please guide me:

1.Appending the Django session id to the image name before storing it in '/opt/myenv/temp'

views.py

def store_data(request):
    thumbnail = request.FILES['myfile']
    file_name = thumbnail.name
    thumbnail.name = file_name + '_' + request.session.session_key
    u = user_info.objects.create(thumbnail=thumbnail)

models.py

class user_info(models.Model):
    thumbnail = models.FileField(upload_to=get_upload_file_name)
    objects = models.Manager()

    def __unicode__(self):
        return self.f_name 

def get_upload_file_name(instance,filename):
    return "temp/%s" % (filename)

2.After the user signs in, I would like to move the images that have the same session id in its name to a new folder 'opt/myenv/permanent' while replacing session_id in the image name with user_name

kurrodu
  • 2,108
  • 4
  • 32
  • 49

2 Answers2

0

Based on the code written by @banerjs, Managed to move files from one folder to another.

views.py

from PIL import Image
from django.conf import settings

def store_data(request):
    thumbnail               = request.FILES['myfile']
    file_name               = thumbnail.name
    thumbnail.name          = file_name + '_' + request.session.session_key
    session_id              = request.session.session_key                       # Add session_id to the table
    request.session['s_id'] = session_id                                        # Store session_id in session
    u                       = user_info.objects.create(thumbnail=thumbnail, session_id=session_id)

def move_image(request):
    session_id          = request.session['s_id']
    image               = user_info.objects.get(session_id__exact=session_id)                 
    image_path          = settings.MEDIA_ROOT + str(image.thumbnail)                                     
    im                  = Image.open(settings.MEDIA_ROOT + str(image.thumbnail))                         
    newpath             = 'permanent/' + str(image.thumbnail).split('/', 1)[1]                    
    im.save(settings.MEDIA_ROOT + newpath)                                                          
    r                   = user_info_confirmed.objects.create(thumbnail=newpath)

models.py

class user_info(models.Model):
    thumbnail = models.FileField(upload_to="temp/")
    session_id = models.CharField(max_length =100, blank=True, null=True) 
    objects = models.Manager()

class user_info_confirmed(models.Model):
    thumbnail = models.FileField(upload_to="permanent/")
    objects = models.Manager()
Community
  • 1
  • 1
kurrodu
  • 2,108
  • 4
  • 32
  • 49
-1

@ server you must give permissions "a+RW" to /opt/myenv/temp and /opt/myenv/permanent. Then follow below link.

http://docs.nullpobug.com/django/trunk/django.core.files.move-module.html