Beispiel #1
0
    def test_content_property(self):
        test_data = 'http://blogthumb2.naver.net/20160302_285/mardukas_1456922688406bYGAH_JPEG/DSC07301.jpg'
        img, is_created = Image.get_or_create_smart(test_data)
        saved = Image.objects.first()
        url = test_data
        self.assertEqual(img.content, url)
        self.assertEqual(saved, img)
        self.assertEqual(saved.content, img.content)

        img2 = Image()
        img2.content = 'http://static.naver.net/www/mobile/edit/2016/0407/mobile_17004159045.png'
        img2.save()
        img2.summarize()
        self.assertNotEqual(img2, img)

        img3 = Image()
        img3.content = 'http://static.naver.net/www/mobile/edit/2016/0407/mobile_17004159045.png'
        img3.save()
        img3.summarize()
        self.assertEqual(img3, img2)

        rf4 = RawFile()
        rf4.file = self.uploadFile('test.jpg')
        rf4.save()
        img4, is_created = Image.get_or_create_smart(rf4.url)
        self.assertNotEqual(img4.content, rf4.url)
        self.assertEqual(img4.url_for_access, rf4.url)
        self.assertEqual(img4.url_for_access.endswith(img4.content), True)
Beispiel #2
0
    def create(self, validated_data):
        # create an image object
        image = Image(**validated_data)

        if validated_data.get("base_64", None):
            file_type = get_file_type(validated_data["base_64"])
            if not file_type:
                raise serializers.ValidationError(
                    {"error": "Image cannot be decoded"})
            else:
                image.extension = file_type
        if validated_data.get("remote_location", None):
            image_check = valid_remote_image(validated_data["remote_location"])
            if not image_check:
                raise serializers.ValidationError(
                    {"error": "Image cannot be downloaded"})
            else:
                success, file_type, base_64 = image_check
                image.base_64 = base_64
                image.extension = file_type
                validated_data["base_64"] = image.base_64

        validated_data["extension"] = image.extension

        image.save()
        # return the correct uuid for the API response
        validated_data["id"] = image.id
        f_data = ContentFile(base64.b64decode(image.base_64))
        image.raw_file.save(f"file_name.{image.extension}", f_data)
        validated_data["raw_file"] = image.raw_file
        convert.delay(image.id)
        return Image(**validated_data)
Beispiel #3
0
    def post(self, request):
        data = json.loads(request.body)

        try:
            images = dict()
            for index, image in enumerate(data['images']):
                images[index] = image
            image = Image(
                    image_1 = images.get(0),
                    image_2 = images.get(1),
                    image_3 = images.get(2),
                    image_4 = images.get(3),
                    image_5 = images.get(4)
                )
            image.save()

            product  = Product(
                seller   = request.user,
                title    = data['title'],
                content  = data['content'],
                price    = data['price'],
                places   = data['places'],
                image    = image,
                category = self.categories.get(id = data['category'])
                )
            product.save()

            return JsonResponse({'product_id':product.id}, status = 201)

        except KeyError:
            return JsonResponse({'message':'INVALID_KEYS'}, status = 400)
Beispiel #4
0
    def step_07_fourth_clustering_by_geography_distance(self):
        for threshold in xrange(1, CLUSTERING_MIN_DISTANCE_THRESHOLD + 1):
            #print('threshold = %d' % threshold)
            prev_group_cnt = 0
            for iteration in xrange(10):
                group_cnt = 0

                for group1 in self.result:
                    img = Image()
                    img.lonLat = group1.lonLat
                    m_value = Group()
                    m_value.members = [Group()]
                    m_value.members[0].members = [img]
                    cluster = Clustering(group1, threshold,
                                         distance_geography_group, m_value,
                                         False)
                    cluster.run()
                    group1.members = [
                        Group(
                            sum([group3.members
                                 for group3 in group2.members], []))
                        for group2 in cluster.result
                    ]
                    group_cnt += len(group1.members)

                #print('step_07:group_cnt == %d' % group_cnt)
                if prev_group_cnt == group_cnt:
                    break
                else:
                    prev_group_cnt = group_cnt
        return True
