def list_notifications(request, page=1):
    # Limit results to last 60 days
    start_date = datetime.datetime.now() - datetime.timedelta(days=60)
    notes = models.Notification.objects.filter(created__gte=start_date).order_by('-created')
    response = template_render('notification_list.html', {'notes': notes})

    return response
def chown_view(request, doctype=None):
    msg = ""
    doctypes = [(doc["description"], doc["meta"]["document_type"])
                for doc in BASEDOCS.values()]

    groups = Group.objects.all().order_by("name")
    if doctype:
        docs = get_docs(doctype, sort=True)
    else:
        docs = None
    context = {
        'docs'     : docs,
        'doctype'  : doctype,
        "doctypes" : doctypes,
        "groups"   : groups
        }

    if request.POST:
        groupname = request.POST.get("group-to")
        update_ids = request.POST.getlist("chown")
        msg = chown_docs(update_ids, groupname, doctype, docs, settings.INDEX_NAME)
        context["msg"] = msg
        context["updated"] = update_ids
    t = env.get_template('chown_list.html')
    s = template_render(t, context)
    return HttpResponse(s)
def document_template_listing(request):
    if not request.user.is_authenticated():
        return redirect('kir.views.index')

    t = env.get_template('document_templates.html')

    services    = searcher.front_page_search(request.user, 'service')[0]
    servicetags = searcher.front_page_search(request.user, 'servicetag')[0]
    periods     = searcher.front_page_search(request.user, 'period')[0]
    persons     = searcher.front_page_search(request.user, 'person', sort='last_name,first_name')[0]

    context = {
        'services': services,
        'service_count': len(services),
        'servicetags': servicetags,
        'servicetag_count': len(servicetags),
        'periods': periods,
        'period_count': len(periods),
        'persons': persons,
        'person_count': len(persons),
        'limit': 5,
        'teaser': teaser
        }

    s = template_render(t, context)
    return HttpResponse(s)
def updateform_view(request, dtype):
    eid = request.POST.get('doc_id')
    data, doc, baseform, template = get_basics(request, doctype)
    append_key = check_append(data)

    if append_key:
        context = add_compoundfield_doc(doc, baseform, data, append_key, eid)
    else:
        context = update_doc(doc, baseform, data)
    if not context:
        raise Http404

    context.update({
        'type' : dtype,
        'doc' : json.dumps(doc),
        'doc_id': eid,
        "description" : doc.get("description").capitalize(),
        'languages' : supported_languages(),
        'tab_ids'     : prepare_tabs(context['form']),
        'skip_js': True,
        'request': request
    })
    s = template_render(template, context)

    return HttpResponse(s)
Example #5
0
def prepare(**env):
    """
    Prepare a deployment. Loads initial environment config and stores initial infrastructure files
    """
    #
    # Check deployment does not exist
    #
    name = env["name"]

    if utils.deployment_exists(name):
        res = tasks.failure(f"Deployment {name} already exists")
        logging.critical(tasks.get_stderr(res))
        return res

    path = utils.path_deployment(name)

    #
    # Print environment
    #
    logging.info(f"[X] Environment:\n{json.dumps(env, indent = 4)}\n")

    #
    # Copy infrastructure files
    #
    logging.info("[X] Copying infrastructure files...")

    # terraform files
    shutil.copytree(utils.path_infrastructure(env["provider"]), path)

    logging.info(
        f"Copied tree [{utils.path_infrastructure(env['provider'])}] -> [{path}]"
    )

    # render terraform input variables from environment
    utils.template_render(utils.path_templates(env["provider"]),
                          "terraform.tfvars.j2", path, **env)

    logging.info("Rendered terraform.tfvars")
    with open(f"{path}/terraform.tfvars", "r") as f:
        logging.debug(f"{f.read()}")

    # save the environment
    utils.environment_save(name, **env)

    logging.info("OK\n")

    return tasks.success()
