예제 #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)
예제 #2
0
파일: views.py 프로젝트: petsmara/backend
    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)
예제 #3
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)
예제 #4
0
파일: views.py 프로젝트: enjoy2000/hoaxinh
def list(request):
    # Handle file upload
    if request.method == 'POST':
        form = ImageForm(request.POST, request.FILES)
        if form.is_valid():
            for img in request.FILES.getlist('name'):
                from django.utils import timezone
                newdoc = Image(name = img, pub_date = timezone.now())
                newdoc.save()

            # Redirect to the document list after POST
            return HttpResponseRedirect(reverse('image.views.list'))

    else:
        form = ImageForm() # A empty, unbound form

    # Load documents for the list page
    documents_list = Image.objects.all()

    # Pagination
    from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
    limit = 16 # limit photos each page
    paginator = Paginator(documents_list, limit)

    # Render list page with the documents and the form
    return render_to_response(
        'image/list.html',
        {'paginator': paginator},
        context_instance=RequestContext(request)
    )
예제 #5
0
파일: views.py 프로젝트: enjoy2000/hoaxinh
def add(request):
    # Check login
    if (request.user.is_authenticated() ==  False):
        return HttpResponseRedirect(reverse('image.views.index'))

    # Handle file upload
    responseData = []
    response_data = {}
    response_data['result'] = 'failed'
    response_data['files'] = 'abc'

    for img in request.FILES.getlist('files[]'):
        # import pdb; pdb.set_trace();
        # Check file is image or not
        file_type = img.content_type.split('/')[0]
        if file_type != 'image':
            messages.error(request, "Your file is not image.")
            HttpResponseRedirect(reverse('image.views.list'))
        
        # Insert photos
        newdoc = Image(name = img, pub_date = timezone.now(), user_id = request.user.id)
        newdoc.save()
        responseData.append(img)

    # Add to couple story
    from django.db.models import Q
    couple = Couple.objects.get(Q(male=request.user.id) | Q(female=request.user.id))
    content = "Added " + str(len(request.FILES.getlist('files[]'))) + " Photos"
    story = Story(couple_id=couple.id, content=content, pub_date=timezone.now())
    story.save()

    return HttpResponse(json.dumps(response_data), content_type="application/json")
예제 #6
0
 def add_image(self, uploadedfile, description, product):
     u'''
     add new image
     '''
     img = Image(product=product, description=description)
     img.image = uploadedfile
     img.save()
예제 #7
0
파일: views.py 프로젝트: jcline/imghost
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':
            upload_url = request.POST['upload_url']
            remote_image = urllib2.urlopen(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.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)

        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)
예제 #8
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)
예제 #9
0
    def save_all_image(slef, form_set, product):
        for item in form_set:
            image = item.cleaned_data.get('image', None)
            description = item.cleaned_data.get('img_description', None)
            
            if image is None or description is None:
                continue

            img_obj = Image(product=product, image=image, description=description)
            img_obj.save()
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)
예제 #11
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())
예제 #12
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
예제 #13
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.'))
예제 #14
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)
예제 #15
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
예제 #16
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})
예제 #17
0
def create_images():

	Image.objects.all().delete()

	for pk, fields in imagenes.iteritems():

		im = Image(pk=pk)
		im.order = fields['order']
		im.user_id = get_user(fields['user'])
		im.width = fields['width']
		im.height = fields['height']

		file_name = fields['image'].split('/')[-1]
		#result = urllib.urlretrieve('http://datea.pe/media/'+fields['image'])
		im.image.save(file_name, File(open('impdata/'+fields['image'])))
		im.client_domain = datea
		im.save()
예제 #18
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()
예제 #19
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()
예제 #20
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)
예제 #21
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)
예제 #22
0
def save_social_profile_image(user, img_url = None, platform='facebook'):

    if platform == 'facebook':
      img_url = "http://graph.facebook.com/{id}/picture?width=9999".format(id=user.facebook_id)
    elif platform == 'twitter':
      img_url = img_url.replace('_normal', '')

    try:
        img = urlopen(img_url)
        suffix = '_fb'
    except HTTPError:
        pass

    if img:
        imgFormat = 'png' if img.headers['content-type'] == 'image/png' else 'jpg'
        img_obj = Image(user=user)
        img_obj.image.save(slugify(user.username + '_'+ platform) + '.' + imgFormat, ContentFile(img.read()))
        img_obj.save()
        user.image = img_obj
        user.save()