Beispiel #5
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)
def save_images(request):
    try:
        for image in request.FILES.values():
            im = Image(image=image)
            im.save()
        return HttpResponse()
    except ValidationError as e:
        return HttpResponse(content=e, status=500)
    except ValueError as e:
        return HttpResponse(content=e, status=415)
Beispiel #7
0
def uploadImage(request):
    global is_error, myfile
    is_error = 0
    if request.user.is_authenticated():
        if request.user.is_superuser:
            if request.POST:
                libelle = request.POST["libelle"]
                type = request.POST["type"]
                description = request.POST["descript"]
                save_plus = request.POST.getlist('save_and')
                if libelle == "":
                    error_libelle = "veuillez remplir ce champs"
                    is_error = 1
                if int(type) == 0:
                    error_type = "veuillez selectionner un type"
                    is_error = 1
                try:
                    myfile = request.FILES['image']
                except:
                    error_logo = "veuillez selectionner une image"
                    is_error = 1
                if is_error != 0:
                    return render(request, 'uploadImage.html', locals())
                else:
                    save_path = settings.MEDIA_ROOT
                    last = myfile.name[myfile.name.rfind("."):len(myfile.name)]
                    fs = FileSystemStorage()
                    currentId = 0
                    try:
                        image = Image.objects.last()
                        currentId = image.idImage + 1
                    except:
                        currentId = 1
                    save_name = "teranga-food-upload-0" + str(currentId) + last
                    fs.save(settings.MEDIA_ROOT + save_name, myfile)
                    myImage = Image()
                    myImage.description = description
                    myImage.libelle = libelle
                    myImage.type = int(type)
                    myImage.saveName = "/media/" + save_name
                    myImage.save()
                    try:
                        _next = int(save_plus[0])
                    except:
                        _next = 0
                    if _next == 0:
                        return redirect(listeImage)
                    else:
                        libelle = ""
                        description = ""
                        return render(request, 'uploadImage.html', locals())
            else:
                return render(request, 'uploadImage.html', locals())
Beispiel #8
0
def create_image(cp, xml, itype, name, version, snap, desc='', enable=True):  
    i = Image()
    i.cephpool = cp
    i.name = name
    i.version = version
    i.snap = snap
    i.desc = desc
    i.xml = xml
    i.type = itype
    i.enable = enable
    i.save()
    return i
Beispiel #9
0
 def setUp(self):
     super(ImageViewsetTest, self).setUp()
     response = self.client.post('/users/register/')
     self.auth_user_token = json_loads(response.content)['auth_user_token']
     self.client.post('/users/login/', {'auth_user_token': self.auth_user_token})
     response = self.client.post('/vds/register/', dict(email='*****@*****.**'))
     self.auth_vd_token = json_loads(response.content)['auth_vd_token']
     self.client.post('/vds/login/', {'auth_vd_token': self.auth_vd_token})
     self.img = Image()
     self.img.content = 'http://blogthumb2.naver.net/20160302_285/mardukas_1456922688406bYGAH_JPEG/DSC07301.jpg'
     self.img.save()
     self.content2 = 'http://blogpfthumb.phinf.naver.net/20100110_16/mardukas_1263055491560_VI01Ic_JPG/DSCN1968.JPG'
Beispiel #10
0
    def handle(self, *args, **options):
        # search
        url, credit_url = search('cat')

        # wget
        filename = '/dev/shm/' + hashlib.md5(str(
            random.random()).encode()).hexdigest() + '.gif'
        try:
            subprocess.getoutput('wget %s -q --timeout 20 -t 2 -O %s' %
                                 (url, filename))
        except:
            self.stderr.write(
                self.style.ERROR('Fail to download. URL: %s' % url))
            sys.exit()

        # thumbnail
        thumbnail_filename = convert(filename)
        if thumbnail_filename != -1:
            pass
        else:
            self.stderr.write(self.style.ERROR('Fail to convert.'))
            sys.exit()

        # upload2weibo
        original_url = upload(filename)
        if original_url != -1:
            pass
        else:
            self.stderr.write(self.style.ERROR('Fail to upload gif.'))
            sys.exit()

        thumbnail_url = upload(thumbnail_filename)
        if thumbnail_url != -1:
            pass
        else:
            self.stderr.write(self.style.ERROR('Fail to upload jpg.'))
            sys.exit()

        new_cat = Image(name='CatHub',
                        original_url=original_url,
                        thumbnail_url=thumbnail_url,
                        pub_date=timezone.now(),
                        oo_num=0,
                        xx_num=0,
                        comment_num=0,
                        legal=True,
                        credit_url=credit_url)
        new_cat.save()
        subprocess.getoutput('rm ' + filename)
        subprocess.getoutput('rm ' + thumbnail_filename)

        self.stdout.write(self.style.SUCCESS('Successfully add a cat.'))