Example #6
0
def admin_index(request, u):
    # 认证石否为管理员
    if u.role != 1:
        log('admin not power', u.role)
        return redirect('/login')
    log('admin', u)
    users = User.all()
    return http_response(template_render('admin_index.html', users=users))
def notice_view(request):
    template = env.get_template('notice.html')
    message = fetch_message(request)
    if message == None:
        return redirect('/')
    return HttpResponse(
        template_render(
            template, {
                'message': message}))
def add_linkdata_view(request, doctype, exdoctype, linkfield):
    """
    Function adds given given exdoc_id's to given target field and
    sets given linkfield with the modeldocs eid

    TODO: reliable way to find where to put the eid of linked doc in form
          reasonable way to loop the model docs with their id's

    building the form is ever so slightly different when one wants to
    update it with some values and replace with others (linkfield and modeldoc)
    """
    #append_data = conn.get(settings.INDEX_NAME, doctype, exdoc_id)
    exdoc_ids = request.GET.getlist('exdoc_id')
    if not exdoc_ids:
        raise Http404
    append_key = request.GET.get('target')
    if not append_key:
        raise Http404
    template = None
    if doctype == 'organisation':
        template = env.get_template('organisation_form.html')

    eid = request.POST.get('doc_id')
    data, doc, baseform, template = get_basics(request, doctype, template=template)
    example_data = searcher.get_docs(exdoctype, exdoc_ids)
    #if is_compoundfield(doc, append_key):
    #    append_data = example_data
    #else:
    #    append_data = create_exampledoc(doc, append_key, example_data[0])
    #TODO: plz fix.. doclist and eid list are in same order, but not pretty
    for eid_num, modeldoc in enumerate(example_data):
        #TODO: plz fix.. linkfield might not be at first level of subdoc
        modeldoc[linkfield] = exdoc_ids[eid_num]
    context = add_linkdata_doc(doc, baseform, data, append_key, example_data)
    if not context:
        raise Http404

    path = request.path_info + '?doc_id={0}'.format(eid)

    context.update({
        'type' : doctype,
        'doc' : json.dumps(doc),
        'user': request.user,
        'doc_id' : eid,
        'title': data.get('name_fi'),
        "description" : doc.get("description").capitalize(),
        'reload_path': '/add/{dt}/?doc_id={eid}'.format(dt=doctype, eid=eid),
        'languages' : supported_languages(),
        'tab_ids'     : prepare_tabs(context['form']),
        'skip_js': True,
        'request': request
    })
    s = template_render(template, context)
    sr = re.sub(r'\s+', ' ', s)
    return HttpResponse(sr)
def edit_view(request, doctype):
    docs = get_docs(doctype)
    doc, baseform = get_base(doctype)
    sort_key = doc['meta']['sort']
    t = env.get_template('edit.html')

    for document in sorted_docs:
        document.summary = doc_presentation(document)
    s = template_render(t, {'docs' : docs, 'doctype' : doctype,
                  'description' : doc.get('description')})
    return HttpResponse(s)
def read_notification(request, note_id):
    note = models.Notification.objects.get(id=note_id)

    try:
        # Mark this note as read by the user
        models.ReadNotifications.objects.create(user=request.user, notification=note).save()
        pass
    except IntegrityError:
        pass

    response = template_render('notification.html', {'note': note})

    return response
Example #11
0
def index(request, u):
    """
    todo 首页的路由函数
    """

    # 找到当前登录的用户, 如果没登录, 就 redirect 到 /login
    # uname = current_user(request)
    # u = User.find_by(username=uname)
    # if u is None:
    #     return redirect('/login')
    todos = Todo.find_all(user_id=u.id)
    # 下面这行生成一个 html 字符串
    # todo_html = ''.join(['<h3>{} : {} </h3>'.format(t.id, t.title)
    #                      for t in todo_list])
    # 上面一行列表推倒的代码相当于下面几行
    return http_response(template_render('todo_index.html', todos=todos))
