Example #1
0
def test_commish_search(args, result_order):
    # searcher
    u1 = db_utils.create_user(username="******")

    # user open for commissions
    create_commish_searchable_user("u2", submittime=arrow.now())

    # user sometimes open for commissions (should behave same as above)
    create_commish_searchable_user("u3", commish_status='s',
                                   submittime=arrow.now() - datetime.timedelta(days=1))

    # user open for commissions, with blacklisted tags
    u4 = create_commish_searchable_user("u4", submittime=arrow.now() - datetime.timedelta(days=2))
    searchtag.associate(u4, optout_tags_userid=u4, tags={'cat'})

    # user with a different commish class and a preference tag
    u5 = create_commish_searchable_user("u5", commishclass="sketch",
                                        submittime=arrow.now() - datetime.timedelta(days=3))
    searchtag.associate(u5, preferred_tags_userid=u5, tags={'cat'})

    # user with a different price
    create_commish_searchable_user("u6", minprice="100.0", maxprice="100.0",
                                   submittime=arrow.now() - datetime.timedelta(days=4))

    results = commishinfo.select_commissionable(userid=u1,
                                                limit=10,
                                                offset=0,
                                                **args)
    rids = [r['username'] for r in results]
    assert rids == result_order
Example #2
0
def test_commish_search(args, result_order):
    # searcher
    u1 = db_utils.create_user(username="******")

    # user open for commissions
    create_commish_searchable_user("u2", submittime=arrow.now())

    # user sometimes open for commissions (should behave same as above)
    create_commish_searchable_user("u3", commish_status='s',
                                   submittime=arrow.now() - datetime.timedelta(days=1))

    # user open for commissions, with blacklisted tags
    u4 = create_commish_searchable_user("u4", submittime=arrow.now() - datetime.timedelta(days=2))
    searchtag.associate(u4, optout_tags_userid=u4, tags={'cat'})

    # user with a different commish class and a preference tag
    u5 = create_commish_searchable_user("u5", commishclass="sketch",
                                        submittime=arrow.now() - datetime.timedelta(days=3))
    searchtag.associate(u5, preferred_tags_userid=u5, tags={'cat'})

    # user with a different price
    create_commish_searchable_user("u6", minprice="100.0", maxprice="100.0",
                                   submittime=arrow.now() - datetime.timedelta(days=4))

    results = commishinfo.select_commissionable(userid=u1,
                                                limit=10,
                                                offset=0,
                                                **args)
    rids = [r['username'] for r in results]
    assert rids == result_order
Example #3
0
def create(userid, journal, friends_only=False, tags=None):
    # Check invalid arguments
    if not journal.title:
        raise WeasylError("titleInvalid")
    elif not journal.content:
        raise WeasylError("contentInvalid")
    elif not journal.rating:
        raise WeasylError("ratingInvalid")
    profile.check_user_rating_allowed(userid, journal.rating)

    # Assign settings
    settings = "f" if friends_only else ""

    # Create journal
    jo = d.meta.tables["journal"]

    # Run the journal through Akismet to check for spam
    is_spam = False
    if spam_filtering.FILTERING_ENABLED:
        result = spam_filtering.check(
            user_ip=journal.submitter_ip_address,
            user_agent_id=journal.submitter_user_agent_id,
            user_id=userid,
            comment_type="journal",
            comment_content=journal.content,
        )
        if result == SpamStatus.DefiniteSpam:
            raise WeasylError("SpamFilteringDropped")
        elif result == SpamStatus.ProbableSpam:
            is_spam = True

    journalid = d.engine.scalar(jo.insert().returning(jo.c.journalid), {
        "userid": userid,
        "title": journal.title,
        "content": journal.content,
        "rating": journal.rating.code,
        "unixtime": arrow.now(),
        "settings": settings,
        "is_spam": is_spam,
        "submitter_ip_address": journal.submitter_ip_address,
        "submitter_user_agent_id": journal.submitter_user_agent_id,
    })

    # Assign search tags
    searchtag.associate(userid, tags, journalid=journalid)

    # Create notifications
    if "m" not in settings:
        welcome.journal_insert(userid, journalid, rating=journal.rating.code,
                               settings=settings)

    d.metric('increment', 'journals')

    return journalid
Example #4
0
    def test_retag_no_owner_remove(self):
        config = CharSettings({'disallow-others-tag-removal'}, {}, {})
        owner = db_utils.create_user(config=config)
        tagger = db_utils.create_user()
        s = db_utils.create_submission(owner, rating=ratings.GENERAL.code)

        searchtag.associate(owner, {'orange'}, submitid=s)
        self.assertEqual(
            submission.select_view(owner, s, ratings.GENERAL.code)['tags'],
            ['orange'])

        searchtag.associate(tagger, {'apple', 'tomato'}, submitid=s)
        self.assertEqual(
            submission.select_view(owner, s, ratings.GENERAL.code)['tags'],
            ['apple', 'orange', 'tomato'])

        searchtag.associate(tagger, {'tomato'}, submitid=s)
        self.assertEqual(
            submission.select_view(owner, s, ratings.GENERAL.code)['tags'],
            ['orange', 'tomato'])

        searchtag.associate(owner, {'kale'}, submitid=s)
        self.assertEqual(
            submission.select_view(owner, s, ratings.GENERAL.code)['tags'],
            ['kale'])
Example #5
0
    def test_retag_no_owner_remove(self):
        config = CharSettings({'disallow-others-tag-removal'}, {}, {})
        owner = db_utils.create_user(config=config)
        tagger = db_utils.create_user()
        s = db_utils.create_submission(owner, rating=ratings.GENERAL.code)

        searchtag.associate(owner, {'orange'}, submitid=s)
        self.assertEqual(
            submission.select_view(owner, s, ratings.GENERAL.code)['tags'],
            ['orange'])

        searchtag.associate(tagger, {'apple', 'tomato'}, submitid=s)
        self.assertEqual(
            submission.select_view(owner, s, ratings.GENERAL.code)['tags'],
            ['apple', 'orange', 'tomato'])

        searchtag.associate(tagger, {'tomato'}, submitid=s)
        self.assertEqual(
            submission.select_view(owner, s, ratings.GENERAL.code)['tags'],
            ['orange', 'tomato'])

        searchtag.associate(owner, {'kale'}, submitid=s)
        self.assertEqual(
            submission.select_view(owner, s, ratings.GENERAL.code)['tags'],
            ['kale'])
Example #6
0
    def POST(self):
        form = web.input(submitid="", charid="", journalid="", tags="")

        tags = searchtag.parse_tags(form.tags)

        submitid = define.get_int(form.submitid)
        charid = define.get_int(form.charid)
        journalid = define.get_int(form.journalid)

        searchtag.associate(self.user_id, tags, submitid, charid, journalid)
        if submitid:
            raise web.seeother("/submission/%i" % (submitid,))
        elif charid:
            raise web.seeother("/character/%i" % (charid,))
        else:
            raise web.seeother("/journal/%i" % (journalid,))
Example #7
0
    def POST(self):
        form = web.input(submitid="", charid="", journalid="", tags="")

        tags = searchtag.parse_tags(form.tags)

        submitid = define.get_int(form.submitid)
        charid = define.get_int(form.charid)
        journalid = define.get_int(form.journalid)

        searchtag.associate(self.user_id, tags, submitid, charid, journalid)
        if submitid:
            raise web.seeother("/submission/%i" % (submitid, ))
        elif charid:
            raise web.seeother("/character/%i" % (charid, ))
        else:
            raise web.seeother("/journal/%i" % (journalid, ))
Example #8
0
def submit_tags_(request):
    form = request.web_input(submitid="", charid="", journalid="", tags="")

    tags = searchtag.parse_tags(form.tags)

    submitid = define.get_int(form.submitid)
    charid = define.get_int(form.charid)
    journalid = define.get_int(form.journalid)

    searchtag.associate(request.userid, tags, submitid, charid, journalid)
    if submitid:
        raise HTTPSeeOther(location="/submission/%i" % (submitid,))
    elif charid:
        raise HTTPSeeOther(location="/character/%i" % (charid,))
    else:
        raise HTTPSeeOther(location="/journal/%i" % (journalid,))
