Exemplo n.º 1
0
    def _edit():
        if scene.user != request.user:
            return HttpResponseForbidden("User does not own scene")

        title = request.POST['title'].strip()
        startpage = request.POST['startPage'].strip()
        endpage = request.POST['endPage'].strip()
        text = request.POST['text'].strip()
        nsfw = True if request.POST.get("nsfw") == 'true' else False
        scene_type = get_object_or_404(models.SceneType, scenetypcd=request.POST['sceneType'])

        error = None
        if bool(startpage) != bool(endpage):
            error = "If one page is passed so must the other"
        else:
            if startpage == "":
                startpage = None
            else:
                startpage = int(startpage)
            if endpage == "":
                endpage = None
            else:
                endpage = int(endpage)

        # Validate
        # Title required for all
        if not title:
            error = "Title cannot be empty"

        if scene_type.scenetypcd == "gnrc":
            if not text:
                error = "Scene text is required"
            elif not startpage:
                error = "Scene start page is required"
            elif not endpage:
                error = "Scene end page is required"
            
        # Since other scene types might have start/end pages check 
        if startpage and endpage and (startpage > endpage):
            error = "Start page must come before end page"

        if error:
            context = {
               "userscene": scene,
               "error": error,
            }
            return render(request, 'cobalt/editscene.html', context)

        scene.title = title
        scene.title_url=string_to_url(title)
        scene.startpage = startpage
        scene.endpage = endpage
        scene.text = text
        scene.nsfw = nsfw
        scene.scenetype = scene_type
        scene.save()

        return redirect('/scene/%s' % scene.id)
Exemplo n.º 2
0
    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)
Exemplo n.º 3
0
def fix_genre_names(apps, schema_editor):
    Genre = apps.get_model("cobalt", "Genre")

    for genre in Genre.objects.all():
        genre.name = string_to_url(genre.name)
        genre.save()
Exemplo n.º 4
0
def scene(request, booktitle):
    if request.method == "POST":
        book = models.Book.objects.filter(title_url=booktitle).first()

        title = request.POST['title'].strip()
        startpage = request.POST['startPage'].strip()
        endpage = request.POST['endPage'].strip()
        text = request.POST['text'].strip()
        nsfw = True if request.POST["nsfw"] == 'true' else False
        scene_type = get_object_or_404(models.SceneType, scenetypcd=request.POST['sceneType'])
        if bool(startpage) != bool(endpage):
            raise ValueError("If one page is passed so must the other")
        else:
            if startpage == "":
                startpage = None
            else:
                startpage = int(startpage)
            if endpage == "":
                endpage = None
            else:
                endpage = int(endpage)

        # Validate
        # Title required for all
        if not title:
            raise ValueError("Title cannot be null or empty")

        if scene_type.scenetypcd == "gnrc":
            if not text:
                raise ValueError("Text is required")
            elif not startpage:
                raise ValueError("StartPage is required")
            elif not endpage:
                raise ValueError("EndPage is required")
            
        # Since other scene types might have start/end pages check 
        if startpage and endpage and (startpage > endpage):
            raise ValueError("Start page must come before end page")

        new_scene = models.Scene(
            user=request.user,
            book=book,
            scenetype=scene_type,
            title=title,
            title_url=string_to_url(title),
            startpage=startpage,
            endpage=endpage,
            text=text,
            nsfw=nsfw,
        )
        new_scene.save()

        # Give user some points
        usertasks.add_point_to_user.delay(request.user.id, "adscn")

        new_scene_url = request.path + "/" + new_scene.title_url
        resp_data = {
            "status": OK,
            "newSceneUrl": new_scene_url,
            "sceneid": new_scene.id,
        }
        return json_response(resp_data)

    if request.method == "GET" and request.META.get("CONTENT_TYPE") == "application/json":
        book = models.Book.objects.filter(title_url=booktitle).first()
        if not book:
            resp_data = err_resp("Book not found")
        else:
            scenes = []
            scenetypcd = request.GET.get("scenetypcd")
            book_scenes = book.scene.filter(scenetype_id=scenetypcd).all()
            for scene in book_scenes:
                scenes.append(scene.dict)

            resp_data = { 
                "scenes": scenes, 
                "status": OK,
            }

        return json_response(resp_data)

    # Default
    return HttpResponse("Scene Page")
Exemplo n.º 5
0
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")
Exemplo n.º 6
0
def booksearch(query):
    if not query:
        raise ValueError("Query cannot be null or empty")
    url = BASE_URL % query
    print("Request: %s" % url)

    resp = requests.get(url)
    resp = resp.json()

    book_results = []
    for book_dict in resp['items']:
        volume_info = book_dict['volumeInfo']
        # Check to see if the book is part of VALID_CATEGORIES
        is_valid = False
        for category in volume_info.get('categories',[]):
            for valid_category in VALID_CATEGORIES:
                if valid_category in category.lower():
                    is_valid = True
                    break

            if is_valid:
                break

        if not is_valid:
            continue

        # Make sure ISBN doesn't exist in db already
        isbn = None
        for ident in volume_info['industryIdentifiers']:
            if ident['type'] == "ISBN_13":
                isbn = ident['identifier']
                break

        if not isbn:
            print("Unable to create book. No ISBN_13. Title:'%s' Identifiers:%s" % (volume_info.get("title"), volume_info.get("industryIdentifiers")))
            continue

        if Book.objects.filter(isbn=isbn).exists():
            book = Book.objects.filter(isbn=isbn).first()
        else:
            try:
                book = Book()
                book.isbn = isbn
                book.title = volume_info['title']
                book.author = volume_info['authors'][0]
                book.summary = volume_info.get('description')
                book.title_url = string_to_url(book.title)
                book.publisher = volume_info.get('publisher')
                book.coverurl = volume_info['imageLinks']['thumbnail']
                book.coverurl.replace("http", "https")
                book.blessed = False
                book.save()
            except Exception as e:
                print("Unable to save book. Error: %s" % e)
                print("Volume: %s" % volume_info)

            imagetasks.adjust_book_cover_url.delay(book.id)

        book_results.append(book)

    return book_results