def delete_view(request, doctype, eid):
    """
    Deletes document from index with given doctype and elastic id.
    Copies document to version index with meta.deleted being the deleted doc's
    id.

    Thus one can search for deleted docs and its versions
    (since meta.deleted and versioned docs meta.original are set same id).
    """
    doc = get_doc_from_index(settings.INDEX_NAME, doctype, eid)
    if not doc:
        raise Http404
    add_metadata(doc["_source"], request.user)
    doc["_source"]["meta"]["deleted"] = eid
    conn.index(doc["_source"], settings.VERSION_INDEX_NAME, doctype)
    conn.delete(settings.INDEX_NAME, doctype, eid)
    t = env.get_template('delete.html')
    s = template_render(t, {"msg" : _("The document with the id %s was removed.") %
                  (eid, ),
                  "doctype" : doctype})
    return HttpResponse(s)
def add_exampledata_view(request, doctype, exdoctype, append_key):
    #append_data = conn.get(settings.INDEX_NAME, doctype, exdoc_id)
    exdoc_ids = request.GET.getlist('exdoc_id')

    if not exdoc_ids:
        raise Http404

    template = None

    eid = request.POST.get('doc_id')

    if doctype == 'organisation':
        template = env.get_template('organisation_form.html')
    data, doc, baseform, template = get_basics(request, doctype, template=template)
    example_data = searcher.get_docs(exdoctype, exdoc_ids)
    if is_compoundfield(doc, append_key):
        append_data = example_data
    else:
        append_data = create_exampledoc(doc, append_key, example_data[0])
    context = add_fielddata_doc(doc, baseform, data, append_key, append_data)
    if not context:
        raise Http404

    path = request.path_info + '?doc_id={0}'.format(eid)

    context.update({
        'type' : doctype,
        'doc' : json.dumps(doc),
        'user': request.user,
        'doc_id' : eid,
        'title': data.get('name_fi'),
        "description" : doc.get("description").capitalize(),
        'reload_path': '/add/{dt}/?doc_id={eid}'.format(dt=doctype,
                                                                    eid=eid),
        'languages' : supported_languages(),
        'tab_ids'     : prepare_tabs(context['form']),
        'skip_js': True,
        'request': request})
    s = template_render(template, context)
    return HttpResponse(s)
def list_examples_view(request, doctype, docs=None, context={}):
    criteria = {}
    consortium = request.GET.get('consortium')

    if consortium:
        criteria['consortium'] = consortium
    if docs is None:
        docs = searcher.example_list(
            request.user, doctype=doctype, criteria=criteria)

    doc, baseform = get_base(doctype)
    t = env.get_template('example_list.html')

    grouped_docs = None

    if doctype == 'service':
        # todo: more generic grouping/sorting
        # using elastic
        grouped_docs = {}
        sorted_docs = docs[0]

        for docz in docs[0]:
            service_type = docz.get('type', 'notype')
            grouped_docs[service_type] = grouped_docs.get(service_type, [])
            grouped_docs[service_type].append(docz)

        for stype, doc_list in grouped_docs.iteritems():
            grouped_docs[stype] = sorted(doc_list, key = lambda x: x.get(
                    'name_fi', '0'))

    context.update({
            'docs' : docs[0],
            'grouped_docs': grouped_docs,
            'doctype' : doctype,
            'description' : doc['description'],
            'presentation_fields' : presentation_fields(doc),
            'teaser': teaser
            })
    s = template_render(t, context)
    return HttpResponse(s)
