Пример #1
0
def addon_image_uploader(request, **kwargs):
    uploader = AjaxUploader(
        filetype="image",
        uploadDirectory=setting("IMAGE_UPLOAD_DIR", "images"),
        sizeLimit=setting("IMAGE_UPLOAD_SIZE", 10485760),
    )
    result = uploader.handleUpload(request)
    if result["success"]:
        new = Pic()
        new.content_type = get_object_or_404(ContentType, id=int(kwargs["content_type"]))
        new.object_id = int(kwargs["object_id"])
        new.description = result["old_filename"]
        new.user = request.user
        new.created_date = now()
        new.pic = result["path"]
        fullpath = os.path.join(settings.MEDIA_ROOT, new.pic.field.upload_to, new.pic.path)
        new.size = os.path.getsize(fullpath)
        new.save()
        try:
            pics_count = dict(pics_count=new.content_object.pics_count)
            result.update(pics_count)
        except:
            pass
        try:
            addons = dict(html=render_to_string("upload/image_item.html", {"pic": new}))
        except:
            addons = {}
        result.update(addons)
    return AjaxAnswer(result)
Пример #2
0
def addon_image_uploader(request, **kwargs):
    uploader = AjaxUploader(filetype='image', upload_dir=setting('IMAGE_UPLOAD_DIR', 'images'),
                            size_limit=setting('IMAGE_UPLOAD_SIZE', 10485760))
    result = uploader.handle_upload(request)
    if result['success']:
        new = Pic()
        new.content_type = get_object_or_404(ContentType, id=int(kwargs['content_type']))
        new.object_id = int(kwargs['object_id'])
        new.description = result['old_filename']
        new.user = request.user
        new.created_date = now()
        new.pic = result['path']
        fullpath = os.path.join(settings.MEDIA_ROOT,
                                new.pic.field.upload_to, new.pic.path)
        new.size = os.path.getsize(fullpath)
        new.save()
        try:
            pics_count = dict(pics_count=new.content_object.pics_count)
            result.update(pics_count)
        except:
            pass
        try:
            addons = dict(html=render_to_string('upload/image_item.html', {'pic': new}))
        except:
            addons = {}
        result.update(addons)
    return AjaxAnswer(result)
Пример #3
0
 def _ajax_upload(self, request, **kwargs):
     if request.method == "POST":
         self._upload_file(request, **kwargs)
         fullpath = os.path.join(settings.MEDIA_ROOT,
                                 self.extra_context['path'])
         try:
             i = Image.open(fullpath)
         except:
             messages.error(request, "File is not image format")
             os.remove(fullpath)
             self.success = False
             self.pic_id = None
             addons = None
         if self.success:
             new = Pic()
             self._new_obj(new, request, **kwargs)
             new.pic = self.extra_context['path']
             fullpath = os.path.join(settings.MEDIA_ROOT,
                                     new.pic.field.upload_to, new.pic.path)
             new.size = os.path.getsize(fullpath)
             new.save()
             self.pic_id = new.pk
             # let Ajax Upload know whether we saved it or not
             addons = {'size': os.path.getsize(fullpath),
                       'thumbnail': make_thumbnail(new.pic.url, width=settings.DEFAULT_THUMBNAIL_WIDTH,
                                                   height=settings.DEFAULT_THUMBNAIL_HEIGHT, aspect=1)}
         payload = {'success': self.success, 'filename': self.filename, 'id': self.pic_id}
         if self.extra_context is not None:
             payload.update(self.extra_context)
         if addons:
             payload.update(addons)
         return AjaxAnswer(payload)
Пример #4
0
 def _ajax_upload(self, request, **kwargs):
     if request.method == "POST":
         self._upload_file(request, **kwargs)
         fullpath = os.path.join(settings.MEDIA_ROOT, self.extra_context["path"])
         try:
             i = Image.open(fullpath)
         except:
             messages.error(request, "File is not image format")
             os.remove(fullpath)
             self.success = False
             self.pic_id = None
             addons = None
         if self.success:
             new = Pic()
             self._new_obj(new, request, **kwargs)
             new.pic = self.extra_context["path"]
             fullpath = os.path.join(settings.MEDIA_ROOT, new.pic.field.upload_to, new.pic.path)
             new.size = os.path.getsize(fullpath)
             new.save()
             self.pic_id = new.pk
             # let Ajax Upload know whether we saved it or not
             addons = {
                 "size": os.path.getsize(fullpath),
                 "thumbnail": make_thumbnail(new.pic.url, width=settings.DEFAULT_UPLOAD_THUMBNAIL_SIZE),
             }
         payload = {"success": self.success, "filename": self.filename, "id": self.pic_id}
         if self.extra_context is not None:
             payload.update(self.extra_context)
         if addons:
             payload.update(addons)
         return AjaxAnswer(payload)
Пример #5
0
 def _ajax_upload(self, request, **kwargs):
     if request.method == "POST":
         self._upload_file(request, **kwargs)
         fullpath = os.path.join(settings.MEDIA_ROOT,
                                 self.extra_context['path'])
         try:
             i = Image.open(fullpath)
         except:
             messages.error(request, "File is not image format")
             os.remove(fullpath)
             self.success = False
             self.pic_id = None
         if self.success:
             try:
                 request.user.img.delete()
             except:
                 pass
             new = Pic()
             new.content_type = ContentType.objects.get_for_model(get_user_model())
             new.object_id = request.user.pk
             new.description = self.extra_context['oldname']
             new.user = request.user
             new.created_date = now()
             new.pic = self.extra_context['path']
             new.save()
             request.user.img = new
             request.user.save()
             self.pic_id = new.pk
             # let Ajax Upload know whether we saved it or not
         payload = {'success': self.success, 'filename': self.filename, 'id': self.pic_id}
         if self.extra_context is not None:
             payload.update(self.extra_context)
         return AjaxAnswer(payload)
