Ejemplo n.º 1
0
def show_networks():
    rep = DI.get_repository()
    blk = DI.get_blockchain()
    upd = Updater(rep, blk)
    upd.check()

    networks = rep.read_networks()
    return render_template('list/networks.html',
                           title='Main',
                           nav='tags',
                           networks=networks)
Ejemplo n.º 2
0
def page_users(param_sub, username):
    local_storage = DI.create_local_storage()
    config = DI.get_config()
    cnt = DI.create_container()

    if param_sub == '':
        users = cnt.list_users()
        users_list = []
        for user in users:
            if config['user'] == user:
                user = '******' + user + '>'
            users_list.append(user)

        if len(users_list) == 0:
            print("You have no users")
            print("type 'cc.py U+username' to generate user")
        else:
            users_list = sorted(users_list)
            print("My users: {}".format(", ".join(users_list)))

    elif param_sub == '+':
        if cnt.user_exists(username):
            print("User {} olready exists".format(username))
        else:
            ask = input(
                "The user generation may take several minutes on slow machines, GENERATE user {} (y/n) :"
                .format(username))
            if ask.lower()[0:1] == 'y':
                if cnt.generate_user(username):
                    print("User {} generated OK".format(username))
    elif param_sub == '-':
        if cnt.user_exists(username):
            ask = input(
                "Delete user {}, you will not be able to restore it (y/n) :".
                format(username))
            if ask.lower()[0:1] == 'y':
                if cnt.delete_user(username):
                    print("User {} deleted OK".format(username))
        else:
            print("User '{}' does not exist".format(username))
    else:
        username = param_sub + username
        if cnt.user_exists(username):
            config = local_storage.get_config()
            config['user'] = username
            local_storage.save_config(config)
            print("User {} selected OK".format(username))
        else:
            print("User '{}' does not exist".format(username))

    return
Ejemplo n.º 3
0
def edit_description(name):
    if 'days' not in request.form:
        return render_template('error.html',
                               error='No "days" in POST params',
                               code='NONE',
                               backlink='/my/edit/' + name)
    if 'url' not in request.form:
        return render_template('error.html',
                               error='No "url" in POST params',
                               code='NONE',
                               backlink='/my/edit/' + name)
    if 'description' not in request.form:
        return render_template('error.html',
                               error='No "description" in POST params',
                               code='NONE',
                               backlink='/my/edit/' + name)

    resource = {
        'url': request.form['url'],
        'description': request.form['description']
    }

    blk = DI.get_blockchain()
    data = blk.edit_resource(name, resource, int(request.form['days']))

    if data['error'] and data['error']['code'] < 0:
        return render_template('error.html',
                               error=data['error']['message'],
                               code=data['error']['code'],
                               backlink='/my_descriptions')
    else:
        return render_template('ok.html',
                               nav='my',
                               message='Resource "' + name + '" edited OK',
                               backlink='/my_descriptions')
Ejemplo n.º 4
0
def show_edit(resource):
    blk = DI.get_blockchain()
    data = blk.show_resource(resource)
    resourse = data['result']
    decoded = json.loads(resourse['value'])
    resourse.update(decoded)
    return render_template('edit.html', nav='my', resourse=resourse)
Ejemplo n.º 5
0
def add(resource_id):
    if 'days' not in request.form:
        return render_template('error.html',
                               error='No "days" in POST params',
                               code='NONE',
                               backlink='/add/' + resource_id)
    if 'url' not in request.form:
        return render_template('error.html',
                               error='No "url" in POST params',
                               code='NONE',
                               backlink='/add/' + resource_id)
    if 'description' not in request.form:
        return render_template('error.html',
                               error='No "description" in POST params',
                               code='NONE',
                               backlink='/add/' + resource_id)

    resource = {
        'url': request.form['url'],
        'description': request.form['description']
    }
    blk = DI.get_blockchain()
    data = blk.post_resource(resource, int(request.form['days']))

    if data['error'] and data['error']['code'] < 0:
        return render_template('error.html',
                               error=data['error']['message'],
                               code=data['error']['code'],
                               backlink='/my')
    else:
        return render_template('ok.html',
                               message='Resource submitted OK',
                               backlink='/my')