def restore_view(request, doctype, eid):
    """
    Restore document from versioning index
    """
    basedoc = get_basedoc(doctype)
    reid = request.GET.get("reid") or request.POST.get("reid")
    if not reid:
        versions = es_tools.get_versions(doctype, eid)
        if not versions:
            raise Http404
        resdoc = None
    else:
        versions = None
        res = get_doc_from_index(settings.VERSION_INDEX_NAME, doctype, reid)
        if res:
            resdoc = htmltree_conversion(es_tools.get_source(res), basedoc)
        else:
            raise Http404
    cur = get_doc_from_index(settings.INDEX_NAME, doctype, eid)
    if cur:
        curdoc = es_tools.get_source(cur)
    else:
        raise Http404

    if request.POST.get("restore"):
        es_tools.restore(doctype, curdoc, resdoc, request.user)
        restore = True
    else:
        restore = False
    t = env.get_template('restore.html')
    context = {"restore" : restore,
                  "data" : htmltree_conversion(curdoc, basedoc),
                  "curdoc" : curdoc,
                  "resdoc" : resdoc,
                  "versions" : versions,
                  "doctype" : doctype}
    context.update(csrf(request))
    s = template_render(t, context)
    return HttpResponse(s)
def statistics(request):
    organisations, city_facet = searcher.search({
        "meta": {},
        "searches": [
            { "TermQuery": [["_type", "organisation"]] }
        ],
        "sort": "contact.street_address.municipality_fi,name_fi",
        "facets": ["contact.street_address.municipality_fi"]
    })

    organisations = build_organisation_city_tree(organisations)

    #print(organisations)

    cities = extract_cities_from_facet(city_facet["contact.street_address.municipality_fi"])

    view_data = {
        'organisations': organisations,
        'cities': cities,
        'years': [y for y in range(2013, datetime.date.today().year)]
    }

    return template_render('statistics.html', view_data)
Example #17
0
def edit(request, u):
    """
    todo edit 的路由函数
    """

    # uname = current_user(request)
    # u = User.find_by(username=uname)
    # if u is None:
    #     return redirect('/login')
    # 得到当前编辑的 todo 的 id
    todo_id = request.query.get('id', -1)
    # 检查 id 是不是数字
    if todo_id == -1 or not todo_id.isdigit():
        return redirect('/todo')
    todo_id = int(todo_id)
    todo = Todo.find_by(id=todo_id)
    if todo is None or todo.user_id != u.id:
        return redirect('/todo')
    # if todo_id < 1:
    #     return error(404)
    # 替换模板文件中的标记字符串 header = response_with_headers(headers)

    return http_response(template_render('todo_edit.html', todo=todo))
def add_view(request, dtype, entity_id=None):
    data, doc, baseform, template = get_basics(request, dtype)
    remove_key = check_remove(data)
    append_key = check_append(data)
    eid = entity_id

    if remove_key:
        context = remove_field_doc(doc, baseform, data, remove_key)
        context['doc_id'] = eid
    elif append_key:
        context = add_compoundfield_doc(doc, baseform, data, append_key, eid)
    else:
        if eid and not data:
            context = open_doc(doc, baseform, request.user, eid)
        elif data:
            context = save_doc(doc, baseform, data, request.user, eid)
            eid = context['doc_id']

            if context['saved'] and context['errors'] == None:
                store_context(context, request.session)
                return redirect('kir.views.add_view', dtype, eid)
        else:
            context = new_doc(doc, baseform)

    if not context:
        raise Http404

    context = restore_context(context, request.session)
    context = process_metafields(context)

    if 'doc_id' in context:
        path = request.path_info
    else:
        path = None

    context.update({
        'type'        : dtype,
        'doc'         : json.dumps(doc),
        'request'     : request,
        'time_separator' : time_separator,
        'image_url'   : settings.MEDIA_URL + settings.UPLOADED_IMAGES_FOLDER + 'medium/',
        'description' : doc.get('description'),
        'languages'   : supported_languages(),
        'tab_ids'     : prepare_tabs(context['form']),
        'title'       : doc_presentation(context['form'].data,
                                         basedoc = doc),
        'reload_path' : path,
        'user': request.user,
    })

    if context['type'] != 'organisation':
        if type(context['title']) == str:
            context['title'] = context['title'].decode('utf-8')

    if dtype == 'period': # todo: generalize as hook?
        context.update(PeriodWidget.template_params(form = context['form']))

    #print(context)

    context['parents'] = parent_chain(doc, context['form'].data, request.user)
    response = HttpResponse(template_render(template, context))
    response['Cache-Control'] = 'no-store'

    print(context.keys())

    return response
