[Answered ]-Automatically update images

2๐Ÿ‘

โœ…

My advice is running django script in a crontab job, lets say, 5 in 5 minutes.

The script would dive into the image folders and compare the images with the records.

A simplified example:

# Set up the Django Enviroment
from django.core.management import setup_environ 
import settings 
setup_environ(settings)
import os
from your_project.your_app.models import *
from datetime import datetime

vehicles_root = '/home/vehicles'
for stock_number in os.listdir(vehicles_root):
    cur_path = vehicles_root+'/'+stock_number
    if not os.path.isdir(cur_path):
        continue # skip non dirs
    for file in os.listdir(cur_path):
        if not isfile(cur_path+'/'+file):
            continue # skip non file
        ext = file.split('.')[-1]
        if ext.lower() not in ('png','gif','jpg',):
            continue # skip non image
        last_mod = os.stat(cur_path+'/'+file).st_mtime
        v = Vehicle.objects.get(stock_number=stock_number)
        if v.last_upd < datetime.fromtimestamp(last_mod):
            # do your magic here, move image, etc.
            v.last_upd = datetime.now()
            v.save()
๐Ÿ‘คPaulo Scardine

Leave a comment