Example #9
0
def submit_tags_(request):
    if not define.is_vouched_for(request.userid):
        raise WeasylError("vouchRequired")

    form = request.web_input(submitid="",
                             charid="",
                             journalid="",
                             preferred_tags_userid="",
                             optout_tags_userid="",
                             tags="")

    tags = searchtag.parse_tags(form.tags)

    submitid = define.get_int(form.submitid)
    charid = define.get_int(form.charid)
    journalid = define.get_int(form.journalid)
    preferred_tags_userid = define.get_int(form.preferred_tags_userid)
    optout_tags_userid = define.get_int(form.optout_tags_userid)

    result = searchtag.associate(request.userid, tags, submitid, charid,
                                 journalid, preferred_tags_userid,
                                 optout_tags_userid)
    if result:
        failed_tag_message = ""
        if result["add_failure_restricted_tags"] is not None:
            failed_tag_message += "The following tags have been restricted from being added to this item by the content owner, or Weasyl staff: **" + result[
                "add_failure_restricted_tags"] + "**. \n"
        if result["remove_failure_owner_set_tags"] is not None:
            failed_tag_message += "The following tags were not removed from this item as the tag was added by the owner: **" + result[
                "remove_failure_owner_set_tags"] + "**.\n"
        failed_tag_message += "Any other changes to this item's tags were completed."
    if submitid:
        location = "/submission/%i" % (submitid, )
        if not result:
            raise HTTPSeeOther(location=location)
        else:
            return Response(
                define.errorpage(request.userid, failed_tag_message,
                                 [["Return to Content", location]]))
    elif charid:
        location = "/character/%i" % (charid, )
        if not result:
            raise HTTPSeeOther(location=location)
        else:
            return Response(
                define.errorpage(request.userid, failed_tag_message,
                                 [["Return to Content", location]]))
    elif journalid:
        location = "/journal/%i" % (journalid, )
        if not result:
            raise HTTPSeeOther(location=location)
        else:
            return Response(
                define.errorpage(request.userid, failed_tag_message,
                                 [["Return to Content", location]]))
    else:
        raise HTTPSeeOther(location="/control/editcommissionsettings")
Example #10
0
def create(userid, journal, friends_only=False, tags=None):
    # Check invalid arguments
    if not journal.title:
        raise WeasylError("titleInvalid")
    elif not journal.content:
        raise WeasylError("contentInvalid")
    elif not journal.rating:
        raise WeasylError("ratingInvalid")
    profile.check_user_rating_allowed(userid, journal.rating)

    # Assign settings
    settings = "f" if friends_only else ""

    # Create journal
    jo = d.meta.tables["journal"]

    journalid = d.engine.scalar(
        jo.insert().returning(jo.c.journalid), {
            "userid": userid,
            "title": journal.title,
            "rating": journal.rating.code,
            "unixtime": arrow.now(),
            "settings": settings,
        })

    # Write journal file
    files.make_path(journalid, "journal")
    files.write(files.make_resource(userid, journalid, "journal/submit"),
                journal.content)

    # Assign search tags
    searchtag.associate(userid, tags, journalid=journalid)

    # Create notifications
    if "m" not in settings:
        welcome.journal_insert(userid,
                               journalid,
                               rating=journal.rating.code,
                               settings=settings)

    d.metric('increment', 'journals')

    return journalid
Example #11
0
def test_attempt_setting_tags_when_some_tags_have_been_restricted():
    """
    Verify that tags are excluded from being added to a submission's tags if the tag is restricted
    """
    userid_owner = db_utils.create_user()
    userid_tag_adder = db_utils.create_user()
    journalid = db_utils.create_journal(userid_owner)
    charid = db_utils.create_character(userid_owner)
    submitid = db_utils.create_submission(userid_owner)
    restricted_tag = searchtag.parse_restricted_tags("pearl")
    searchtag.edit_user_tag_restrictions(userid_owner, restricted_tag)

    searchtag.associate(userid_tag_adder, tags, submitid=submitid)
    searchtag.associate(userid_tag_adder, tags, charid=charid)
    searchtag.associate(userid_tag_adder, tags, journalid=journalid)

    # Verify that the "pearl" tag was not added
    submission_tags = searchtag.select(submitid=submitid)
    assert tags_two == set(submission_tags)

    character_tags = searchtag.select(charid=charid)
    assert tags_two == set(character_tags)

    journal_tags = searchtag.select(journalid=journalid)
    assert tags_two == set(journal_tags)
Example #12
0
def test_moderators_and_above_can_add_restricted_tags_successfully(monkeypatch):
    """
    Moderators (and admins, technical, and directors) can add restricted tags to content.
    Developers are not included in this test, as they are for all intents and purposes just
      normal user accounts.
    """
    userid_owner = db_utils.create_user()
    mod_tag_adder = db_utils.create_user()
    monkeypatch.setattr(staff, 'MODS', frozenset([mod_tag_adder]))
    journalid = db_utils.create_journal(userid_owner)
    charid = db_utils.create_character(userid_owner)
    submitid = db_utils.create_submission(userid_owner)
    restricted_tag = searchtag.parse_restricted_tags("pearl")
    searchtag.edit_user_tag_restrictions(userid_owner, restricted_tag)

    searchtag.associate(mod_tag_adder, tags, submitid=submitid)
    searchtag.associate(mod_tag_adder, tags, charid=charid)
    searchtag.associate(mod_tag_adder, tags, journalid=journalid)

    # Verify that all tags were added successfully. 'pearl' is restricted.
    submission_tags = searchtag.select(submitid=submitid)
    assert tags == set(submission_tags)

    character_tags = searchtag.select(charid=charid)
    assert tags == set(character_tags)

    journal_tags = searchtag.select(journalid=journalid)
    assert tags == set(journal_tags)
Example #13
0
def create(userid, journal, friends_only=False, tags=None):
    # Check invalid arguments
    if not journal.title:
        raise WeasylError("titleInvalid")
    elif not journal.content:
        raise WeasylError("contentInvalid")
    elif not journal.rating:
        raise WeasylError("ratingInvalid")
    profile.check_user_rating_allowed(userid, journal.rating)

    # Assign settings
    settings = "f" if friends_only else ""

    # Create journal
    jo = d.meta.tables["journal"]

    journalid = d.engine.scalar(jo.insert().returning(jo.c.journalid), {
        "userid": userid,
        "title": journal.title,
        "rating": journal.rating.code,
        "unixtime": arrow.now(),
        "settings": settings,
    })

    # Write journal file
    files.make_path(journalid, "journal")
    files.write(files.make_resource(userid, journalid, "journal/submit"),
                journal.content)

    # Assign search tags
    searchtag.associate(userid, tags, journalid=journalid)

    # Create notifications
    if "m" not in settings:
        welcome.journal_insert(userid, journalid, rating=journal.rating.code,
                               settings=settings)

    d.metric('increment', 'journals')

    return journalid
Example #14
0
def test_TargetRecordMissing_WeasylError_if_item_record_missing_or_invalid():
    userid_tag_adder = db_utils.create_user()

    with pytest.raises(WeasylError) as err:
        searchtag.associate(userid_tag_adder, tags, submitid=666)
    assert err.value.value == "TargetRecordMissing"

    with pytest.raises(WeasylError) as err:
        searchtag.associate(userid_tag_adder, tags, charid=666)
    assert err.value.value == "TargetRecordMissing"

    with pytest.raises(WeasylError) as err:
        searchtag.associate(userid_tag_adder, tags, journalid=666)
    assert err.value.value == "TargetRecordMissing"
