Beispiel #1
0
def init_image():
    user_default = UserProfile.objects.get(email='*****@*****.**')
    cur_sqlite = db_sqlite.cursor()
    cur_sqlite.execute(
        "select name,url_image,url_thumb,width,height,file_type,category_id,tags from image"
    )

    for item in cur_sqlite.fetchall():
        image = Image()
        image.name = item[0]
        image.url = item[1]
        image.url_thumb = item[2]
        image.width = item[3]
        image.height = item[4]
        image.type = item[5].replace('.', '')

        image.user = user_default
        image.save()

        category_id = item[6]
        if category_id != '':
            category = Category.objects.get(pk=int(item[6]))
            category.count += 1
            category.save()
            image.categorys.add(category)

        tags_info = item[7]
        if tags_info != '':
            tags_info = "{'tags':" + tags_info + "}"
            tags_info = tags_info.replace("'tags'", '"tags"') \
                .replace("'name'", '"name"') \
                .replace("'id'", '"id"') \
                .replace(": '", ': "') \
                .replace("', ", '", ') \
                .replace("'}", '"}')
            try:
                tags_info = json.loads(tags_info)
            except Exception as e:
                logging.error(tags_info)
                pass
            try:
                for item in tags_info['tags']:
                    try:
                        tag = Tag.objects.get(name=item['name'])
                        tag.count += 1
                        tag.save()
                        image.tags.add(tag)
                    except Exception as e:
                        tag = Tag()
                        tag.name = item['name']
                        tag.count = 1
                        tag.save()
                        image.tags.add(tag)
            except Exception as e:
                logging.error(tags_info)
                logging.error(e)
Beispiel #2
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,
            })