自动更新图像

发布于 2024-09-19 09:28:28 字数 1597 浏览 6 评论 0原文

我想在我的应用程序中实现一项功能,但我不知道如何去做。我想要的是:我有一个使用 imagekit 保存其图像的模型类,我希望用户能够轻松更新车辆的图像,而无需编辑每个相应的车辆记录。

他们的做法是,会有一个名为 originals 的文件夹,其中包含每辆车的文件夹,格式为 /PUBLIC 如果用户将图像移动到车辆的 PUBLIC 文件夹中,执行脚本时,会将这些图像与当前图像进行比较,如果 PUBLIC 文件夹中的图像存在,则更新它们较新。如果记录没有图像,则会添加图像。另外,如果图像已从 site_media 目录中删除,则它们的链接也应从数据库中删除。

我怎样才能有效地解决这个问题?我的模型如下:

class Photo(ImageModel):
   name = models.CharField(max_length = 100)
   original_image = models.ImageField(upload_to = 'photos')
   num_views = models.PositiveIntegerField(editable = False, default=0)
   position = models.ForeignKey(PhotoPosition)
   content_type = models.ForeignKey(ContentType)
   object_id = models.PositiveIntegerField()
   content_object = generic.GenericForeignKey('content_type', 'object_id')

   class IKOptions:
      spec_module = 'vehicles.specs'
      cache_dir = 'photos'
      image_field = 'original_image'
      save_count_as = 'num_views'


class Vehicle(models.Model):
   objects = VehicleManager()
   stock_number = models.CharField(max_length=6, blank=False, unique=True)
   vin = models.CharField(max_length=17, blank=False)
   ....
   images = generic.GenericRelation('Photo', blank=True, null=True)

进度更新 我已经尝试了该代码,虽然它有效,但我缺少一些东西,因为我可以获得图像,但在那之后,它们不会传输到 site_media/photos 目录中...我应该这样做还是 imagekit 会自动执行此操作?我有点困惑。

我像这样保存照片:

Photo.objects.create(content_object = vehicle, object_id = vehicle.id, 
                     original_image = file)

I'd like to implement a functionality in an app of mine, but I don't know how to go about it. What I want is this: I have a model class that uses imagekit to save its images, and I'd like to have the users being able to update the images easily for the vehicles without having to edit each respective vehicle record.

How they'll do this is that there will be a folder named originals and it'll contain folders for each vehicle in the format <stock_number>/PUBLIC If a user moves images into the PUBLIC folder for a vehicle, when the script is executed, it'll compare those images with the current ones and update them if those in the PUBLIC folder are newer. If the record has no images, then they will be added. Also, if the images have been deleted from the site_media directory, then their links should be deleted from the database.

How can I go about this in an efficient way? My models are as below:

class Photo(ImageModel):
   name = models.CharField(max_length = 100)
   original_image = models.ImageField(upload_to = 'photos')
   num_views = models.PositiveIntegerField(editable = False, default=0)
   position = models.ForeignKey(PhotoPosition)
   content_type = models.ForeignKey(ContentType)
   object_id = models.PositiveIntegerField()
   content_object = generic.GenericForeignKey('content_type', 'object_id')

   class IKOptions:
      spec_module = 'vehicles.specs'
      cache_dir = 'photos'
      image_field = 'original_image'
      save_count_as = 'num_views'


class Vehicle(models.Model):
   objects = VehicleManager()
   stock_number = models.CharField(max_length=6, blank=False, unique=True)
   vin = models.CharField(max_length=17, blank=False)
   ....
   images = generic.GenericRelation('Photo', blank=True, null=True)

Progress Update
I've tried out the code, and while it works, I'm missing something as I can get the image, but after that, they aren't transferred into the site_media/photos directory...am I suppossed to do this or imagekit will do this automatically? I'm a bit confused.

I'm saving the photos like so:

Photo.objects.create(content_object = vehicle, object_id = vehicle.id, 
                     original_image = file)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

为人所爱 2024-09-26 09:28:29

我的建议是在 crontab 作业中运行 django 脚本,比如说 5 分钟内运行 5 个脚本。

该脚本将深入图像文件夹并将图像与记录进行比较。

一个简化的例子:

# 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()

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()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文