Example #15
0
def submit_tags_(request):
    form = request.web_input(submitid="", charid="", journalid="", preferred_tags_userid="", optout_tags_userid="", tags="")

    tags = searchtag.parse_tags(form.tags)

    submitid = define.get_int(form.submitid)
    charid = define.get_int(form.charid)
    journalid = define.get_int(form.journalid)
    preferred_tags_userid = define.get_int(form.preferred_tags_userid)
    optout_tags_userid = define.get_int(form.optout_tags_userid)

    result = searchtag.associate(request.userid, tags, submitid, charid, journalid, preferred_tags_userid, optout_tags_userid)
    if result:
        failed_tag_message = ""
        if result["add_failure_restricted_tags"] is not None:
            failed_tag_message += "The following tags have been restricted from being added to this item by the content owner, or Weasyl staff: **" + result["add_failure_restricted_tags"] + "**. \n"
        if result["remove_failure_owner_set_tags"] is not None:
            failed_tag_message += "The following tags were not removed from this item as the tag was added by the owner: **" + result["remove_failure_owner_set_tags"] + "**.\n"
        failed_tag_message += "Any other changes to this item's tags were completed."
    if submitid:
        location = "/submission/%i" % (submitid,)
        if not result:
            raise HTTPSeeOther(location=location)
        else:
            return Response(define.errorpage(request.userid, failed_tag_message,
                                             [["Return to Content", location]]))
    elif charid:
        location = "/character/%i" % (charid,)
        if not result:
            raise HTTPSeeOther(location=location)
        else:
            return Response(define.errorpage(request.userid, failed_tag_message,
                                             [["Return to Content", location]]))
    elif journalid:
        location = "/journal/%i" % (journalid,)
        if not result:
            raise HTTPSeeOther(location=location)
        else:
            return Response(define.errorpage(request.userid, failed_tag_message,
                                             [["Return to Content", location]]))
    else:
        raise HTTPSeeOther(location="/control/editcommissionsettings")
Example #16
0
    def test_retag(self):
        owner = db_utils.create_user()
        tagger = db_utils.create_user()
        s = db_utils.create_submission(owner, rating=ratings.GENERAL.code)

        searchtag.associate(owner, {'orange'}, submitid=s)
        self.assertEqual(
            submission.select_view(owner, s, ratings.GENERAL.code)['tags'],
            ['orange'])

        searchtag.associate(tagger, {'apple', 'tomato'}, submitid=s)
        self.assertEqual(
            submission.select_view(owner, s, ratings.GENERAL.code)['tags'],
            ['apple', 'tomato'])

        searchtag.associate(tagger, {'tomato'}, submitid=s)
        self.assertEqual(
            submission.select_view(owner, s, ratings.GENERAL.code)['tags'],
            ['tomato'])

        searchtag.associate(owner, {'kale'}, submitid=s)
        self.assertEqual(
            submission.select_view(owner, s, ratings.GENERAL.code)['tags'],
            ['kale'])
Example #17
0
    def test_retag(self):
        owner = db_utils.create_user()
        tagger = db_utils.create_user()
        s = db_utils.create_submission(owner, rating=ratings.GENERAL.code)

        searchtag.associate(owner, {'orange'}, submitid=s)
        self.assertEqual(
            submission.select_view(owner, s, ratings.GENERAL.code)['tags'],
            ['orange'])

        searchtag.associate(tagger, {'apple', 'tomato'}, submitid=s)
        self.assertEqual(
            submission.select_view(owner, s, ratings.GENERAL.code)['tags'],
            ['apple', 'tomato'])

        searchtag.associate(tagger, {'tomato'}, submitid=s)
        self.assertEqual(
            submission.select_view(owner, s, ratings.GENERAL.code)['tags'],
            ['tomato'])

        searchtag.associate(owner, {'kale'}, submitid=s)
        self.assertEqual(
            submission.select_view(owner, s, ratings.GENERAL.code)['tags'],
            ['kale'])
Example #18
0
def test_adding_tags_when_no_tags_previously_existed():
    userid_owner = db_utils.create_user()
    userid_tag_adder = db_utils.create_user()
    journalid = db_utils.create_journal(userid_owner)
    charid = db_utils.create_character(userid_owner)
    submitid = db_utils.create_submission(userid_owner)

    searchtag.associate(userid_tag_adder, tags, submitid=submitid)
    searchtag.associate(userid_tag_adder, tags, charid=charid)
    searchtag.associate(userid_tag_adder, tags, journalid=journalid)

    submission_tags = searchtag.select(submitid=submitid)
    assert tags == set(submission_tags)

    character_tags = searchtag.select(charid=charid)
    assert tags == set(character_tags)

    journal_tags = searchtag.select(journalid=journalid)
    assert tags == set(journal_tags)
Example #19
0
def test_contentOwnerIgnoredYou_WeasylError_if_user_ignored_by_item_owner():
    # Set up for this test
    userid_owner = db_utils.create_user()
    userid_tag_adder = db_utils.create_user()
    journalid = db_utils.create_journal(userid_owner)
    charid = db_utils.create_character(userid_owner)
    submitid = db_utils.create_submission(userid_owner)
    db_utils.create_ignoreuser(userid_owner, userid_tag_adder)

    with pytest.raises(WeasylError) as err:
        searchtag.associate(userid_tag_adder, tags, submitid=submitid)
    assert err.value.value == "contentOwnerIgnoredYou"

    with pytest.raises(WeasylError) as err:
        searchtag.associate(userid_tag_adder, tags, charid=charid)
    assert err.value.value == "contentOwnerIgnoredYou"

    with pytest.raises(WeasylError) as err:
        searchtag.associate(userid_tag_adder, tags, journalid=journalid)
    assert err.value.value == "contentOwnerIgnoredYou"
Example #20
0
def test_InsufficientPermissions_WeasylError_if_user_does_not_have_tagging_permissions():
    # Set up for this test
    admin = db_utils.create_user()
    userid_owner = db_utils.create_user()
    userid_tag_adder = db_utils.create_user()
    journalid = db_utils.create_journal(userid_owner)
    charid = db_utils.create_character(userid_owner)
    submitid = db_utils.create_submission(userid_owner)
    profile.do_manage(admin, userid_tag_adder, permission_tag=False)

    with pytest.raises(WeasylError) as err:
        searchtag.associate(userid_tag_adder, tags, submitid=submitid)
    assert err.value.value == "InsufficientPermissions"

    with pytest.raises(WeasylError) as err:
        searchtag.associate(userid_tag_adder, tags, charid=charid)
    assert err.value.value == "InsufficientPermissions"

    with pytest.raises(WeasylError) as err:
        searchtag.associate(userid_tag_adder, tags, journalid=journalid)
    assert err.value.value == "InsufficientPermissions"
