Example #1
0
def main():
    header = u"Content-Type: text/html\n"
    content = []
    blog = Blog()
    post_datas = FieldStorage()
    mode = get_value(post_datas, "mode", "add")
    user = get_value(post_datas, "user")
    password = get_value(post_datas, "password")
    hash = get_value(post_datas, "hash")
    user, power = blog.is_user(user, password, hash)
    post_id = get_value(post_datas, "post")
    target = get_value(post_datas, "target")
    target_password = get_value(post_datas, "target_password")
    power = get_value(post_datas, "power")
    power_exists = (power and power.isdigit() and int(power) < VOICE)
    if not user:
        content.append(protocol.error(_(u"Wrong username/password")))
    elif not (blog.has_power(user, OP if mode == "add" else ROOT) or
            user == post[0]["author"]):
        content.append(protocol.error(_(u"You don't have the permission to do"
                                         " that.")))
    elif not (target and (power_exists and password and mode == "add") or 
             ((power_exists or password) and mode == "edit") or mode == "del"):
        content.append(protocol.error(_(u"Post datas are not set")))
    # It's ok.
    elif mode == "add":
        if not (target_password and power):
            content.append(protocol.error(_(u"Please set a password and"
                                             " permissions.")))
        else:
            ret = blog.add_user(target, target_password, power)
            content.append(protocol.success(ret) if ret else
                    protocol.error(_(u"User exists")))
    elif mode == "edit":
        blog.set_user(target, target_password if target_password else None,
                power if power else None)
        content.append(protocol.success())
    elif mode == "del":
        blog.del_user(target)
        content.append(protocol.success())
    else:
        content.append(protocol.error(_(u"Wrong mode.")))
    print header
    print u"\n".join(content).encode("utf-8", "replace")
Example #2
0
def main():
    header = u"Content-Type: text/html\n"
    content = []
    blog = Blog()
    post_datas = FieldStorage()
    mode = get_value(post_datas, "mode", "retrieve")
    user = get_value(post_datas, "user")
    password = get_value(post_datas, "password")
    hash = get_value(post_datas, "hash")
    user, power = blog.is_user(user, password, hash)
    post_id = get_value(post_datas, "post", "")
    if mode in ("retrieve", "edit", "del") and not (post_id and
            post_id.isdigit()):
        content.append(protocol.error(_(u"Variable 'post' is not set.")))

    elif mode == "retrieve":
        post = blog.get_posts(post_id=int(post_id))
        if post:
            content.append(protocol.success(protocol.format_post(post)))
        else:
            content.append(protocol.error(_(u"No id %s." % post_id)))

    elif mode == "add":
        if user and blog.has_power(user, VOICE):
            datas = {"author": user, "date": time()}
            for k, d in (("title", u"No title"), ("tags", u""),
                    ("content", u"")):
                datas[k] = get_value(post_datas, k, d)
            new_id = blog.add_post(Post(**datas))
            content.append(protocol.success(unicode(new_id)))
        else:
            content.append(protocol.error(_(u"Wrong username/password.")))

    elif mode == "edit":
        post_id = get_value(post_datas, "post", "")
        datas = {"author": user, "date": time(), "id": int(post_id)}
        for k, d in (("title", u"No title"), ("tags", u""),
                ("content", u"")):
            datas[k] = get_value(post_datas, k, d)
        post = blog.get_posts(post_id=int(post_id))
        if post:
            if user == post["author"] or blog.has_power(user, HOP):
                blog.edit_post(Post(**datas))
                content.append(protocol.success(unicode(datas["id"])))
            else:
                content.append(protocol.error(_(u"You don't have the permission"
                                                 " to do that.")))
        else:
            content.append(protocol.error(_(u"No id %s.")) % post_id)

    elif mode == "del":
        post_id = get_value(post_datas, "post", "")
        post = blog.get_posts(post_id=int(post_id))
        if user == post["author"] or blog.has_power(user, HOP):
            if blog.del_post(post_id):
                content.append(protocol.error(_(u"(internal)")))
            else:
                content.append(protocol.success())
        else:
            content.append(protocol.error(_(u"You don't have the permission to"
                                             " do that.")))

    else:
        content.append(protocol.error(_(u"Wrong mode.")))

    print header
    print u"\n".join(content).encode("utf-8", "replace")