Beispiel #11
0
    def create(self, request, *args, **kwargs):
        serializer = self.get_serializer(data=request.data)
        serializer.is_valid(raise_exception=True)

        try:
            shared_post = Post.objects.get(id=self.request.data['shared_post'])
            new_post = serializer.save(posted_by=self.request.user.profile, shared_post=shared_post)

            if request.FILES:
                for file in request.FILES.getlist('image'):
                    new_file = Image(image=file, post=new_post)
                    new_file.save()

        except MultiValueDictKeyError:
            new_post = serializer.save(posted_by=self.request.user.profile)

            if request.FILES:
                for file in request.FILES.getlist('image'):
                    new_file = Image(image=file, post=new_post)
                    new_file.save()

        return Response(serializer.data, status=status.HTTP_201_CREATED)
Beispiel #12
0
    def test_save_and_retreive(self):
        img = Image()
        test_data = 'http://blogthumb2.naver.net/20160302_285/mardukas_1456922688406bYGAH_JPEG/DSC07301.jpg'
        img.content = test_data
        img.save()
        saved = Image.objects.first()

        self.assertEqual(saved, img)
        saved2 = Image.get_from_json('{"uuid": "%s", "content": null}' % img.uuid)
        self.assertEqual(saved2, img)
        saved3 = Image.get_from_json('{"uuid": "%s", "content": null, "note": {"uuid": null, "content": null}}' % img.uuid)
        self.assertEqual(saved3, img)
        saved4 = Image.get_from_json('{"uuid": null, "content": "%s"}' % img.content)
        self.assertEqual(saved4, img)
Beispiel #13
0
def download_blog_images(group_id, group_key, blog_ct, writer_ct):
    """
    非同期でブログ画像をダウンロード、旧imgScraper.update()
    :param group_id:
    :param group_key:
    :param blog_ct:
    :param writer_ct:
    :return:
    """
    try:
        blog = Blog.objects.get(publishing_group__group_id=group_id,
                                blog_ct=blog_ct)
        img_urls = otapick.BlogImageCrawler().crawl(group_key=group_key,
                                                    blog_ct=blog_ct)

        # crawl error
        if img_urls is None:
            pass
        # image not found
        elif len(img_urls) == 0:
            pass
        else:
            # clear halfway remaining images
            Image.objects.filter(publisher=blog).delete()
            order = 0
            for i, img_url in enumerate(img_urls):
                media = otapick.BlogImageDownloader().download(
                    img_url, group_id, blog_ct, writer_ct)
                if media == 'not_image':  # exclude gif
                    pass
                elif media is not None:
                    if not Image.objects.filter(order=i,
                                                publisher=blog).exists():
                        image = Image(
                            order=order,
                            picture=media,
                            publisher=blog,
                        )
                        image.save()
                        otapick.compress_blog_image(image)
                        order += 1
                # image download failed
                else:
                    import traceback
                    traceback.print_exc()

    # timeout(60s)後の処理
    except SoftTimeLimitExceeded:
        pass
Beispiel #14
0
def index(request):
    if request.POST:
        image_form = ImageForm(request.POST, request.FILES)
        if image_form.is_valid():
            image = Image()
            image.image = image_form.cleaned_data['image']
            image.filename = image_form.cleaned_data['image'].name
            image.user_id = request.user.id
            image.save()
    else:
        image_form = ImageForm()

    images = Image.objects.filter(user_id=request.user.id)
    return render(request, 'image/index.html', {'image_form': image_form,
                                                'images': images})