Example #21
0
def create_multimedia(userid,
                      submission,
                      embedlink=None,
                      friends_only=None,
                      tags=None,
                      coverfile=None,
                      thumbfile=None,
                      submitfile=None,
                      critique=False,
                      create_notifications=True,
                      auto_thumb=False):
    embedlink = embedlink.strip()

    # Determine filesizes
    coversize = len(coverfile)
    thumbsize = len(thumbfile)
    submitsize = len(submitfile)

    if not submitsize and not embedlink:
        raise WeasylError("submitSizeZero")
    elif embedlink and not embed.check_valid(embedlink):
        raise WeasylError("embedlinkInvalid")
    elif coversize > 10 * _MEGABYTE:
        raise WeasylError("coverSizeExceedsLimit")
    elif thumbsize > 10 * _MEGABYTE:
        raise WeasylError("thumbSizeExceedsLimit")

    if submitsize:
        submitextension = files.get_extension_for_category(
            submitfile, m.MULTIMEDIA_SUBMISSION_CATEGORY)
        if submitextension is None:
            raise WeasylError("submitType")
        elif submitextension not in [".mp3", ".swf"] and not embedlink:
            raise WeasylError("submitType")
        elif _limit(submitsize, submitextension):
            raise WeasylError("submitSizeExceedsLimit")
        submit_media_item = orm.MediaItem.fetch_or_create(
            submitfile, file_type=submitextension.lstrip('.'))
        check_for_duplicate_media(userid, submit_media_item.mediaid)
    else:
        submit_media_item = None

    # Check if the submission is spam
    is_spam = _check_for_spam(submission=submission, userid=userid)

    thumb_media_item = media.make_cover_media_item(thumbfile)
    cover_media_item = media.make_cover_media_item(coverfile)
    if cover_media_item and not thumb_media_item:
        thumb_media_item = cover_media_item

    tempthumb_media_item = None
    im = None
    if auto_thumb:
        if thumbsize == 0 and coversize == 0:
            # Fetch default thumbnail from source if available
            thumb_url = embed.thumbnail(embedlink)
            if thumb_url:
                resp = d.http_get(thumb_url, timeout=5)
                im = image.from_string(resp.content)
    if not im and (thumbsize or coversize):
        im = image.from_string(thumbfile or coverfile)
    if im:
        tempthumb = images.make_thumbnail(im)
        tempthumb_type = images.image_file_type(tempthumb)
        tempthumb_media_item = orm.MediaItem.fetch_or_create(
            tempthumb.to_buffer(format=tempthumb_type),
            file_type=tempthumb_type,
            im=tempthumb)

    # Assign settings
    settings = []
    settings.append("f" if friends_only else "")
    settings.append("q" if critique else "")
    settings.append("v" if embedlink else "")
    settings = "".join(settings)

    # Inject embedlink
    if embedlink:
        submission.content = "".join([embedlink, "\n", submission.content])

    # Create submission
    db = d.connect()
    now = arrow.get()
    q = (d.meta.tables['submission'].insert().values([{
        "folderid":
        submission.folderid,
        "userid":
        userid,
        "unixtime":
        now,
        "title":
        submission.title,
        "content":
        submission.content,
        "subtype":
        submission.subtype,
        "rating":
        submission.rating,
        "settings":
        settings,
        "favorites":
        0,
        "is_spam":
        is_spam,
        "submitter_ip_address":
        submission.submitter_ip_address,
        "submitter_user_agent_id":
        submission.submitter_user_agent_id,
    }]).returning(d.meta.tables['submission'].c.submitid))
    submitid = db.scalar(q)

    # Assign search tags
    searchtag.associate(userid, tags, submitid=submitid)

    if submit_media_item:
        orm.SubmissionMediaLink.make_or_replace_link(submitid, 'submission',
                                                     submit_media_item)
    if cover_media_item:
        orm.SubmissionMediaLink.make_or_replace_link(submitid, 'cover',
                                                     cover_media_item)
    if thumb_media_item:
        orm.SubmissionMediaLink.make_or_replace_link(submitid,
                                                     'thumbnail-source',
                                                     thumb_media_item)
    if tempthumb_media_item:
        orm.SubmissionMediaLink.make_or_replace_link(submitid,
                                                     'thumbnail-custom',
                                                     tempthumb_media_item)

    # Create notifications
    if create_notifications:
        _create_notifications(userid, submitid, submission.rating, settings)

    d.metric('increment', 'submissions')
    d.metric('increment', 'multimediasubmissions')

    return submitid, bool(thumb_media_item)
Example #22
0
def create_multimedia(userid, submission, embedlink=None, friends_only=None,
                      tags=None, coverfile=None, thumbfile=None, submitfile=None,
                      critique=False, create_notifications=True, auto_thumb=False):
    premium = d.get_premium(userid)
    embedlink = embedlink.strip()

    # Determine filesizes
    coversize = len(coverfile)
    thumbsize = len(thumbfile)
    submitsize = len(submitfile)

    if not submitsize and not embedlink:
        raise WeasylError("submitSizeZero")
    elif embedlink and not embed.check_valid(embedlink):
        raise WeasylError("embedlinkInvalid")
    elif coversize > 10 * _MEGABYTE:
        raise WeasylError("coverSizeExceedsLimit")
    elif thumbsize > 10 * _MEGABYTE:
        raise WeasylError("thumbSizeExceedsLimit")

    if submitsize:
        submitextension = files.get_extension_for_category(submitfile, m.MULTIMEDIA_SUBMISSION_CATEGORY)
        if submitextension is None:
            raise WeasylError("submitType")
        elif submitextension not in [".mp3", ".swf"] and not embedlink:
            raise WeasylError("submitType")
        elif _limit(submitsize, submitextension, premium):
            raise WeasylError("submitSizeExceedsLimit")
        submit_media_item = orm.fetch_or_create_media_item(
            submitfile, file_type=submitextension.lstrip('.'))
        check_for_duplicate_media(userid, submit_media_item.mediaid)
    else:
        submit_media_item = None

    thumb_media_item = media.make_cover_media_item(thumbfile)
    cover_media_item = media.make_cover_media_item(coverfile)
    if cover_media_item and not thumb_media_item:
        thumb_media_item = cover_media_item

    tempthumb_media_item = None
    im = None
    if auto_thumb:
        if thumbsize == 0 and coversize == 0:
            # Fetch default thumbnail from source if available
            thumb_url = embed.thumbnail(embedlink)
            if thumb_url:
                resp = d.http_get(thumb_url, timeout=5)
                im = image.from_string(resp.content)
    if not im and (thumbsize or coversize):
        im = image.from_string(thumbfile or coverfile)
    if im:
        tempthumb = images.make_thumbnail(im)
        tempthumb_type = images.image_file_type(tempthumb)
        tempthumb_media_item = orm.fetch_or_create_media_item(
            tempthumb.to_buffer(format=tempthumb_type),
            file_type=tempthumb_type,
            im=tempthumb)

    # Assign settings
    settings = []
    settings.append("f" if friends_only else "")
    settings.append("q" if critique else "")
    settings.append("v" if embedlink else "")
    settings = "".join(settings)

    # Inject embedlink
    if embedlink:
        submission.content = "".join([embedlink, "\n", submission.content])

    # Create submission
    db = d.connect()
    now = arrow.get()
    try:
        q = (
            d.meta.tables['submission'].insert().values([{
                "folderid": submission.folderid,
                "userid": userid,
                "unixtime": now,
                "title": submission.title,
                "content": submission.content,
                "subtype": submission.subtype,
                "rating": submission.rating,
                "settings": settings,
                "sorttime": now,
            }])
            .returning(d.meta.tables['submission'].c.submitid))
        submitid = db.scalar(q)
    except PostgresError:
        files.clear_temporary(userid)
        raise

    # Assign search tags
    searchtag.associate(userid, tags, submitid=submitid)

    if submit_media_item:
        orm.SubmissionMediaLink.make_or_replace_link(submitid, 'submission', submit_media_item)
    if cover_media_item:
        orm.SubmissionMediaLink.make_or_replace_link(submitid, 'cover', cover_media_item)
    if thumb_media_item:
        orm.SubmissionMediaLink.make_or_replace_link(submitid, 'thumbnail-source', thumb_media_item)
    if tempthumb_media_item:
        orm.SubmissionMediaLink.make_or_replace_link(submitid, 'thumbnail-custom',
                                                     tempthumb_media_item)

    # Create notifications
    if create_notifications:
        _create_notifications(userid, submitid, submission.rating, settings,
                              submission.title, tags)

    # Clear temporary files
    files.clear_temporary(userid)

    d.metric('increment', 'submissions')
    d.metric('increment', 'multimediasubmissions')

    return submitid, bool(thumb_media_item)