def documents_view(request):
    doctypes = sorted([(doc['meta']['document_type'], doc['description'])
                       for doc in BASEDOCS.values()])
    t = env.get_template('documents.html')
    s = template_render(t, {'doctypes' : doctypes})
    return HttpResponse(s)
def index(request, entity_id=None):
    current_step = 1
    docs = []
    only_one = False

    if not request.user.is_authenticated():
        login_required = True
    else:
        login_required = False
        current_step = 2

    doc_id = entity_id
    selected_id = None
    selected_library = None

    if doc_id:
        selected_doc = get_doc_from_index(
                settings.INDEX_NAME, 'organisation', doc_id)
        if selected_doc:
            selected_library = get_source(selected_doc)
            selected_id=doc_id
            current_step = 3

            if selected_library['organisation_type'] != 'library':
                family_tree = searcher.search_family_tree(user=request.user)
                member = family_tree.get(doc_id)
                if member:
                    selected_library['children'] = [
                        (cid, cname) for cid, cname in
                        zip(member['children'], member['children_names'])]

                    parents = member['parent_names']
                    parents.reverse()
                    selected_library['parents'] = ' &rarr; '.join(parents)
    else:
        if not login_required:
            request.session.set_expiry(12*60*60)
            current_step = 2

        required_fields = ['organisation_type',
                           'contact.street_address.municipality_fi',
                           'name_fi',
                           'parent_organisation',
                           'meta.modified']

        if request.user.is_authenticated():
            if request.user.is_superuser:
                results = searcher.search(
                    # todo: remove size
                    searcher.ALL_LIBS, size = 10000, fields = required_fields)
            else:
                results = searcher.front_page_search(
                    request.user, size = 500, fields = required_fields)

            family_tree = searcher.search_family_tree(user=request.user)

            for doc in results[0]:
                if 'organisation_type' not in doc:
                    print(doc)

                if (doc['organisation_type'] in
                    ['branchlibrary', 'unit', 'library']):
                    family_member = family_tree.get(doc['_id'])

                    if family_member:
                        parents = family_member['parent_names']
                        children = family_member['children_names']
                        children_ids = family_member['children']

                        if len(parents):
                            parents.reverse()
                            doc['parents'] = ' &rarr; '.join(parents)

                        if len(children):
                            doc['children'] = [(cid, cname) for cid, cname in
                                               zip(children_ids, children)]

                if (doc['organisation_type'] not in ['department',
                                                     'mobile_stop']):
                    docs.append(doc)


            if not login_required and len(docs) == 1:
                current_step = 3
                selected_library = docs[0]
                selected_id = selected_library['_id']
                only_one = True

    t = env.get_template('index.html')

    note_filter_date = dt.datetime.now() - dt.timedelta(days=7)
    notifications = models.Notification.objects.unread(request.user, note_filter_date)

    context = {
        'login_required'      : login_required,
        'docs'                : docs,
        'library_count'       : len(docs),
        'username'            : request.user.username,
        'access_to_templates' : (
            not login_required and
            request.user.has_perm('sites.access_to_templates')),

        'summary'             : teaser('organisation'),
        'selected_library'    : selected_library,
        'selected_id'         : selected_id ,
        'frontpage_url'       : frontpage_url,
        'editpage_url'        : editpage_url,
        'current_step'        : current_step,
        'only_one'            : only_one,
        'library_limit'       : 5,
        'username_string'     : _("Username"),
        'password_string'     : _("Password"),
        'login_string'        : _("Log in"),
        'notifications'       : notifications,
        'can_export'          : user_has_group(request.user, 'export')
        }

    context.update(csrf(request)) # todo: better way?

    s = template_render(t, context)

    return HttpResponse(s)