Beispiel #15
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()
Beispiel #16
0
def image_handler(files, request):
    tmp = tempfile.mkstemp()
    md5 = hashlib.md5()
    if request.POST['upload_type'] == 'file':
        orig = files.name
        fext = orig[orig.rfind('.') + 1:]
        f = os.fdopen(tmp[0], "wb+")
        for chunk in files.chunks():
            f.write(chunk)
            md5.update(chunk)
        f.close()
    elif request.POST['upload_type'] == 'url':
        md5.update(files)
        fext = request.POST['upload_url'][-3:]
        orig = request.POST['upload_url']

        f = os.fdopen(tmp[0], "wb+")
        f.write(files)
        f.close()

    img = Image()
    try:
        next_id = Image.objects.order_by('-id')[0].id + 1
    except IndexError:
        next_id = settings.IMAGE_ID_OFFSET + 1

    img.id = next_id
    img.base62 = base62(next_id)
    img.filename = base62(next_id) + "." + fext.lower()
    img.orig_filename = orig
    img.type = ''  # todo
    img.description = ''  # not implemented yet.
    img.uploader = request.user
    img.md5sum = md5.hexdigest()
    image_file = os.path.join(settings.MEDIA_ROOT, img.filename)
    thumbnail = os.path.join(settings.MEDIA_ROOT, 'thumbs', img.filename)

    try:
        img.save()
    except IntegrityError, e:
        os.unlink(tmp[1])  # delete the uploaded file if it already exists
        return HttpResponseRedirect(settings.MEDIA_URL + Image.objects.get(
            md5sum=img.md5sum).filename)
Beispiel #17
0
    def handle(self, *args, **options):
        filename = options['filename&name'][0]
        name = options['filename&name'][1]

        # generate thumbnail
        thumbnail_filename = convert(filename)
        if thumbnail_filename != -1:
            pass
        else:
            self.stderr.write(self.style.ERROR('Fail to convert.'))
            raise CommandError('无法解析该图片')

        # upload2weibo
        original_url = upload(filename)
        if original_url != -1:
            pass
        else:
            self.stderr.write(self.style.ERROR('Fail to upload gif.'))
            raise CommandError('无法上传原图到微博图床')

        thumbnail_url = upload(thumbnail_filename)
        if thumbnail_url != -1:
            pass
        else:
            self.stderr.write(self.style.ERROR('Fail to upload jpg.'))
            raise CommandError('无法上传缩略图到微博图床')

        new_cat = Image(name=name,
                        original_url=original_url,
                        thumbnail_url=thumbnail_url,
                        pub_date=timezone.now(),
                        oo_num=0,
                        xx_num=0,
                        comment_num=0)
        new_cat.save()
        subprocess.getoutput('rm ' + filename)
        subprocess.getoutput('rm ' + thumbnail_filename)

        self.stdout.write(self.style.SUCCESS('.%s.' % new_cat.id))
Beispiel #18
0
    def step_06_third_clustering_by_hamming_distance(self):
        from uuid import UUID

        def distance_hamming_group(g1, g2):
            distances = [
                img1.pd_hamming_distance(img2) for img1 in g1.members
                for img2 in g2.members
            ]
            return min(distances)

        group_cnt = 0
        for group1 in self.result:
            img = Image()
            img.phash = UUID(b'0' * 32)
            img.dhash = 0
            m_value = Group()
            m_value.members = [img]
            cluster = Clustering(group1, IMG_PD_HDIST_THREASHOLD,
                                 distance_hamming_group, m_value, False)
            cluster.run()
            group1.members = cluster.result
            group_cnt += len(cluster.result)
        print('step_06:group_cnt == %d' % group_cnt)
        return True
Beispiel #19
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,
            })