Example #23
0
def test_associate_return_values():
    """
    ``associate()`` returns a dict, of the following format:
    return {"add_failure_restricted_tags": add_failure_restricted_tags,
            "remove_failure_owner_set_tags": remove_failure_owner_set_tags}
    /OR/ None

    add_failure_restricted_tags is None if no tags failed to be added during the associate call,
    when due to a tag being on the user or globally restricted tags list. Otherwise, it contains
    a space-separated list of tags which failed to be added to the content item.

    remove_failure_owner_set_tags is None if no tags failed to be removed during the associate call.
    Otherwise, it contains the same space-separated list as above, however containing tags which the
    content owner added and has opted to not permit others to remove.

    If neither element of the dict is set, ``associate()`` returns None.
    """
    config = CharSettings({'disallow-others-tag-removal'}, {}, {})
    userid_owner = db_utils.create_user(config=config)
    userid_tag_adder = db_utils.create_user()
    submitid = db_utils.create_submission(userid_owner)
    journalid = db_utils.create_journal(userid_owner)
    charid = db_utils.create_character(userid_owner)

    """ Test the None result (no failures), then manually clear the tags afterwards. """
    result = searchtag.associate(userid_tag_adder, tags, submitid=submitid)
    assert result is None
    result = searchtag.associate(userid_tag_adder, tags, journalid=journalid)
    assert result is None
    result = searchtag.associate(userid_tag_adder, tags, journalid=journalid)
    assert result is None
    searchtag.associate(userid_tag_adder, set(), submitid=submitid)
    searchtag.associate(userid_tag_adder, set(), journalid=journalid)
    searchtag.associate(userid_tag_adder, set(), journalid=journalid)

    """ Test the result:None variant (restricted tags added, no tags removed) """
    restricted_tag = searchtag.parse_restricted_tags("pearl")
    searchtag.edit_user_tag_restrictions(userid_owner, restricted_tag)
    result = searchtag.associate(userid_tag_adder, tags, submitid=submitid)
    assert "pearl" in result["add_failure_restricted_tags"]
    assert result["remove_failure_owner_set_tags"] is None
    result = searchtag.associate(userid_tag_adder, tags, charid=charid)
    assert "pearl" in result["add_failure_restricted_tags"]
    assert result["remove_failure_owner_set_tags"] is None
    result = searchtag.associate(userid_tag_adder, tags, journalid=journalid)
    assert "pearl" in result["add_failure_restricted_tags"]
    assert result["remove_failure_owner_set_tags"] is None
    searchtag.associate(userid_owner, set(), submitid=submitid)
    searchtag.associate(userid_owner, set(), charid=charid)
    searchtag.associate(userid_owner, set(), journalid=journalid)
    searchtag.edit_user_tag_restrictions(userid_owner, set())

    """Test the None:result variant (no restricted tags added, tag removal blocked)
    - Submission items will return None in this case (different method of preventing tag removal)
    - Character and journal items should return the None:result variant, as expected"""
    searchtag.associate(userid_owner, tags, submitid=submitid)
    searchtag.associate(userid_owner, tags, charid=charid)
    searchtag.associate(userid_owner, tags, journalid=journalid)
    result = searchtag.associate(userid_tag_adder, tags_two, submitid=submitid)
    assert result is None
    result = searchtag.associate(userid_tag_adder, tags_two, charid=charid)
    assert result["add_failure_restricted_tags"] is None
    assert "pearl" in result["remove_failure_owner_set_tags"]
    result = searchtag.associate(userid_tag_adder, tags_two, journalid=journalid)
    assert result["add_failure_restricted_tags"] is None
    assert "pearl" in result["remove_failure_owner_set_tags"]
    searchtag.associate(userid_owner, set(), submitid=submitid)
    searchtag.associate(userid_owner, set(), charid=charid)
    searchtag.associate(userid_owner, set(), journalid=journalid)

    """Test the result:result variant (restricted tags added, tag removal blocked)
    - Submission items will behave in the result:None variant
    - Character/Journal items will behave in the result:result manner"""
    restricted_tag = searchtag.parse_restricted_tags("profanity")
    searchtag.edit_user_tag_restrictions(userid_owner, restricted_tag)
    searchtag.associate(userid_owner, tags, submitid=submitid)
    searchtag.associate(userid_owner, tags, charid=charid)
    searchtag.associate(userid_owner, tags, journalid=journalid)
    # Effect upon adding this set: Remove user-set tag "pearl"; add restricted tag "profanity"
    tags_three = tags_two | {"profanity"}
    result = searchtag.associate(userid_tag_adder, tags_three, submitid=submitid)
    assert "profanity" in result["add_failure_restricted_tags"]
    assert result["remove_failure_owner_set_tags"] is None
    result = searchtag.associate(userid_tag_adder, tags_three, charid=charid)
    assert "profanity" in result["add_failure_restricted_tags"]
    assert "pearl" in result["remove_failure_owner_set_tags"]
    result = searchtag.associate(userid_tag_adder, tags_three, journalid=journalid)
    assert "profanity" in result["add_failure_restricted_tags"]
    assert "pearl" in result["remove_failure_owner_set_tags"]
Example #24
0
def create(userid, character, friends, tags, thumbfile, submitfile):
    # Make temporary filenames
    tempthumb = files.get_temporary(userid, "thumb")
    tempsubmit = files.get_temporary(userid, "char")

    # Determine filesizes
    thumbsize = len(thumbfile)
    submitsize = len(submitfile)

    # Check invalid title or rating
    if not character.char_name:
        raise WeasylError("characterNameInvalid")
    elif not character.rating:
        raise WeasylError("ratingInvalid")
    profile.check_user_rating_allowed(userid, character.rating)

    # Write temporary thumbnail file
    if thumbsize:
        files.easyupload(tempthumb, thumbfile, "image")
        thumbextension = files.get_extension_for_category(
            thumbfile, macro.ART_SUBMISSION_CATEGORY)
    else:
        thumbextension = None

    # Write temporary submission file
    if submitsize:
        files.easyupload(tempsubmit, submitfile, "image")
        submitextension = files.get_extension_for_category(
            submitfile, macro.ART_SUBMISSION_CATEGORY)
    else:
        submitextension = None

    # Check invalid file data
    if not submitsize:
        files.clear_temporary(userid)
        raise WeasylError("submitSizeZero")
    elif submitsize > 10 * _MEGABYTE:
        files.clear_temporary(userid)
        raise WeasylError("submitSizeExceedsLimit")
    elif thumbsize > 10 * _MEGABYTE:
        files.clear_temporary(userid)
        raise WeasylError("thumbSizeExceedsLimit")
    elif submitextension not in [".jpg", ".png", ".gif"]:
        files.clear_temporary(userid)
        raise WeasylError("submitType")
    elif thumbsize and thumbextension not in [".jpg", ".png", ".gif"]:
        files.clear_temporary(userid)
        raise WeasylError("thumbType")

    # Assign settings
    settings = []
    settings.append("f" if friends else "")
    settings.append(files.typeflag("submit", submitextension))
    settings.append(files.typeflag("cover", submitextension))
    settings = "".join(settings)

    # Insert submission data
    ch = define.meta.tables["character"]

    try:
        charid = define.engine.scalar(ch.insert().returning(ch.c.charid), {
            "userid": userid,
            "unixtime": arrow.now(),
            "char_name": character.char_name,
            "age": character.age,
            "gender": character.gender,
            "height": character.height,
            "weight": character.weight,
            "species": character.species,
            "content": character.content,
            "rating": character.rating.code,
            "settings": settings,
        })
    except PostgresError:
        files.clear_temporary(userid)
        raise

    # Assign search tags
    searchtag.associate(userid, tags, charid=charid)

    # Make submission file
    files.make_path(charid, "char")
    files.copy(tempsubmit, files.make_resource(userid, charid, "char/submit", submitextension))

    # Make cover file
    image.make_cover(tempsubmit, files.make_resource(userid, charid, "char/cover", submitextension))

    # Make thumbnail selection file
    if thumbsize:
        image.make_cover(
            tempthumb, files.make_resource(userid, charid, "char/.thumb"))

    thumbnail.create(userid, 0, 0, 0, 0, charid=charid, remove=False)

    # Create notifications
    welcome.character_insert(userid, charid, rating=character.rating.code,
                             settings=settings)

    # Clear temporary files
    files.clear_temporary(userid)

    define.metric('increment', 'characters')

    return charid