Ejemplo n.º 6
0
def page_config(param_full):
    local_storage = DI.create_local_storage()
    config = local_storage.get_config()

    if param_full == '':
        # out config
        pp = pprint.PrettyPrinter(indent=4)
        pp.pprint(config)
    else:
        # updating values and saving config.json
        value_pairs = param_full.split('+')

        for value_pair in value_pairs:
            value_pair_list = value_pair.split('=')
            if len(value_pair_list) == 2:
                if value_pair_list[0] in [
                        'container', 'editor', 'remote', 'upload', 'user'
                ]:
                    config[value_pair_list[0]] = value_pair_list[1]
                    print("Param {} set to {}".format(value_pair_list[0],
                                                      value_pair_list[1]))
                else:
                    print("Invalid param {}".format(value_pair_list[0]))
        local_storage.save_config(config)
    return
Ejemplo n.º 7
0
def hello_world():
    blk = DI.get_blockchain()
    try:
        data = blk.get_info()
        return render_template('main.html', data=data['result'])
    except Exception:
        return redirect('/config')
Ejemplo n.º 8
0
def show_add(resource_id):
    rep = DI.get_repository()
    resource = rep.read_resource(resource_id)
    tags = rep.read_tags(resource['network_id'], resource['lang_id'],
                         resource['type_id'])
    return render_template('add.html',
                           nav='submit',
                           resource=resource,
                           tags_id=tags[0]['id'])
Ejemplo n.º 9
0
def search():
    rep = DI.get_repository()
    blk = DI.get_blockchain()
    upd = Updater(rep, blk)
    upd.check()

    resources = []
    if request.form['type'] == 'url':
        resources = rep.search_resources_by_url(request.form['search'])
    elif request.form['type'] == 'description':
        resources = rep.search_resources_by_description(request.form['search'])
    # print(resources)
    return render_template('list/search.html',
                           nav='search',
                           title='Search',
                           search=request.form['search'],
                           type=request.form['type'],
                           resources=resources)
Ejemplo n.º 10
0
def show_langs(network_id):
    rep = DI.get_repository()
    langs = rep.read_langs(network_id)
    network = rep.get_network_name_by_id(network_id)
    return render_template('list/langs.html',
                           title='Main',
                           nav='tags',
                           langs=langs,
                           network_id=network_id,
                           network=network)
Ejemplo n.º 11
0
def delete(resource):
    blk = DI.get_blockchain()
    data = blk.delete_resource(resource)

    if data['error'] and data['error']['code'] < 0:
        return render_template('error.html',
                               error=data['error']['message'],
                               code=data['error']['code'],
                               backlink='/my')
    else:
        return render_template('ok.html', message='DELETED OK', backlink='/my')
Ejemplo n.º 12
0
def submit():
    if 'days' not in request.form:
        return render_template('error.html',
                               error='No "days" in POST params',
                               code='NONE',
                               backlink='/submit')
    if 'url' not in request.form:
        return render_template('error.html',
                               error='No "url" in POST params',
                               code='NONE',
                               backlink='/submit')
    if 'network' not in request.form:
        return render_template('error.html',
                               error='No "network" in POST params',
                               code='NONE',
                               backlink='/submit')
    if 'type' not in request.form:
        return render_template('error.html',
                               error='No "type" in POST params',
                               code='NONE',
                               backlink='/submit')
    if 'tags' not in request.form:
        return render_template('error.html',
                               error='No "tags" in POST params',
                               code='NONE',
                               backlink='/submit')
    if 'description' not in request.form:
        return render_template('error.html',
                               error='No "description" in POST params',
                               code='NONE',
                               backlink='/submit')

    resource = {
        'url': request.form['url'],
        'network': request.form['network'],
        'type': request.form['type'],
        'lang': request.form['lang'],
        'tags': request.form['tags'],
        'description': request.form['description']
    }
    blk = DI.get_blockchain()
    data = blk.post_resource(resource, int(request.form['days']))

    if data['error'] and data['error']['code'] < 0:
        return render_template('error.html',
                               error=data['error']['message'],
                               code=data['error']['code'],
                               backlink='/my')
    else:
        return render_template('ok.html',
                               message='Resource submitted OK',
                               backlink='/my')