예제 #23
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))
예제 #24
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)
예제 #25
0
파일: pipeline.py 프로젝트: datea/datea-api
def save_avatar(backend, user, response, details, is_new=False,*args,**kwargs):

    if is_new or not user.image:

        img = None

        # FACEBOOK
        if backend.name == 'facebook':
            try:
                img_url = "http://graph.facebook.com/{id}/picture?type=large".format(id=response["id"])
                img = urlopen(img_url)
                suffix = '_fb'
            except HTTPError:
                pass

        # TWITTER
        if backend.name == 'twitter':
            try:
                img = urlopen(response['profile_image_url'].replace('_normal', ''))
                suffix = '_tw'
            except HTTPError:
                pass

        # GOOGLE
        if backend.name == 'google-oauth2':
            try:
                img = urlopen(response['picture'])
                suffix = '_g'
            except HTTPError:
                pass

        if img:
            format = 'png' if img.headers['content-type'] == 'image/png' else 'jpg'
            img_obj = Image(user=user)
            img_obj.image.save(slugify(user.username + suffix) + '.' + format, ContentFile(img.read()))
            img_obj.save()
            user.image = img_obj
            user.save()
예제 #26
0
파일: views.py 프로젝트: datea/datea-api
def save_image(request):
    """
    Save DateaImage instance for specified object FK or M2M-Field
    post parameters must include:
        image:           image file input
        order:           the order of the image (optional)
    """
    key_auth = ApiKeyAuthentication()

    if not key_auth.is_authenticated(request):
        return HttpResponse("<h1>Login required</h1>")

    form = ImageUploadForm(request.POST, request.FILES)

    if form.is_valid():

        image_data = request.FILES['image']
        image_instance = Image(image=image_data, user=request.user)
        if 'order' in form.cleaned_data:
            image_instance.order = form.cleaned_data['order']
        image_instance.save()

        # create image resource
        ir = ImageResource()
        im_bundle = ir.build_bundle(obj=image_instance)
        im_bundle = ir.full_dehydrate(im_bundle)

        data = {'ok': True, 'message':'ok', 'resource': im_bundle.data}
        status=201
    else:
        data = {'ok': False, 'message': form.errors}
        status = 400

    resp = JsonResponse(data)
    resp.status_code = status
    return resp
예제 #27
0
파일: views.py 프로젝트: By2048/wallpaper
    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,
            })
예제 #28
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)
예제 #29
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')
예제 #30
0
 def setUp(self):
     self.c = Client()
     im = Image(image=File(open("image/test_image.jpeg", "rb")))
     im.save()
예제 #31
0
class ImageViewsetTest(APITestBase):

    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'

    def test_list(self):
        response = self.client.get('/imgs/')
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        results = json_loads(response.content)['results']
        self.assertEqual(type(results), list)
        self.assertEqual(len(results), 1)

    def test_detail(self):
        response = self.client.get('/imgs/%s/' % self.img.id)
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        result = json_loads(response.content)
        self.assertEqual(type(result), dict)
        self.assertIn('uuid', result)
        self.assertNotIn('id', result)
        self.assertNotIn('phash', result)
        self.assertEqual(result['uuid'], self.img.uuid)

    def test_create(self):
        response = self.client.post('/imgs/', dict(content=self.content2, test=True))
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)

    def test_create2(self):
        response = self.client.post('/imgs/', dict(content=self.content2, lon=127.0, lat=37.0, local_datetime='2015:04:22 11:54:19', test=True))
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
        img = Image.objects.get(content=self.content2)
        self.assertEqual(img.lonLat, GEOSGeometry('POINT(%f %f)' % (127.0, 37.0), srid=4326))
        self.assertEqual(img.timestamp, 1429671259000)

    def test_create3(self):
        with open('image/samples/gps_test.jpg') as f:
            response = self.client.post('/rfs/', dict(file=f))
        img_url = json_loads(response.content)['url']
        response = self.client.post('/imgs/', dict(content=img_url, test=True))
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
        uuid = json_loads(response.content)['uuid']
        img = Image.get_from_json('{"uuid": "%s"}' % uuid)
        self.assertEqual(img.lonLat, GEOSGeometry('POINT(%f %f)' % (127.103744, 37.399731), srid=4326))
        self.assertEqual(img.timestamp, 1459149534000)

    def test_create4(self):
        with open('image/samples/gps_test.jpg') as f:
            response = self.client.post('/rfs/', dict(file=f))
        img_url = json_loads(response.content)['url']
        response = self.client.post('/imgs/', dict(content=img_url, lon=127.0, lat=37.0, local_datetime='2015:04:22 11:54:19', test=True))
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
        uuid = json_loads(response.content)['uuid']
        img = Image.get_from_json('{"uuid": "%s"}' % uuid)
        self.assertEqual(img.lonLat, GEOSGeometry('POINT(%f %f)' % (127.0, 37.0), srid=4326))
        self.assertEqual(img.timestamp, 1429671259000)

    def test_create_twice(self):
        self.assertEqual(Image.objects.count(), 1)
        response = self.client.post('/imgs/', dict(content=self.content2, test=True))
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
        self.assertEqual(Image.objects.count(), 2)

        response = self.client.post('/imgs/', dict(content=self.content2, test=True))
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
        self.assertEqual(Image.objects.count(), 2)