예제 #1
0
def tripcode_test():
    assert make_tripcode("tripcode") == "3GqYIJ3Obs"
    assert make_tripcode("tripcod3") == "U6qBEwdIxU"
    assert make_tripcode("##") == make_tripcode("###")
예제 #2
0
def finish_post(post, user, thread, files, section, ip, useragent, feed=None):
    # Set some common attributes.
    post.ip = ip
    post.date = datetime.now()
    post.password = get_key(post.password)
    post.is_op_post = not thread

    if models.Wordfilter.objects.scan(post.message):
        raise ValidationError(_("Your post contains blacklisted word."))

    if not thread:
        section = models.Section.objects.get(slug=section)
        thread_o = models.Thread(section=section, bump=post.date)
    else:
        thread_o = models.Thread.objects.get(id=thread)
        section = thread_o.section
        if thread_o.is_closed and not user:
            raise ValidationError(
                _("This thread is closed, you cannot post to it.")
            )

    section_is_feed = section.type == 3

    if files:
        allowed = section.allowed_filetypes()
        extension = allowed.get(files.content_type)
        if not extension:
            raise InvalidFileError(_("Invalid file type"))

        lim = section.filesize_limit
        if files.size > lim > 0:
            raise InvalidFileError(_("Too big file"))

        m = md5()
        map(m.update, files.chunks())
        # TODO: Check if this file already exists.
        #       (Is this really needed at all?)
        #if models.File.objects.filter(hash=m.hexdigest()).count() > 0:
        #    raise InvalidFileError(_("This file already exists"))

        filetype = models.FileType.objects.filter(extension=extension)[0]
        post.file = handle_uploaded_file(
            models.File(
                name=files.name, type=filetype, hash=m.hexdigest(),
                file=DjangoFile(files), image_height=0, image_width=0
            )
        )
    else:
        if not post.message:
            raise ValidationError(
                _("Enter post message or attach a file to your post")
            )
        elif not thread:
            if section.force_files:
                raise ValidationError(
                    _("You need to upload file to create new thread.")
                )
            elif section_is_feed and not user:
                raise NotAuthenticatedError(
                    _(
                        "Authentication required to "
                        "create threads in this section"
                    )
                )

    # Bump the thread.
    if (post.email.lower() != "sage"
    and thread and thread_o.posts().count() < section.bumplimit):
        thread_o.bump = post.date

    # Parse the signature.
    author, sign = (post.poster.split("!", 1) + [""])[:2]

    if sign == "OP" and thread and post.password == thread_o.op_post.password:
        post.tripcode = "!OP"

    if sign == "name" and user:
        if user.is_superuser:
            post.tripcode = "!{}".format(user.username)
        else:
            post.tripcode = "!Mod"

    # Parse the tripcode.
    author, tripcode = (author.split("#", 1) + [""])[:2]
    if tripcode:
        post.tripcode = make_tripcode(tripcode)
    post.poster = author

    # Force-set the author name on some boards.
    if not post.poster or section.anonymity:
        post.poster = section.default_name

    if post.email == "mvtn".encode("rot13"):  # easter egg o/
        s = u"\u5350"
        post.poster = post.email = post.topic = s * 10
        post.message = (s + u" ") * 50

    if section.type == 4:
        # 4 == /int/ - International
        # Assign the country code to this post.
        post.data = {
            "country_code": models.GeoIP().country(post.ip)["country_code"]
        }
    elif section.type == 5:
        # 5 == /bugs/ - Bugtracker
        # Display the user's browser name derived from HTTP User-Agent.
        parsed = parse_user_agent(useragent)
        browser = parsed.get("browser", {"name": "Unknown", "version": ""})
        platform = parsed.get("os", {"name": "Unknown"})

        browser["os_name"] = platform["name"]
        browser["os_version"] = parsed.get("flavor", {}).get("version", "")
        browser["raw"] = useragent
        post.data = {"useragent": browser}

    if not thread:
        thread_o.save(rebuild_cache=False)
        post.thread = thread_o
    post.pid = section.pid_incr()
    post.save()
    thread_o.save()

    if feed is not None:
        thread_id = int(thread or post.id)
        feed.add(thread_id)
    return post