Ejemplo n.º 13
0
def show_types(network_id, lang_id):
    rep = DI.get_repository()
    types = rep.read_types(network_id, lang_id)
    network = rep.get_network_name_by_id(network_id)
    lang = rep.get_lang_name_by_id(lang_id)
    return render_template('list/types.html',
                           title='Main',
                           nav='tags',
                           types=types,
                           network_id=network_id,
                           lang_id=lang_id,
                           network=network,
                           lang=lang)
Ejemplo n.º 14
0
def test():
    # rep = DI.get_repository()
    # res = rep.search_resources_by_url('youtube')
    # print(res)
    # res = rep.search_resources_by_description('Биткоин')
    # print(res)
    # rep = DI.get_repository()
    # blk = DI.get_blockchain()
    # upd = Updater(rep, blk)
    # print(upd.check())

    blk = DI.get_blockchain()
    print(blk.check())
    return 'OK'


# app.run()
Ejemplo n.º 15
0
def post_config():
    Config.save_config({
        'host': request.form['host'],
        'port': int(request.form['port']),
        'user': request.form['user'],
        'password': request.form['password']
    })

    blk = DI.get_blockchain()
    if blk.check():
        result = 'Connection OK =)'
    else:
        result = 'FAILED'

    return render_template('config.html',
                           nav='config',
                           title='Config',
                           result=result,
                           data=request.form)
Ejemplo n.º 16
0
def show_my():
    blk = DI.get_blockchain()
    if not blk.check():
        return render_template('error_rpc.html', nav='my')

    records = blk.get_my()
    my_records = []

    for record in records:
        record['decoded'] = json.loads(record['value'])
        record['expires_in'] = round(record['expires_in'] / 175)

    for record in records:
        if 'network' in record['decoded'] and 'type' in record[
                'decoded'] and 'lang' in record[
                    'decoded'] and 'tags' in record['decoded']:
            my_records.append(record)

    return render_template('my.html', records=my_records, nav='my')
Ejemplo n.º 17
0
def show_resources(network_id, lang_id, type_id, tags_id):
    rep = DI.get_repository()
    resources = rep.read_resources(network_id, lang_id, type_id, tags_id)
    network = rep.get_network_name_by_id(network_id)
    lang = rep.get_lang_name_by_id(lang_id)
    type = rep.get_type_name_by_id(type_id)
    tag = rep.get_tag_name_by_id(tags_id)
    return render_template('list/resources.html',
                           title='Main',
                           nav='tags',
                           resources=resources,
                           network_id=network_id,
                           lang_id=lang_id,
                           type_id=type_id,
                           tags_id=tags_id,
                           network=network,
                           lang=lang,
                           type=type,
                           tag=tag)
Ejemplo n.º 18
0
def show_resource(network_id, lang_id, type_id, tags_id, resource_id):
    rep = DI.get_repository()
    network = rep.get_network_name_by_id(network_id)
    lang = rep.get_lang_name_by_id(lang_id)
    type = rep.get_type_name_by_id(type_id)
    tag = rep.get_tag_name_by_id(tags_id)
    tags = rep.read_tags(network_id, lang_id, type_id)
    resource = rep.read_resource(resource_id)
    descriptions = rep.read_descriptions(resource_id)
    return render_template('list/resource.html',
                           title='Main',
                           nav='tags',
                           network_id=network_id,
                           lang_id=lang_id,
                           type_id=type_id,
                           tags_id=tags_id,
                           resource_id=resource_id,
                           network=network,
                           lang=lang,
                           type=type,
                           tag=tag,
                           tags=tags,
                           resource=resource,
                           descriptions=descriptions)