Example #25
0
def create_visual(userid, submission, friends_only, tags, imageURL, thumbfile,
                  submitfile, critique, create_notifications):
    if imageURL:
        resp = d.http_get(imageURL, timeout=5)
        submitfile = resp.content

    # Determine filesizes
    thumbsize = len(thumbfile)
    submitsize = len(submitfile)

    if not submitsize:
        raise WeasylError("submitSizeZero")
    elif thumbsize > 10 * _MEGABYTE:
        raise WeasylError("thumbSizeExceedsLimit")

    im = image.from_string(submitfile)
    submitextension = image.image_extension(im)
    if submitextension not in [".jpg", ".png", ".gif"]:
        raise WeasylError("submitType")
    if _limit(submitsize, submitextension):
        raise WeasylError("submitSizeExceedsLimit")

    # Check if the submission is spam
    is_spam = _check_for_spam(submission=submission, userid=userid)

    submit_file_type = submitextension.lstrip('.')
    submit_media_item = orm.MediaItem.fetch_or_create(
        submitfile, file_type=submit_file_type, im=im)
    check_for_duplicate_media(userid, submit_media_item.mediaid)
    cover_media_item = submit_media_item.ensure_cover_image(im)

    # Thumbnail stuff.
    # Always create a 'generated' thumbnail from the source image.
    with BytesIO(submitfile) as buf:
        thumbnail_formats = images_new.get_thumbnail(buf)

    thumb_generated, thumb_generated_file_type, thumb_generated_attributes = thumbnail_formats.compatible
    thumb_generated_media_item = orm.MediaItem.fetch_or_create(
        thumb_generated,
        file_type=thumb_generated_file_type,
        attributes=thumb_generated_attributes,
    )

    if thumbnail_formats.webp is None:
        thumb_generated_media_item_webp = None
    else:
        thumb_generated, thumb_generated_file_type, thumb_generated_attributes = thumbnail_formats.webp
        thumb_generated_media_item_webp = orm.MediaItem.fetch_or_create(
            thumb_generated,
            file_type=thumb_generated_file_type,
            attributes=thumb_generated_attributes,
        )

    # If requested, also create a 'custom' thumbnail.
    thumb_media_item = media.make_cover_media_item(thumbfile)
    if thumb_media_item:
        thumb_custom = images.make_thumbnail(image.from_string(thumbfile))
        thumb_custom_media_item = orm.MediaItem.fetch_or_create(
            thumb_custom.to_buffer(format=submit_file_type),
            file_type=submit_file_type,
            im=thumb_custom)

    # Assign settings
    settings = []
    settings.append("f" if friends_only else "")
    settings.append("q" if critique else "")
    settings = "".join(settings)

    # TODO(kailys): maintain ORM object
    db = d.connect()
    now = arrow.get()
    q = (d.meta.tables['submission'].insert().values([{
        "folderid":
        submission.folderid,
        "userid":
        userid,
        "unixtime":
        now,
        "title":
        submission.title,
        "content":
        submission.content,
        "subtype":
        submission.subtype,
        "rating":
        submission.rating.code,
        "settings":
        settings,
        "favorites":
        0,
        "is_spam":
        is_spam,
        "submitter_ip_address":
        submission.submitter_ip_address,
        "submitter_user_agent_id":
        submission.submitter_user_agent_id,
    }]).returning(d.meta.tables['submission'].c.submitid))
    submitid = db.scalar(q)

    orm.SubmissionMediaLink.make_or_replace_link(submitid, 'submission',
                                                 submit_media_item)
    orm.SubmissionMediaLink.make_or_replace_link(submitid, 'cover',
                                                 cover_media_item)

    orm.SubmissionMediaLink.make_or_replace_link(submitid,
                                                 'thumbnail-generated',
                                                 thumb_generated_media_item)

    if thumb_generated_media_item_webp is not None:
        orm.SubmissionMediaLink.make_or_replace_link(
            submitid, 'thumbnail-generated-webp',
            thumb_generated_media_item_webp)

    if thumb_media_item:
        orm.SubmissionMediaLink.make_or_replace_link(submitid,
                                                     'thumbnail-source',
                                                     thumb_media_item)
        orm.SubmissionMediaLink.make_or_replace_link(submitid,
                                                     'thumbnail-custom',
                                                     thumb_custom_media_item)

    # Assign search tags
    searchtag.associate(userid, tags, submitid=submitid)

    # Create notifications
    if create_notifications:
        _create_notifications(userid, submitid, submission.rating, settings)

    d.metric('increment', 'submissions')
    d.metric('increment', 'visualsubmissions')

    return submitid
Example #26
0
def test_clearing_all_tags():
    userid_owner = db_utils.create_user()
    userid_tag_adder = db_utils.create_user()
    journalid = db_utils.create_journal(userid_owner)
    charid = db_utils.create_character(userid_owner)
    submitid = db_utils.create_submission(userid_owner)
    searchtag.associate(userid_tag_adder, tags, submitid=submitid)
    searchtag.associate(userid_tag_adder, tags, charid=charid)
    searchtag.associate(userid_tag_adder, tags, journalid=journalid)

    # Clear all tags now that they were initially set
    empty_tags = set()
    searchtag.associate(userid_tag_adder, empty_tags, submitid=submitid)
    searchtag.associate(userid_tag_adder, empty_tags, charid=charid)
    searchtag.associate(userid_tag_adder, empty_tags, journalid=journalid)

    submitid_tags = searchtag.select(submitid=submitid)
    assert submitid_tags == []

    charid_tags = searchtag.select(charid=charid)
    assert charid_tags == []

    journalid_tags = searchtag.select(journalid=journalid)
    assert journalid_tags == []
Example #27
0
def create_literary(userid, submission, embedlink=None, friends_only=False, tags=None,
                    coverfile=None, thumbfile=None, submitfile=None, critique=False,
                    create_notifications=True):
    premium = d.get_premium(userid)

    if embedlink:
        check_google_doc_embed_data(embedlink)

    # Determine filesizes
    coversize = len(coverfile)
    thumbsize = len(thumbfile)
    submitsize = len(submitfile)

    if not submitsize and not embedlink:
        raise WeasylError("submitSizeZero")
    elif coversize > 10 * _MEGABYTE:
        raise WeasylError("coverSizeExceedsLimit")
    elif thumbsize > 10 * _MEGABYTE:
        raise WeasylError("thumbSizeExceedsLimit")

    if submitsize:
        submitextension = files.get_extension_for_category(submitfile, m.TEXT_SUBMISSION_CATEGORY)
        if submitextension is None:
            raise WeasylError("submitType")
        if _limit(submitsize, submitextension, premium):
            raise WeasylError("submitSizeExceedsLimit")
        submit_media_item = orm.fetch_or_create_media_item(
            submitfile, file_type=submitextension.lstrip('.'))
        check_for_duplicate_media(userid, submit_media_item.mediaid)
    else:
        submit_media_item = None

    thumb_media_item = media.make_cover_media_item(thumbfile)
    cover_media_item = media.make_cover_media_item(coverfile)
    if cover_media_item and not thumb_media_item:
        thumb_media_item = cover_media_item

    # Assign settings
    settings = []
    settings.append("f" if friends_only else "")
    settings.append("q" if critique else "")
    if embedlink:
        settings.append('D')
    settings = "".join(settings)

    # Create submission
    # TODO(kailys): use ORM object
    db = d.connect()
    now = arrow.get()
    try:
        q = (
            d.meta.tables['submission'].insert().values([{
                "folderid": submission.folderid,
                "userid": userid,
                "unixtime": now,
                "title": submission.title,
                "content": submission.content,
                "subtype": submission.subtype,
                "rating": submission.rating.code,
                "settings": settings,
                "sorttime": now,
            }])
            .returning(d.meta.tables['submission'].c.submitid))
        submitid = db.scalar(q)
        if embedlink:
            q = (d.meta.tables['google_doc_embeds'].insert()
                 .values(submitid=submitid, embed_url=embedlink))
            db.execute(q)
    except:
        files.clear_temporary(userid)
        raise

    # Assign search tags
    searchtag.associate(userid, tags, submitid=submitid)

    if submit_media_item:
        orm.SubmissionMediaLink.make_or_replace_link(submitid, 'submission', submit_media_item)
    if cover_media_item:
        orm.SubmissionMediaLink.make_or_replace_link(submitid, 'cover', cover_media_item)
    if thumb_media_item:
        orm.SubmissionMediaLink.make_or_replace_link(submitid, 'thumbnail-source', thumb_media_item)

    # Create notifications
    if create_notifications:
        _create_notifications(userid, submitid, submission.rating, settings,
                              submission.title, tags)

    # Clear temporary files
    files.clear_temporary(userid)

    d.metric('increment', 'submissions')
    d.metric('increment', 'literarysubmissions')

    return submitid, bool(thumb_media_item)