Beispiel #20
0
    def handle(self, *args, **options):
        Model = common.get_image_model(options)

        # get the model dir path
        # Mad Django
        storage = Model._meta.get_field('src').storage
        location = Path(storage.location)
        file_dir = Path(storage.path(Model.upload_dir))
        fl = self.originals_fp_list(file_dir)

        #print(str(options))
        #print(str([p.name for p in dbl]))
        #raise Exception()

        if options['remove_orphaned_models']:
            dbl = self.db_image_list(Model, location)
            model_no_file = [m for m in dbl if not (m['full_path'] in fl)]
            pks = [m['pk'] for m in model_no_file]
            r = Image.objects.filter(pk__in=pks).delete()
            if (options['verbosity'] > 0):
                print("{} model(s) deleted".format(r[0]))
            if (options['verbosity'] > 2):
                for m in model_no_file:
                    print(m['src'])

        elif options['remove_orphaned_files']:
            count = 0
            fail = []
            dbl = self.db_image_fp_list(Model, location)
            file_no_model = set(fl) - set(dbl)
            for path in file_no_model:
                try:
                    path.unlink()
                    count += 1
                except Exception:
                    fail.append(path.stem)
            if (options['verbosity'] > 0):
                print("{} image(s) deleted".format(count))
                if (len(fail) > 0):
                    print("{} image(s) failed to delete. Path: '{}'".format(
                        len(fail),
                        "', '".join(fail),
                    ))
        elif options['add_orphaned_files']:
            # Add models for orphaned files
            count = 0
            fail = []
            dbl = self.db_image_fp_list(Model, location)
            file_no_model = set(fl) - set(dbl)
            for path in file_no_model:
                # Make new models.
                # Worth explaining:
                # How do we register orphaned files?
                # When the model is saved, the generated FieldFile will
                # not recognise the file is stored and attempt a copy
                # onto itself. This should fail silently, in linux,
                # but Django throws a FileExistsError, then makes a
                # renamed copy.
                # Preferebly, we would tell FieldFile the storage is
                # committed. Interesingly, a simple string path, as
                # opposed to an open File or Imagefile, assumes the
                # image is commited.
                #
                # Presuming files in /media are already truncated by
                # configuration.
                # The update effect in Imagefields will not work with
                # Paths, only strings. Also, it works relative to /media
                # (so path relativization here)
                i = Image(src=str(file_dir / path.name), )
                try:
                    i.save()
                    count += 1
                except Exception:
                    fail.append(path.stem)

            if (options['verbosity'] > 0):
                print("{} image(s) created".format(count))
                if (len(fail) > 0):
                    print("{} image(s) failed import. Path: '{}'".format(
                        len(fail),
                        "', '".join(fail),
                    ))
        else:
            # status report
            if (options['verbosity'] > 0):
                header = "SYNC STATUS: {}".format(Model.__name__)
                print("-" * (len(header) + 1))
                print(header)
                print("-" * (len(header) + 1))
            model_count = self.db_image_count(Model)
            file_count = len(fl)
            if ((model_count != file_count) or options['verbosity'] > 0):
                print("model_count: {}".format(model_count))
                print("file_count: {}".format(file_count))
            if (model_count > file_count):
                print(
                    '[warning] files appear to be missing. Try --remove-orphaned-models'
                )
            elif (file_count > model_count):
                print(
                    '[warning] models appear to be missing. Try --add-orphaned-files or --remove-orphaned-files'
                )
            else:
                print('** No sync issues detected')
Beispiel #21
0
 def setUp(self):
     self.c = Client()
     im = Image(image=File(open("image/test_image.jpeg", "rb")))
     im.save()
