コード例 #1
0
ファイル: bookviews.py プロジェクト: drulang/foxleaf
    def _edit():
        if art.user != request.user:
            return HttpResponseForbidden("User does not own art")

        # Update Fields
        art.arttype_id = request.POST['artType']
        
        # Check if title changed
        art_title = request.POST.get("artTitle", "").strip()
        if art_title != art.title:
            if len(art_title) == 0:
                context = {
                    "userart": art,
                    "error": "Art title cannot be empty",
                }
                return render(request, 'cobalt/editart.html', context)
            art.title = art_title
            art.title_url = string_to_url(art.title)

        # Description
        art.description = request.POST['artText']

        # NSFW
        if request.POST.get("nsfw"):
            art.nsfw = True
        else:
            art.nsfw = False

        # Image
        request_file = request.FILES.get('art-image')

        if request_file:
            if not image_type_valid(request_file.content_type):
                context = {
                    "userart": art,
                    "error": "Unsupported file type",
                }
                return render(request, 'cobalt/editart.html', context)

            # Save file to disk
            filename = art.image.filename
            fq_filename = os.path.join(ART_IMAGE_PATH, filename)
            handle_uploaded_image(request_file, fq_filename)

        art.save()
        return redirect('/art/%s' % art.id)
コード例 #2
0
ファイル: __init__.py プロジェクト: drulang/foxleaf
def profileimage(request):
    if request.method == "POST":
        request_file = request.FILES['profile-image']

        # Build profile image name
        filename, ext = os.path.splitext(request_file.name)
        filename = "profile_image_" + request.user.username + ext
        fq_filename = os.path.join(PROFILE_IMAGE_PATH, filename)

        # Save file to disk
        handle_uploaded_image(request_file, fq_filename)

        # If successful then create an Image and assign to user
        if request.user.profileimage:
            profile_image = request.user.profileimage
        else:
            profile_image = models.Image()

        profile_image.imagetype = PROFILE_IMAGE_TYPE
        profile_image.relativepath = fq_filename
        profile_image.filename = filename

        profile_image.save()

        # Assign to user
        request.user.profileimage = profile_image
        request.user.save()

        imagetasks.resize_profile_image.delay(request.user.id)

        return redirect("/profile?profile-img-proc=True")
    else:
        # Reutrn the profile image for user
        if request.user.profileimage:
            image_filename = request.user.profileimage.relativepath
        else:
            image_filename = "cobalt/static/cobalt/img/noprofileimage.png"
            image_filename = os.path.join(settings.BASE_DIR, image_filename)

        with open(image_filename, "rb") as f:
            return HttpResponse(f.read(), content_type="image/jpeg")
コード例 #3
0
ファイル: bookviews.py プロジェクト: drulang/foxleaf
def art(request, booktitle):
    if request.method == "POST":
        book = models.Book.objects.filter(title_url=booktitle).first()
        if not book:
            raise ValueError("Unable to find book with title: %s" % booktitle)
        request_file = request.FILES['art-image']
        art_title = request.POST.get("artTitle", "").strip()
        # Validate data
        if not image_type_valid(request_file.content_type):
            raise ValueError("Unsupported content type")
        elif len(art_title) == 0:
            raise ValueError("Title cannot be empty")
        elif request_file.size > MAX_FILE_SIZE:
            raise ValueError("File cannot be greater than 25MB")

        # Create art
        title_url = string_to_url(request.POST['artTitle'])
        filename, ext = os.path.splitext(request_file.name)
        for _ in range(50):
            # Try 50 times to find a filename
            filename = "art_image__" + random_string(length=80) + ext
            fq_filename = os.path.join(ART_IMAGE_PATH, filename)

            if os.path.exists(fq_filename):
                if _ == 49:
                    raise ValueError("Error saving image")
                else:
                    continue
            else:
                break

        # Save file to disk
        handle_uploaded_image(request_file, fq_filename)

        # If successful then create an Image and assign to user
        art_image = models.Image()
        art_image.imagetype = ART_IMAGE_TYPE
        art_image.relativepath = fq_filename
        art_image.filename = filename
        art_image.save()

        # Create Art model
        art = models.Art()
        art.user = request.user
        art.arttype_id = request.POST['artType']
        art.scene_id = request.POST.get("sceneid")
        art.book = book
        art.image = art_image
        art.title = art_title
        art.title_url = title_url
        art.description = request.POST['artText']
        art.nsfw = request.POST.get('nsfw', "f")[0]
        art.save() 

        #
        # TODO: Need to resize and standardize the iamge with a job
        #

        usertasks.add_point_to_user.delay(request.user.id, "adart")

        art_url = "/book/" + book.title_url + "/art/" + art.title_url;
        data = {
           "status": OK,
           "artURL": art_url,
        }
        return json_response(data)
    else:
        return HttpResponse("Art Page")