Ejemplo n.º 19
0
def page_pub(param_sub, param_name):
    cnt = DI.create_container()
    local_storage = DI.create_local_storage()
    config = DI.get_config()

    if 'container' not in config or config['container'] == '':
        print(
            "You have no selected container, type 'cc.py Ccontainer_name' to select container"
        )
        return False

    if 'user' not in config or config['user'] == '' or config[
            'user'] not in cnt.list_users():
        print(
            "You have no selected user, type 'cc.py Uusername' to select user")
        return False

    cnt = DI.create_container()

    if not cnt.container_exists(config['container']):
        print("Container {} does not exist".format(config['container']))
        return False

    pub_messages = local_storage.list_pub_messages(config['container'])

    if param_sub in ('+', '='):
        if 'editor' not in config or config['editor'] == '':
            print("Editor not defined, type 'cc.py Feditor=my_editor'")
            return

        if len(pub_messages) and config['user'] in pub_messages:
            # print(pub_messages[config['user']])
            os.system(config['editor'] + ' ' +
                      pub_messages[config['user']]['filename'])
        else:
            filename = cnt.generate_pub(config['container'], config['user'],
                                        "Type your message here ...")
            os.system(config['editor'] + ' ' + filename)
    elif param_sub == '-':
        if len(pub_messages) and config['user'] in pub_messages:
            if local_storage.delete_pub_message(config['container'],
                                                config['user']):
                print('Unsaved public message deleted')
    elif param_sub == '*':
        path = cnt.get_container_path(config['container'], config['user'])
        if param_name == '*':
            os.system(config['editor'] + ' ' + path['path_ipub'])
        else:
            os.system(config['editor'] + ' ' + path['path_pub'])
    else:
        msg = local_storage.list_pub_messages_user(config['container'],
                                                   config['user'])
        if msg:
            msg = msg[config['user']]
            t = datetime.fromtimestamp(int(msg['created']))
            created = t.strftime("%Y-%m-%d %H:%M")
            print("You have unsaved message at {} in {}".format(
                created, msg['filename']))
            print(
                "Type 'cc.py P=' to edit message, 'cc.py P-' to delete message and 'cc.py C@' to save current container"
            )
        else:
            print(
                'You have no unsaved messages, type \'cc.py P=\' to create new message'
            )
    return
