Example #1
0
    def update_image(cls, engines):
        for engine in engines:
            data = {'exec': 'images()'}
            response = requests.post(engine.url(), data=data)
            results = response.json()['results']

            for result in results:
                image = Image()
                image.create_at = datetime.fromtimestamp(result['Created'])
                image.uid = result['Id']
                image.size = result['Size']
                image.virtualsize = result['VirtualSize']
                image.tag = json.dumps(result['RepoTags'])
                image.name = result['RepoTags'][0].split(':')[0]
                image.engine = engine
                if image.name == '<none>':
                    image.order = 99
                image.save()
Example #2
0
    def update_image(cls, engines):
        for engine in engines:
            data = {'exec': 'images()'}
            response = requests.post(engine.url(), data=data)
            results = response.json()['results']

            for result in results:
                image = Image()
                image.create_at = datetime.fromtimestamp(result['Created'])
                image.uid = result['Id']
                image.size = result['Size']
                image.virtualsize = result['VirtualSize']
                image.tag = json.dumps(result['RepoTags'])
                image.name = result['RepoTags'][0].split(':')[0]
                image.engine = engine
                if image.name == '<none>':
                    image.order = 99
                image.save()
Example #3
0
    def post(self, request):
        all_category = Category.objects.all()
        images = request.FILES.getlist("image_file")
        categorys = request.POST.getlist("image_category")
        description = request.POST.get("image_description")
        tags = request.POST.get('image_tag', '')
        if tags != '' and tags.find('|'):
            tags = tags.split('|')

        all_file_url = []
        upload_path = os.path.join(settings.MEDIA_ROOT, 'release')
        upload_path_thumb = os.path.join(settings.MEDIA_ROOT, 'release_thumb')
        if images:
            for image in images:
                md5 = hashlib.md5()
                for chrunk in image.chunks():
                    md5.update(chrunk)
                name = image.name
                size = image.size
                type = os.path.splitext(name)[-1]
                md5_name = md5.hexdigest()

                img_path = os.path.join(upload_path, md5_name) + type
                img_path_thumb = os.path.join(upload_path_thumb,
                                              md5_name) + type

                url = os.path.join(settings.MEDIA_URL, 'release',
                                   md5_name) + type
                url_thumb = os.path.join(settings.MEDIA_URL, 'release_thumb',
                                         md5_name) + type

                all_file_url.append(url_thumb)

                img = open(img_path, 'wb')
                for chrunk in image.chunks():
                    img.write(chrunk)
                img.close()

                pimg = PImage.open(img_path)

                _img = Image()
                _img.user = request.user
                _img.name = name
                _img.description = description
                _img.url = url
                _img.url_thumb = url_thumb
                _img.size = size
                _img.width, _img.height = pimg.size
                _img.type = type.strip('.')
                _img.save()

                pimg.thumbnail((300, 300))
                pimg.save(img_path_thumb)

                if len(categorys) == 0:
                    category = Category.objects.get(name='Other')
                    category.count += 1
                    category.save()
                    _img.categorys.add(category)
                else:
                    for category_id in categorys:
                        category = Category.objects.get(pk=category_id)
                        category.count += 1
                        category.save()
                        _img.categorys.add(category)

                if len(tags) == 0:
                    tag = Tag.objects.get(name='Other')
                    tag.count += 1
                    tag.save()
                    _img.tags.add(tag)

                else:
                    for tag_name in tags:
                        tag = None
                        try:
                            tag = Tag.objects.get(name=tag_name)
                        except:
                            pass
                        if tag:
                            tag.count += 1
                            tag.save()
                            _img.tags.add(tag)
                        else:
                            tag = Tag()
                            tag.name = tag_name
                            tag.user = request.user
                            tag.count += 1
                            tag.save()
                            _img.tags.add(tag)
                _img.save()

            return render(
                request, 'user/release.html', {
                    'message': '文件上传成功!',
                    'all_file_url': all_file_url,
                    'all_category': all_category,
                })
        else:
            return render(request, 'user/release.html', {
                'message': '请先选择需要上传的文件!',
                'all_category': all_category,
            })