Example #28
0
def create_visual(userid, submission,
                  friends_only, tags, imageURL, thumbfile,
                  submitfile, critique, create_notifications):
    premium = d.get_premium(userid)

    if imageURL:
        resp = d.http_get(imageURL, timeout=5)
        submitfile = resp.content

    # Determine filesizes
    thumbsize = len(thumbfile)
    submitsize = len(submitfile)

    if not submitsize:
        files.clear_temporary(userid)
        raise WeasylError("submitSizeZero")
    elif thumbsize > 10 * _MEGABYTE:
        files.clear_temporary(userid)
        raise WeasylError("thumbSizeExceedsLimit")

    im = image.from_string(submitfile)
    submitextension = image.image_extension(im)
    if _limit(submitsize, submitextension, premium):
        raise WeasylError("submitSizeExceedsLimit")
    elif submitextension not in [".jpg", ".png", ".gif"]:
        raise WeasylError("submitType")
    submit_file_type = submitextension.lstrip('.')
    submit_media_item = orm.fetch_or_create_media_item(
        submitfile, file_type=submit_file_type, im=im)
    check_for_duplicate_media(userid, submit_media_item.mediaid)
    cover_media_item = submit_media_item.ensure_cover_image(im)

    # Thumbnail stuff.
    # Always create a 'generated' thumbnail from the source image.
    thumb_generated = images.make_thumbnail(im)
    if thumb_generated is im:
        thumb_generated_media_item = submit_media_item
    else:
        thumb_generated_media_item = orm.fetch_or_create_media_item(
            thumb_generated.to_buffer(format=submit_file_type), file_type=submit_file_type,
            im=thumb_generated)
    # If requested, also create a 'custom' thumbnail.
    thumb_media_item = media.make_cover_media_item(thumbfile)
    if thumb_media_item:
        thumb_custom = images.make_thumbnail(image.from_string(thumbfile))
        thumb_custom_media_item = orm.fetch_or_create_media_item(
            thumb_custom.to_buffer(format=submit_file_type), file_type=submit_file_type,
            im=thumb_custom)

    # Assign settings
    settings = []
    settings.append("f" if friends_only else "")
    settings.append("q" if critique else "")
    settings = "".join(settings)

    # TODO(kailys): maintain ORM object
    db = d.connect()
    now = arrow.get()
    q = (
        d.meta.tables['submission'].insert().values([{
            "folderid": submission.folderid,
            "userid": userid,
            "unixtime": now,
            "title": submission.title,
            "content": submission.content,
            "subtype": submission.subtype,
            "rating": submission.rating.code,
            "settings": settings,
            "sorttime": now,
        }]).returning(d.meta.tables['submission'].c.submitid))
    submitid = db.scalar(q)

    orm.SubmissionMediaLink.make_or_replace_link(
        submitid, 'submission', submit_media_item)
    orm.SubmissionMediaLink.make_or_replace_link(
        submitid, 'cover', cover_media_item)
    orm.SubmissionMediaLink.make_or_replace_link(
        submitid, 'thumbnail-generated', thumb_generated_media_item)
    if thumb_media_item:
        orm.SubmissionMediaLink.make_or_replace_link(submitid, 'thumbnail-source', thumb_media_item)
        orm.SubmissionMediaLink.make_or_replace_link(
            submitid, 'thumbnail-custom', thumb_custom_media_item)

    # Assign search tags
    searchtag.associate(userid, tags, submitid=submitid)

    # Create notifications
    if create_notifications:
        _create_notifications(userid, submitid, submission.rating, settings,
                              submission.title, tags)

    d.metric('increment', 'submissions')
    d.metric('increment', 'visualsubmissions')

    return submitid
Example #29
0
def create(userid, character, friends, tags, thumbfile, submitfile):
    # Make temporary filenames
    tempthumb = files.get_temporary(userid, "thumb")
    tempsubmit = files.get_temporary(userid, "char")

    # Determine filesizes
    thumbsize = len(thumbfile)
    submitsize = len(submitfile)

    # Check invalid title or rating
    if not character.char_name:
        raise WeasylError("characterNameInvalid")
    elif not character.rating:
        raise WeasylError("ratingInvalid")
    profile.check_user_rating_allowed(userid, character.rating)

    # Write temporary thumbnail file
    if thumbsize:
        files.write(tempthumb, thumbfile)
        thumbextension = files.get_extension_for_category(
            thumbfile, macro.ART_SUBMISSION_CATEGORY)
    else:
        thumbextension = None

    # Write temporary submission file
    if submitsize:
        files.write(tempsubmit, submitfile)
        submitextension = files.get_extension_for_category(
            submitfile, macro.ART_SUBMISSION_CATEGORY)
    else:
        submitextension = None

    # Check invalid file data
    if not submitsize:
        files.clear_temporary(userid)
        raise WeasylError("submitSizeZero")
    elif submitsize > _MAIN_IMAGE_SIZE_LIMIT:
        files.clear_temporary(userid)
        raise WeasylError("submitSizeExceedsLimit")
    elif thumbsize > 10 * _MEGABYTE:
        files.clear_temporary(userid)
        raise WeasylError("thumbSizeExceedsLimit")
    elif submitextension not in [".jpg", ".png", ".gif"]:
        files.clear_temporary(userid)
        raise WeasylError("submitType")
    elif thumbsize and thumbextension not in [".jpg", ".png", ".gif"]:
        files.clear_temporary(userid)
        raise WeasylError("thumbType")

    # Assign settings
    settings = []
    settings.append("f" if friends else "")
    settings.append(files.typeflag("submit", submitextension))
    settings.append(files.typeflag("cover", submitextension))
    settings = "".join(settings)

    # Insert submission data
    ch = define.meta.tables["character"]

    try:
        charid = define.engine.scalar(ch.insert().returning(ch.c.charid), {
            "userid": userid,
            "unixtime": arrow.now(),
            "char_name": character.char_name,
            "age": character.age,
            "gender": character.gender,
            "height": character.height,
            "weight": character.weight,
            "species": character.species,
            "content": character.content,
            "rating": character.rating.code,
            "settings": settings,
        })
    except PostgresError:
        files.clear_temporary(userid)
        raise

    # Assign search tags
    searchtag.associate(userid, tags, charid=charid)

    # Make submission file
    files.make_character_directory(charid)
    files.copy(tempsubmit, files.make_resource(userid, charid, "char/submit", submitextension))

    # Make cover file
    image.make_cover(tempsubmit, files.make_resource(userid, charid, "char/cover", submitextension))

    # Make thumbnail selection file
    if thumbsize:
        image.make_cover(
            tempthumb, files.make_resource(userid, charid, "char/.thumb"))

    thumbnail.create(0, 0, 0, 0, charid=charid, remove=False)

    # Create notifications
    welcome.character_insert(userid, charid, rating=character.rating.code,
                             settings=settings)

    # Clear temporary files
    files.clear_temporary(userid)

    define.metric('increment', 'characters')

    return charid