Ejemplo n.º 20
0
def page_msg(param_sub, param_name):
    local_storage = DI.create_local_storage()
    config = DI.get_config()
    cnt = DI.create_container()

    if not cnt.container_exists(config['container']):
        print("Container {} does not exist".format(config['container']))
        return False

    if 'user' not in config or config['user'] == '' or config[
            'user'] not in cnt.list_users():
        print(
            "You have no selected user, type 'cc.py Uusername' to select user")
        return False

    from_username = config['user']

    priv_messages = local_storage.list_priv_messages(config['container'])
    priv_messages_user = []
    for filename in priv_messages:
        if priv_messages[filename]['from'] == from_username:
            priv_messages_user.append(priv_messages[filename])

    userlist = sorted(cnt.list_container_users(config['container']))

    if param_sub == '+':
        if 'editor' not in config or config['editor'] == '':
            print("Editor not defined, type 'cc.py Feditor=my_editor'")
            return

        params = param_name.split('+')
        username = ''
        subject = ''
        reply = ''
        if len(params) > 0:
            username = params[0]
        if len(params) > 1:
            subject = params[1]
        if len(params) > 2:
            i = 2

            while len(params) > i:
                if reply != '':
                    reply += '+'
                reply += params[i]
                i += 1

        if reply[0:1] == '%':
            reply = reply.replace('+', ' ')

        if username == '':
            print("Select user you want to write to:")
            i = 0
            for i, usr in enumerate(userlist):
                print("{}: {}".format(i, usr))
            ask = input()[0].lower()

            if ask.isnumeric() and int(ask) < len(userlist):
                username = userlist[int(ask)]
            else:
                return False
        else:
            if username not in userlist:
                print("Error: {} not found in container users list ".format(
                    username))
                return False

        if subject == '':
            subject = input('Type message subject:')

        filename = cnt.generate_msg(config['container'], config['user'],
                                    username, "Type your message here ...", {
                                        'subject': subject,
                                        'reply': reply
                                    })

        os.system(config['editor'] + ' "' + filename + '"')
        return
    elif param_sub == '=':
        if 'editor' not in config or config['editor'] == '':
            print("Editor not defined, type 'cc.py Feditor=my_editor'")
            return

        if len(priv_messages_user) > 0:
            print('Select message to edit:')
            i = 0
            for i, msg in enumerate(priv_messages_user):
                print(i, ': ', os.path.basename(msg['filename']))

            ask = input()[0].lower()
            if ask.isnumeric() and int(ask) < len(priv_messages_user):
                os.system(config['editor'] + ' "' +
                          priv_messages_user[int(ask)]['filename'] + '"')

        else:
            print("You have no unsaved messages")
        return
    elif param_sub == '-':
        if len(priv_messages_user) > 0:
            print('Select message to delete:')
            i = 0
            for i, msg in enumerate(priv_messages_user):
                print(i, ': ', os.path.basename(msg['filename']))

            ask = input()[0].lower()
            if ask.isnumeric() and int(ask) < len(priv_messages_user):
                os.unlink(priv_messages_user[int(ask)]['filename'])
        else:
            print("You have no unsaved messages")
        return
    elif param_sub == '*':
        path = cnt.get_container_path(config['container'], config['user'])
        if param_name == '*':
            os.system(config['editor'] + ' ' + path['path_vbox'])
        else:
            os.system(config['editor'] + ' ' + path['path_inbox'])
    else:
        if len(priv_messages_user) > 0:
            print("You have {} unsaved messages:".format(
                len(priv_messages_user)))
            i = 0
            for msg in priv_messages_user:
                t = datetime.fromtimestamp(int(msg['created']))
                print(t.strftime("%Y-%m-%d %H:%M:%S"),
                      os.path.basename(msg['filename']))
        else:
            print("You have no unsaved messages")
        return

    return
Ejemplo n.º 21
0
def page_status():
    cnt = DI.create_container()
    local_storage = DI.create_local_storage()
    config = DI.get_config()

    users = cnt.list_users()
    uu = []
    for user in users:
        if config['user'] == user:
            user = '******' + user + '>'
        uu.append(user)

    if len(uu) == 0:
        print("You have no users")
        print("type 'cc.py U+username' to generate user")
    else:
        uu = sorted(uu)
        print("My users: {}".format(", ".join(uu)))

    containers = cnt.list_containers()
    if len(containers):
        cc = []
        for container in containers:
            if config['container'] == container:
                container = '<' + container + '>'
            cc.append(container)

        uu = sorted(cc)
        print("Containers: {}".format(", ".join(uu)))

        if 'container' in config and config['container'] != '':
            container = cnt.get_container(config['container'])

            print("Container '{}': ".format(config['container']))
            users = cnt.list_container_users(config['container'])
            uu = []
            for user in users:
                if config['user'] == user:
                    user = '******' + user + '>'
                uu.append(user)

            uu = sorted(uu)
            if len(uu) > 0:
                print("    Container users: {}".format(", ".join(uu)))
            else:
                print("    Container has no users")

            mcount = len(local_storage.list_priv_messages(config['container']))
            if mcount > 0:
                print("    Private message count (unsaved): {}".format(mcount))
            else:
                print("    NO unsaved private messages")

            mcount = len(local_storage.list_pub_messages(config['container']))
            if mcount > 0:
                print("    Public message count (unsaved): {}".format(mcount))
            else:
                print("    NO unsaved public messages")
    else:
        print("You have no containers")
    return