Пример #6
0
 def _ajax_upload(self, request, **kwargs):
     if request.method == "POST":
         self._upload_file(request, **kwargs)
         fullpath = os.path.join(settings.MEDIA_ROOT, self.extra_context["path"])
         try:
             i = Image.open(fullpath)
         except:
             messages.error(request, "File is not image format")
             os.remove(fullpath)
             self.success = False
             self.pic_id = None
         if self.success:
             try:
                 request.user.get_profile().avatar.delete()
             except:
                 pass
             new = Pic()
             new.content_type = ContentType.objects.get_for_model(settings.AUTH_USER_MODEL)
             new.object_id = request.user.pk
             new.description = self.extra_context["oldname"]
             new.user = request.user
             new.created_date = datetime.now()
             new.pic = self.extra_context["path"]
             new.save()
             request.user.get_profile().avatar = new
             request.user.get_profile().save()
             self.pic_id = new.pk
             # let Ajax Upload know whether we saved it or not
         payload = {"success": self.success, "filename": self.filename, "id": self.pic_id}
         if self.extra_context is not None:
             payload.update(self.extra_context)
         return AjaxAnswer(payload)
Пример #7
0
 def _ajax_upload(self, request, **kwargs):
     if request.method == "POST":
         self._upload_file(request, **kwargs)
         fullpath = os.path.join(settings.MEDIA_ROOT,
                                 self.extra_context['path'])
         try:
             i = Image.open(fullpath)
         except:
             messages.error(request, "File is not image format")
             os.remove(fullpath)
             self.success = False
             self.pic_id = None
         if self.success:
             try:
                 request.user.img.delete()
             except:
                 pass
             new = Pic()
             new.content_type = ContentType.objects.get_for_model(
                 get_user_model())
             new.object_id = request.user.pk
             new.description = self.extra_context['oldname']
             new.user = request.user
             new.created_date = now()
             new.pic = self.extra_context['path']
             new.save()
             request.user.img = new
             request.user.save()
             self.pic_id = new.pk
             # let Ajax Upload know whether we saved it or not
         payload = {
             'success': self.success,
             'filename': self.filename,
             'id': self.pic_id
         }
         if self.extra_context is not None:
             payload.update(self.extra_context)
         return AjaxAnswer(payload)
Пример #8
0
 def _ajax_upload(self, request, **kwargs):
     if request.method == "POST":
         self._upload_file(request, **kwargs)
         fullpath = os.path.join(settings.MEDIA_ROOT,
                                 self.extra_context['path'])
         try:
             i = Image.open(fullpath)
         except:
             messages.error(request, "File is not image format")
             os.remove(fullpath)
             self.success = False
             self.pic_id = None
             addons = None
         if self.success:
             new = Pic()
             self._new_obj(new, request, **kwargs)
             new.pic = self.extra_context['path']
             fullpath = os.path.join(settings.MEDIA_ROOT,
                                     new.pic.field.upload_to, new.pic.path)
             new.size = os.path.getsize(fullpath)
             new.save()
             self.pic_id = new.pk
             # let Ajax Upload know whether we saved it or not
             addons = {
                 'size':
                 os.path.getsize(fullpath),
                 'thumbnail':
                 make_thumbnail(new.pic.url,
                                width=settings.DEFAULT_THUMBNAIL_WIDTH,
                                height=settings.DEFAULT_THUMBNAIL_HEIGHT,
                                aspect=1)
             }
         payload = {
             'success': self.success,
             'filename': self.filename,
             'id': self.pic_id
         }
         if self.extra_context is not None:
             payload.update(self.extra_context)
         if addons:
             payload.update(addons)
         return AjaxAnswer(payload)
Пример #9
0
 def create_pic(self, target, pic_file):
     img = Pic(content_object=target)
     img.pic.save(pic_file.name, pic_file)
     img.save()
     return img
Пример #10
0
def addon_image_uploader(request, **kwargs):
    uploader = AjaxUploader(filetype='image',
                            uploadDirectory=setting('IMAGE_UPLOAD_DIR',
                                                    'images'),
                            sizeLimit=setting('IMAGE_UPLOAD_SIZE', 10485760))
    result = uploader.handleUpload(request)
    if result['success']:
        new = Pic()
        new.content_type = get_object_or_404(ContentType,
                                             id=int(kwargs['content_type']))
        new.object_id = int(kwargs['object_id'])
        new.description = result['old_filename']
        new.user = request.user
        new.created_date = now()
        new.pic = result['path']
        fullpath = os.path.join(settings.MEDIA_ROOT, new.pic.field.upload_to,
                                new.pic.path)
        new.size = os.path.getsize(fullpath)
        new.save()
        try:
            pics_count = dict(pics_count=new.content_object.pics_count)
            result.update(pics_count)
        except:
            pass
        try:
            addons = dict(
                html=render_to_string('upload/image_item.html', {'pic': new}))
        except:
            addons = {}
        result.update(addons)
    return AjaxAnswer(result)
Пример #11
0
 def create_pic(self, target, pic_file):
     img = Pic(content_object=target)
     img.pic.save(pic_file.name, pic_file)
     img.save()
     return img