Beispiel #22
0
def exe_registration(blog_info_list, post_date, group_id, all_check, tweet, console):
    """
    ブログの登録処理
    Args:
        blog_info_list (list): ブログ情報のリスト。前提としてリストの中のブログは同じpost_dateを持つ。
        post_date (date): 共通のpost_date
        group_id (int): グループID
        all_check (bool): 保存済みのブログを見つけても処理を実行
        tweet (bool): 更新通知をtweetするか否か
        console (bool): ログ出力するか否か
    Returns:
        True(登録処理終了), False(登録処理続行)
    """
    download_count = 0
    blog_objects = []
    image_objects = []

    for i, blog_info in enumerate(blog_info_list):
        # new blog
        if not Blog.objects.filter(blog_ct=blog_info['blog_ct'], publishing_group__group_id=group_id).exists():
            blog = Blog(
                blog_ct=blog_info['blog_ct'],
                title=blog_info['title'],
                post_date=post_date,
                order_for_simul=i,
                writer=blog_info['member'],
                publishing_group=Group.objects.filter(
                    group_id=group_id).first(),
            )
            blog_objects.append(blog)
            download_count += 1
        # already saved
        else:
            blog = Blog.objects.get(
                blog_ct=blog_info['blog_ct'], publishing_group__group_id=group_id)

        if len(blog_info['image_urls']) > 0:
            order = 0
            for image_url in blog_info['image_urls']:
                if not Image.objects.filter(publisher=blog).exists():
                    media = otapick.BlogImageDownloader().download(
                        image_url, group_id, blog.blog_ct, blog.writer.ct)
                    if media == 'not_image':  # exclude gif
                        pass
                    elif media is not None:
                        image = Image(
                            order=order,
                            picture=media,
                            publisher=blog,
                        )

                        # set width & height
                        w, h = otapick.get_image_w_h(image)
                        image.width = w
                        image.height = h

                        image_objects.append(image)
                        order += 1
                    else:
                        import traceback
                        traceback.print_exc()

        # change the order_for_simul of already saved blog with the same post_date
        if Blog.objects.filter(post_date=post_date).exists():
            for saved_simultime_blog in Blog.objects.filter(post_date=post_date):
                saved_simultime_blog.order_for_simul += download_count
                saved_simultime_blog.save()

    # save new blog
    for blog_object in blog_objects:
        blog_object.save()
        if console:
            otapick.print_console(
                'register 「' + blog_object.title + '」 written by ' + blog_object.writer.full_kanji)

    # save new image
    for image_object in image_objects:
        image_object.save()
        otapick.compress_blog_image(image_object)

    # tweet update info
    if tweet:
        updateBot = otapick.UpdateBot()
        for blog_object in blog_objects:
            updateBot.tweet(
                group_id=blog_object.publishing_group.group_id, blog_ct=blog_object.blog_ct)

    # When there is at least one already saved blog in blog_list and all_check is False
    if download_count != len(blog_info_list) and not all_check:
        return True

    # When all blog in blog_list are new or when all_check is True
    else:
        return False
Beispiel #23
0
def upload(request):
    if request.method == 'GET':
        return render_to_response('upload.html', {
            'url': request.GET.get('url', ''),
        },
                                  context_instance=RequestContext(request))
    elif request.method == 'POST':
        tmp = tempfile.mkstemp()
        md5 = hashlib.md5()
        fext = ""
        orig = ""

        if request.POST['upload_type'] == 'file':
            file = request.FILES['upload_file']
            fext = file.name[-3:]
            orig = file.name
            f = os.fdopen(tmp[0], "wb+")
            for chunk in file.chunks():
                f.write(chunk)
                md5.update(chunk)
            f.close()

        elif request.POST['upload_type'] == 'url':
            remote_image = urllib2.urlopen(request.POST['upload_url'])
            data = remote_image.read()
            md5.update(data)
            fext = request.POST['upload_url'][-3:]
            orig = request.POST['upload_url']

            f = os.fdopen(tmp[0], "wb+")
            f.write(data)
            f.close()

        img = Image()
        try:
            next_id = Image.objects.order_by('-id')[0].id + 1
        except IndexError:
            next_id = settings.IMAGE_ID_OFFSET + 1

        img.base62 = base62(next_id)
        img.filename = base62(next_id) + "." + fext.lower()
        img.orig_filename = orig
        img.type = ''  # todo
        img.description = ''  # not implemented yet.
        img.uploader = request.user
        img.md5sum = md5.hexdigest()
        image_file = os.path.join(settings.MEDIA_ROOT, img.filename)
        thumbnail = os.path.join(settings.MEDIA_ROOT, 'thumbs', img.filename)

        try:
            img.save()
        except IntegrityError:
            os.unlink(tmp[1])  # delete the uploaded file if it already exists
            return HttpResponseRedirect(settings.MEDIA_URL + Image.objects.get(
                md5sum=img.md5sum).filename)

        shutil.move(tmp[1], image_file)
        os.system("/usr/bin/convert %s -thumbnail 150x150 %s" %
                  (image_file, thumbnail))

        return HttpResponseRedirect(settings.MEDIA_URL + img.filename)