Ejemplo n.º 22
0
def sync():
    rep = DI.get_repository()
    blk = DI.get_blockchain()
    upd = Updater(rep, blk)
    upd.sync()
    return 'ok'
Ejemplo n.º 23
0
def page_container(param_sub, container_name):
    local_storage = DI.create_local_storage()
    config = DI.get_config()
    cnt = DI.create_container()

    if param_sub == '+':
        if cnt.generate_container(container_name):
            print("Container {} generated OK".format(container_name))
        else:
            print('Error')
    elif param_sub == '-':
        if container_name == '':
            print("No container selected")
            return False

        if cnt.container_exists(container_name):
            ask = input(
                "Delete container {}, you will not be able to restore it (y/n) :"
                .format(container_name))
            if ask.lower() == 'y':
                if cnt.delete_container(container_name):
                    print("Container {} deleted OK".format(container_name))
        else:
            print("Container {} does not exist".format(container_name))
        return
    elif param_sub == '@':
        if container_name == '':
            container_name = config['container']
        if container_name == '':
            print("No container selected")
            return False

        if 'user' not in config or config['user'] == '' or config[
                'user'] not in cnt.list_users():
            print(
                "You have no selected user, type 'cc.py Uusername' to select user"
            )
            return False

        if 'upload' in config and config['upload'] == '':
            print(
                "Upload path not defined, type 'cc.py Fupload=my_upload_path'")

        if cnt.container_exists(container_name):
            if cnt.save(container_name):
                cnt.decrypt(container_name)
                print("Container {} saved OK".format(container_name))
        else:
            print("Container {} does not exist".format(container_name))
        return
    elif param_sub == '?':
        if cnt.container_exists(container_name):
            container = cnt.get_container(container_name)
            print("Container '{}': ".format(container_name))
            print("Synchronized: {} ".format(
                container['container']['synchronized']))
            users = cnt.list_container_users(container_name)
            uu = []
            for user in users:
                if config['user'] == user:
                    user = '******' + user + '>'
                uu.append(user)

            uu = sorted(uu)
            print("    Container users: {}".format(", ".join(uu)))

            mcount = len(local_storage.list_priv_messages(container_name))
            if mcount > 0:
                print("    Private message count (unsaved): {}".format(mcount))
            else:
                print("    NO unsaved private messages")

            mcount = len(local_storage.list_pub_messages(container_name))
            if mcount > 0:
                print("    Public message count (unsaved): {}".format(mcount))
            else:
                print("    NO unsaved public messages")
        else:
            print("Container {} does not exist".format(container_name))
    elif param_sub == '^':
        if 'remote' in config and config['remote'] != '':
            cnt.sync()
        else:
            print(
                "Remote path not defined, type 'cc.py Fremote=my_remote_path'")
        return
    elif param_sub == '#':
        if container_name == '':
            container_name = config['container']
        if container_name == '':
            print("No container selected")

        if cnt.container_exists(container_name):
            if cnt.container_upload(container_name):
                print("Container {} uploaded to {} OK".format(
                    container_name, config['upload']))
            else:
                print("Error: uploading container {} to {}".format(
                    container_name, config['upload']))
        else:
            print(
                "Error: container '{}' does not exist".format(container_name))
        return
    elif param_sub != '':
        container_name = param_sub + container_name

        containers = cnt.list_containers()

        if len(containers) == 0:
            print('There are no containers')
            print('Type \'cc.py C+container_name\' to generate container')
            return False

        if container_name in containers:
            config['container'] = container_name
            local_storage.save_config(config)
            print("Container {} selected OK".format(container_name))
        else:
            print("Container {} does not exists".format(container_name))
    else:
        containers = cnt.list_containers()

        if len(containers) == 0:
            print('There are no containers')
            print('Type \'cc.py C+container_name\' to generate container')
            return False

        cc = []
        for container in containers:
            if container == config['container']:
                container = '<' + config['container'] + '>'
            cc.append(container)

        cc = sorted(cc)
        print("Containers: {}".format(", ".join(cc)))
    return