• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

OCHA-DAP / hdx-ckan / #6761

23 Sep 2025 01:49PM UTC coverage: 78.293% (+3.4%) from 74.853%
#6761

Pull #6752

coveralls-python

web-flow
Merge pull request #6751 from OCHA-DAP/feature/HDX-11115-coverage-increase-hdx-package

HDX-11115 - hdx_package
Pull Request #6752: Feature/hdx 11115 coverage increase

68 of 83 new or added lines in 12 files covered. (81.93%)

5 existing lines in 4 files now uncovered.

13464 of 17197 relevant lines covered (78.29%)

0.78 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

87.36
/ckanext-hdx_theme/ckanext/hdx_theme/views/quick_links_custom_settings.py
1
import flask
1✔
2
import json
1✔
3
import logging
1✔
4
import uuid
1✔
5

6
from six import text_type
1✔
7

8
import ckan.plugins.toolkit as tk
1✔
9
import ckan.model as model
1✔
10

11
from ckan.types import Context
1✔
12
from ckanext.hdx_theme.helpers.uploader import GlobalUpload
1✔
13

14
abort = tk.abort
1✔
15
_ = tk._
1✔
16
g = tk.g
1✔
17
h = tk.h
1✔
18
request = tk.request
1✔
19
check_access = tk.check_access
1✔
20
get_action = tk.get_action
1✔
21
render = tk.render
1✔
22

23
log = logging.getLogger(__name__)
1✔
24

25
hdx_quick_links = flask.Blueprint(u'hdx_quick_links', __name__, url_prefix=u'/ckan-admin/quick-links')
1✔
26

27

28
def show():
1✔
29
    context = {u'user': g.user}
1✔
30
    try:
1✔
31
        check_access('hdx_quick_links_update', context, {})
1✔
32
    except tk.NotAuthorized:
1✔
33
        return abort(403, _('Not authorized to access this page'))
1✔
34

NEW
35
    action_context: Context = {'model': model, 'session': model.Session, 'user': g.user}
×
NEW
36
    setting_value = get_action('hdx_quick_links_settings_show')(action_context, {})
×
UNCOV
37
    template_data = {
×
38
        'data': {
39
            'hdx.quick_links.config': json.dumps(setting_value)
40
        }
41
    }
42

43
    return render('admin/quick_links.html', extra_vars=template_data)
×
44

45

46
def delete(id):
1✔
47
    context = {u'user': g.user}
1✔
48
    try:
1✔
49
        check_access('hdx_quick_links_update', context, {})
1✔
50
    except tk.NotAuthorized:
1✔
51
        return abort(403, _('Not authorized to access this page'))
1✔
52

53
    action_context: Context = {'model': model, 'session': model.Session, 'user': g.user}
1✔
54
    existing_setting_list = get_action('hdx_quick_links_settings_show')(action_context, {'not_initial': True})
1✔
55
    remove_index, remove_element = _find_quick_links_item_by_id(existing_setting_list, id)
1✔
56

57
    if remove_index >= 0:
1✔
58
        del existing_setting_list[remove_index]
1✔
59

60
    data_dict = {
1✔
61
        'hdx.quick_links.config': existing_setting_list
62
    }
63

64
    settings_json = get_action('hdx_quick_links_settings_update')(action_context, data_dict)
1✔
65

66
    return settings_json
1✔
67

68

69
def update():
1✔
70
    context = {u'user': g.user}
1✔
71
    try:
1✔
72
        check_access('hdx_quick_links_update', context, {})
1✔
73
    except tk.NotAuthorized:
1✔
74
        return abort(403, _('Not authorized to access this page'))
1✔
75

76
    item = _process_request()
1✔
77

78
    if item:
1✔
79
        action_context: Context = {'model': model, 'session': model.Session, 'user': g.user}
1✔
80
        existing_setting_list = get_action('hdx_quick_links_settings_show')(action_context, {'not_initial': True})
1✔
81

82
        if item.pop('new'):
1✔
83
            existing_setting_list.append(item)
1✔
84
        else:
85
            existing_index, existing_element = _find_quick_links_item_by_id(existing_setting_list, item.get('id'))
×
86
            existing_setting_list[existing_index] = item
×
87

88
        data_dict = {
1✔
89
            'hdx.quick_links.config': _sort_quick_links_items(existing_setting_list)
90
        }
91

92
        ret = get_action('hdx_quick_links_settings_update')(action_context, data_dict)
1✔
93
    else:
94
        ret = json.dumps({
×
95
            'message': _('Badly formatted data')
96
        })
97

98
    return ret
1✔
99

100

101
def _sort_quick_links_items(quick_links_items):
1✔
102
    return sorted(quick_links_items, key=lambda x: x.get('order'))
1✔
103

104

105
def _find_quick_links_item_by_id(quick_links_items, id):
1✔
106
    index = -1
1✔
107
    element = None
1✔
108
    for i, item in enumerate(quick_links_items):
1✔
109
        if item.get('id') == id:
1✔
110
            index = i
1✔
111
            element = item
1✔
112
            break
1✔
113
    return index, element
1✔
114

115

116
def _remove_file_by_path(path):
1✔
117
    '''
118
    :param path: something like /global/[uuid].png
119
    '''
120
    if path:
×
121
        existing_upload = GlobalUpload({
×
122
            'filename': path,
123
            'upload': None
124
        })
125
        existing_upload.delete()
×
126

127

128
def _process_request():
1✔
129
    '''
130
    :return: processes the request and returns a quick_links item
131
    :rtype: dict
132
    '''
133

134
    title = request.form.get('title')
1✔
135
    if not title:
1✔
136
        return None
×
137
    else:
138
        item = {
1✔
139
            'title': title,
140
            # 'description': request.form.get('description'),
141
            # 'graphic': request.form.get('graphic'),
142
            # 'graphic_upload': request.form.get('graphic_upload'),
143
            'url': request.form.get('url'),
144
            'order': int(request.form.get('order', -1)),
145
            'newTab': True if request.form.get('newTab') == 'true' else False,
146
            'archived': True if request.form.get('archived') == 'true' else False,
147
            # 'embed': True if request.form.get('embed') == 'true' else False,
148
            'new': False if request.form.get('id') else True,
149
            'id': request.form.get('id') if request.form.get('id') else text_type(uuid.uuid4())
150
        }
151
        if request.form.get('buttonText'):
1✔
152
            item['buttonText'] = request.form.get('buttonText')
1✔
153

154
    return item
1✔
155

156

157
hdx_quick_links.add_url_rule(u'/show', view_func=show)
1✔
158
hdx_quick_links.add_url_rule(u'/update', view_func=update, methods=[u'POST'])
1✔
159
hdx_quick_links.add_url_rule(u'/delete/<id>', view_func=delete, methods=[u'POST'])
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc