import atexit
from mice import s
import uuid
from flask import Flask, jsonify

PARENT_CHANNEL = "diss_club"

def get_root_channel(server):
    channels = s.getChannels()
    root_channel = [i for i in channels.values() if i.name == PARENT_CHANNEL][0]
    return root_channel


app = Flask(__name__)

USERS = {}

@app.route("/register/<code>")
def register(code):
    if code in USERS:
        return jsonify(msg="already created")
    channel_name = str(uuid.uuid4())
    password = "123"
    username = "diss%s" % (len(USERS.items()) + 1)
    USERS[code] = {"channel": channel_name, "password": password, "username": username}

    root_channel = get_root_channel(s)
    s.addChannel(channel_name, root_channel.id)
    return jsonify(msg="ok")


def murmur_cleanup():
    channels = s.getChannels()
    root_channel = get_root_channel(s)
    diss_channels = [i for i in channels.values() if i.parent == root_channel.id]
    for (code, value) in USERS.iteritems():
        found = filter(lambda x: x.name == value['channel'], diss_channels)
        for channel in found:
            s.removeChannel(channel.id)

atexit.register(murmur_cleanup)


if __name__ == '__main__':
    app.run()

Изменить пасту