Example #21
0
def infrastructure(name):
    """
    Create infrastructure for a deployment.
    """
    #
    # Check deployment does exist
    #
    res, path, env = utils.deployment_verify(name)
    if tasks.has_failed(res):
        logging.critical(tasks.get_stderr(res))
        return res

    #
    # Create infrastructure
    #
    logging.info("[X] Creating infrastructure...")

    # init
    logging.info("Initializing Terraform")
    res = terraform.init(path)
    if tasks.has_failed(res):
        logging.critical(tasks.get_stderr(res))
        return res
    else:
        logging.debug(tasks.get_stdout(res))

    # switch to workspace
    logging.info(f"Switching to workspace {env['name']}")
    res = terraform.workspace(path, env["name"])
    if tasks.has_failed(res):
        logging.error(tasks.get_stderr(res))
        return res
    else:
        logging.debug(tasks.get_stdout(res))

    # apply
    logging.info(f"Executing plan")
    res = terraform.apply(path)
    if tasks.has_failed(res):
        logging.critical(tasks.get_stderr(res))
        return res
    else:
        logging.debug(tasks.get_stdout(res))

    logging.info("OK\n")

    #
    # Get terraform outputs
    #
    logging.info("[X] Adding terraform outputs to environment...")

    # capture output
    logging.info(f"Capturing output")
    res = terraform.output(path)
    if tasks.has_failed(res):
        logging.critical(tasks.get_stderr(res))
        return res
    else:
        logging.debug(tasks.get_stdout(res))

    # load as json
    terraform_json = json.loads(tasks.get_stdout(res))

    # translate "a_b = v" outputs to env[terraform][a][b] = v
    logging.info(f"Translating output")
    for _, (k, v) in enumerate(terraform_json.items()):
        if v["value"]:
            key, _, subkey = k.partition("_")
            env["terraform"][key][subkey] = v["value"]

    # save enriched enviroment data
    utils.environment_save(name, **env)

    logging.info(f"Updated environment")
    logging.debug(f"{json.dumps(env, indent = 4)}\n")

    logging.info("OK\n")

    #
    # Copy provision files
    #
    logging.info("[X] Copying provision files...")

    # render grains files for nodes using enviroment
    for index in range(0, int(env["terraform"]["node"]["count"])):
        utils.template_render(utils.path_templates(env["provider"]),
                              "node.grains.j2",
                              path,
                              index=index,
                              **env)

        res = tasks.run(
            f"cd {path} && mv node.grains node-0{index + 1}.grains")
        if tasks.has_failed(res):
            logging.critical(tasks.get_stderr(res))
            return res

        logging.info(f"Rendered node-0{index + 1}.grains")
        with open(f"{path}/node-0{index + 1}.grains", "r") as f:
            logging.debug(f"{f.read()}")

    # if there is a iscsi device, render grains file for iscsi using enviroment
    if "public_ip" in env["terraform"]["iscsi"]:
        utils.template_render(utils.path_templates(env["provider"]),
                              "iscsi.grains.j2", path, **env)

        logging.info("Rendered iscsi.grains")
        with open(f"{path}/iscsi.grains", "r") as f:
            logging.debug(f"{f.read()}")

    # if there is a monitor device, render grains file for monitor using enviroment
    if "public_ip" in env["terraform"]["monitor"]:
        utils.template_render(utils.path_templates(env["provider"]),
                              "monitor.grains.j2", path, **env)

        logging.info("Rendered monitor.grains")
        with open(f"{path}/monitor.grains", "r") as f:
            logging.debug(f"{f.read()}")

    logging.info("OK\n")

    return tasks.success()
def export_view(request):
    return template_render('export.html', {})