Example #30
0
def test_removing_tags():
    userid_owner = db_utils.create_user()
    userid_tag_adder = db_utils.create_user()
    journalid = db_utils.create_journal(userid_owner)
    charid = db_utils.create_character(userid_owner)
    submitid = db_utils.create_submission(userid_owner)
    searchtag.associate(userid_tag_adder, tags, submitid=submitid)
    searchtag.associate(userid_tag_adder, tags, charid=charid)
    searchtag.associate(userid_tag_adder, tags, journalid=journalid)

    # Remove the 'pearl' tag
    searchtag.associate(userid_tag_adder, tags_two, submitid=submitid)
    searchtag.associate(userid_tag_adder, tags_two, charid=charid)
    searchtag.associate(userid_tag_adder, tags_two, journalid=journalid)

    submission_tags = searchtag.select(submitid=submitid)
    assert tags_two == set(submission_tags)

    character_tags = searchtag.select(charid=charid)
    assert tags_two == set(character_tags)

    journal_tags = searchtag.select(journalid=journalid)
    assert tags_two == set(journal_tags)
Example #31
0
def create_literary(userid,
                    submission,
                    embedlink=None,
                    friends_only=False,
                    tags=None,
                    coverfile=None,
                    thumbfile=None,
                    submitfile=None,
                    critique=False,
                    create_notifications=True):
    if embedlink:
        check_google_doc_embed_data(embedlink)

    # Determine filesizes
    coversize = len(coverfile)
    thumbsize = len(thumbfile)
    submitsize = len(submitfile)

    if not submitsize and not embedlink:
        raise WeasylError("submitSizeZero")
    elif coversize > 10 * _MEGABYTE:
        raise WeasylError("coverSizeExceedsLimit")
    elif thumbsize > 10 * _MEGABYTE:
        raise WeasylError("thumbSizeExceedsLimit")

    if submitsize:
        submitextension = files.get_extension_for_category(
            submitfile, m.TEXT_SUBMISSION_CATEGORY)
        if submitextension is None:
            raise WeasylError("submitType")
        if _limit(submitsize, submitextension):
            raise WeasylError("submitSizeExceedsLimit")
        submit_media_item = orm.MediaItem.fetch_or_create(
            submitfile, file_type=submitextension.lstrip('.'))
        check_for_duplicate_media(userid, submit_media_item.mediaid)
    else:
        submit_media_item = None

    # Check if the submission is spam
    is_spam = _check_for_spam(submission=submission, userid=userid)

    thumb_media_item = media.make_cover_media_item(thumbfile)
    cover_media_item = media.make_cover_media_item(coverfile)
    if cover_media_item and not thumb_media_item:
        thumb_media_item = cover_media_item

    # Assign settings
    settings = []
    settings.append("f" if friends_only else "")
    settings.append("q" if critique else "")
    if embedlink:
        settings.append('D')
    settings = "".join(settings)

    # Create submission
    # TODO(kailys): use ORM object
    db = d.connect()
    now = arrow.get()
    q = (d.meta.tables['submission'].insert().values([{
        "folderid":
        submission.folderid,
        "userid":
        userid,
        "unixtime":
        now,
        "title":
        submission.title,
        "content":
        submission.content,
        "subtype":
        submission.subtype,
        "rating":
        submission.rating.code,
        "settings":
        settings,
        "favorites":
        0,
        "is_spam":
        is_spam,
        "submitter_ip_address":
        submission.submitter_ip_address,
        "submitter_user_agent_id":
        submission.submitter_user_agent_id,
    }]).returning(d.meta.tables['submission'].c.submitid))
    submitid = db.scalar(q)
    if embedlink:
        q = (d.meta.tables['google_doc_embeds'].insert().values(
            submitid=submitid, embed_url=embedlink))
        db.execute(q)

    # Assign search tags
    searchtag.associate(userid, tags, submitid=submitid)

    if submit_media_item:
        orm.SubmissionMediaLink.make_or_replace_link(submitid, 'submission',
                                                     submit_media_item)
    if cover_media_item:
        orm.SubmissionMediaLink.make_or_replace_link(submitid, 'cover',
                                                     cover_media_item)
    if thumb_media_item:
        orm.SubmissionMediaLink.make_or_replace_link(submitid,
                                                     'thumbnail-source',
                                                     thumb_media_item)

    # Create notifications
    if create_notifications:
        _create_notifications(userid, submitid, submission.rating, settings)

    d.metric('increment', 'submissions')
    d.metric('increment', 'literarysubmissions')

    return submitid, bool(thumb_media_item)
Example #32
0
def create_visual(userid, submission,
                  friends_only, tags, imageURL, thumbfile,
                  submitfile, critique, create_notifications):
    premium = d.get_premium(userid)

    if imageURL:
        resp = d.http_get(imageURL, timeout=5)
        submitfile = resp.content

    # Determine filesizes
    thumbsize = len(thumbfile)
    submitsize = len(submitfile)

    if not submitsize:
        files.clear_temporary(userid)
        raise WeasylError("submitSizeZero")
    elif thumbsize > 10 * _MEGABYTE:
        files.clear_temporary(userid)
        raise WeasylError("thumbSizeExceedsLimit")

    im = image.from_string(submitfile)
    submitextension = image.image_extension(im)
    if _limit(submitsize, submitextension, premium):
        raise WeasylError("submitSizeExceedsLimit")
    elif submitextension not in [".jpg", ".png", ".gif"]:
        raise WeasylError("submitType")
    submit_file_type = submitextension.lstrip('.')
    submit_media_item = orm.fetch_or_create_media_item(
        submitfile, file_type=submit_file_type, im=im)
    check_for_duplicate_media(userid, submit_media_item.mediaid)
    cover_media_item = submit_media_item.ensure_cover_image(im)

    # Thumbnail stuff.
    # Always create a 'generated' thumbnail from the source image.
    thumb_generated = images.make_thumbnail(im)
    if thumb_generated is im:
        thumb_generated_media_item = submit_media_item
    else:
        thumb_generated_media_item = orm.fetch_or_create_media_item(
            thumb_generated.to_buffer(format=submit_file_type), file_type=submit_file_type,
            im=thumb_generated)
    # If requested, also create a 'custom' thumbnail.
    thumb_media_item = media.make_cover_media_item(thumbfile)
    if thumb_media_item:
        thumb_custom = images.make_thumbnail(image.from_string(thumbfile))
        thumb_custom_media_item = orm.fetch_or_create_media_item(
            thumb_custom.to_buffer(format=submit_file_type), file_type=submit_file_type,
            im=thumb_custom)

    # Assign settings
    settings = []
    settings.append("f" if friends_only else "")
    settings.append("q" if critique else "")
    settings = "".join(settings)

    # TODO(kailys): maintain ORM object
    db = d.connect()
    now = arrow.get()
    q = (
        d.meta.tables['submission'].insert().values([{
            "folderid": submission.folderid,
            "userid": userid,
            "unixtime": now,
            "title": submission.title,
            "content": submission.content,
            "subtype": submission.subtype,
            "rating": submission.rating.code,
            "settings": settings,
            "sorttime": now,
        }]).returning(d.meta.tables['submission'].c.submitid))
    submitid = db.scalar(q)

    orm.SubmissionMediaLink.make_or_replace_link(
        submitid, 'submission', submit_media_item)
    orm.SubmissionMediaLink.make_or_replace_link(
        submitid, 'cover', cover_media_item)
    orm.SubmissionMediaLink.make_or_replace_link(
        submitid, 'thumbnail-generated', thumb_generated_media_item)
    if thumb_media_item:
        orm.SubmissionMediaLink.make_or_replace_link(submitid, 'thumbnail-source', thumb_media_item)
        orm.SubmissionMediaLink.make_or_replace_link(
            submitid, 'thumbnail-custom', thumb_custom_media_item)

    # Assign search tags
    searchtag.associate(userid, tags, submitid=submitid)

    # Create notifications
    if create_notifications:
        _create_notifications(userid, submitid, submission.rating, settings,
                              submission.title, tags)

    d.metric('increment', 'submissions')
    d.metric('increment', 'visualsubmissions')

    return submitid