admin.py 9.68 KB
Newer Older
1
from adminsortable2.admin import SortableAdminMixin
2
from celery import group
3
from django.contrib import admin
4
from django.urls import reverse
5
from django.utils.html import format_html
6
from import_export.admin import ImportExportModelAdmin
7

8
from ..app.models import Job
9
from ..celery import PRIO_HIGH, app
10
from .geojson import import_from_scratch, update_coordinates
11 12
from .models import (AdministrativeRegion, Configuration, MapDataCache, OrganizationRating,
                     UrlRating, VulnerabilityStatistic)
13

14

15
@admin.register(OrganizationRating)
16
class OrganizationRatingAdmin(ImportExportModelAdmin, admin.ModelAdmin):
17
    def inspect_organization(self, obj):
18 19 20
        return format_html(
            '<a href="../../organizations/organization/{id}/change">inspect organization</a>',
            id=format(obj.organization_id))
21

22
    list_display = ('organization', 'high', 'medium', 'low', 'when', 'inspect_organization')
23
    search_fields = (['organization__name', 'rating', 'high', 'medium', 'low', 'when', 'calculation'])
24
    list_filter = ('organization', 'organization__country', 'organization__type__name', 'rating', 'when')
Elger Jonker's avatar
Elger Jonker committed
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
    fields = ('organization', 'total_urls', 'total_endpoints',
              'high',
              'medium',
              'low',
              'high_urls',
              'medium_urls',
              'low_urls',
              'high_endpoints',
              'medium_endpoints',
              'low_endpoints',
              'total_url_issues',
              'url_issues_high',
              'url_issues_medium',
              'url_issues_low',
              'total_endpoint_issues',
              'endpoint_issues_high',
              'endpoint_issues_medium',
              'endpoint_issues_low',

              'explained_high',
              'explained_medium',
              'explained_low',
              'explained_high_urls',
              'explained_medium_urls',
              'explained_low_urls',
              'explained_high_endpoints',
              'explained_medium_endpoints',
              'explained_low_endpoints',
              'explained_total_url_issues',
              'explained_url_issues_high',
              'explained_url_issues_medium',
              'explained_url_issues_low',
              'explained_total_endpoint_issues',
              'explained_endpoint_issues_high',
              'explained_endpoint_issues_medium',
              'explained_endpoint_issues_low',

              'when',
              'calculation')
64 65 66 67 68 69

    ordering = ["-when"]

    save_as = True


70
@admin.register(UrlRating)
71
class UrlRatingAdmin(ImportExportModelAdmin, admin.ModelAdmin):
72
    def inspect_url(self, obj):
73 74
        return format_html('<a href="../../organizations/url/{id}/change">inspect</a>',
                           id=format(obj.url_id))
75

76
    list_display = ('url', 'high', 'medium', 'low', 'total_endpoints', 'when', 'inspect_url')
77
    search_fields = (['url__organization__name', 'rating', 'high', 'medium', 'low', 'when', 'calculation'])
78
    list_filter = ('url', 'rating', 'when')
Elger Jonker's avatar
Elger Jonker committed
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
    fields = ('url', 'total_endpoints',
              'total_issues',

              'high',
              'medium',
              'low',
              'high_endpoints',
              'medium_endpoints',
              'low_endpoints',
              'total_url_issues',
              'url_issues_high',
              'url_issues_medium',
              'url_issues_low',
              'total_endpoint_issues',
              'endpoint_issues_high',
              'endpoint_issues_medium',
              'endpoint_issues_low',

              'explained_high',
              'explained_medium',
              'explained_low',
              'explained_high_endpoints',
              'explained_medium_endpoints',
              'explained_low_endpoints',
              'explained_total_url_issues',
              'explained_url_issues_high',
              'explained_url_issues_medium',
              'explained_url_issues_low',
              'explained_total_endpoint_issues',
              'explained_endpoint_issues_high',
              'explained_endpoint_issues_medium',
              'explained_endpoint_issues_low',

112
              'when', 'calculation')
113 114 115 116 117 118

    ordering = ["-when"]

    save_as = True


119
# todo: set is_imported flag after importing
120
@admin.register(AdministrativeRegion)
121
class AdministrativeRegionAdmin(ImportExportModelAdmin, admin.ModelAdmin):
122

123 124 125 126 127
    list_display = ('country', 'organization_type', 'admin_level', 'imported')
    search_fields = (['country', 'organization_type', 'admin_level'])
    list_filter = ('country', 'organization_type', 'admin_level', 'imported')
    fields = ('country', 'organization_type', 'admin_level', 'imported')

128 129 130 131 132 133
    actions = []

    def import_region(self, request, queryset):
        tasks = []

        for region in queryset:
134 135
            tasks.append(import_from_scratch.s([region.country], [region.organization_type])
                         | add_configuration.si(region.country, region.organization_type))
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163

        task_name = "%s (%s) " % ("Import region", ','.join(map(str, list(queryset))))
        task = group(tasks)

        job = Job.create(task, task_name, request, priority=PRIO_HIGH)
        link = reverse('admin:app_job_change', args=(job.id,))
        self.message_user(request, 'Job created, <a href="%s">%s</a>' % (link, task_name))

    import_region.short_description = '🛃  Import region'
    actions.append(import_region)

    # ah, hmm... abstractions...
    # something something abstractions
    def update_coordinates(self, request, queryset):
        tasks = []

        for region in queryset:
            tasks.append(update_coordinates.s([region.country], [region.organization_type]))

        task_name = "%s (%s) " % ("Update region", ','.join(map(str, list(queryset))))
        task = group(tasks)

        job = Job.create(task, task_name, request, priority=PRIO_HIGH)
        link = reverse('admin:app_job_change', args=(job.id,))
        self.message_user(request, 'Job created, <a href="%s">%s</a>' % (link, task_name))
    update_coordinates.short_description = '🛂  Update region'
    actions.append(update_coordinates)

164

165 166 167 168 169 170 171 172 173 174 175 176
@app.task(queue='storage')
def add_configuration(country, organization_type):
    a = Configuration(
        country=country,
        organization_type=organization_type,
        is_the_default_option=False,
        is_displayed=False,
        is_scanned=False
    )
    a.save()


177
@admin.register(Configuration)
178 179
class ConfigurationAdmin(SortableAdminMixin, ImportExportModelAdmin, admin.ModelAdmin):

180 181
    list_display = ('country', 'organization_type', 'is_displayed', 'is_the_default_option', 'is_scanned',
                    'is_reported')
182
    search_fields = (['country', 'organization_type', ])
Elger Jonker's avatar
Elger Jonker committed
183 184
    list_filter = ('country', 'organization_type', 'is_displayed', 'is_the_default_option', 'is_scanned',)
    fields = ('country', 'organization_type', 'is_displayed', 'is_the_default_option', 'is_scanned', )
185

186 187 188 189 190 191 192 193
    actions = []

    def display(self, request, queryset):

        for configuration in queryset:
            configuration.is_displayed = True
            configuration.save()

Elger Jonker's avatar
Elger Jonker committed
194
    display.short_description = '☀️ Display'
195 196 197 198 199 200 201 202
    actions.append(display)

    def hide(self, request, queryset):

        for configuration in queryset:
            configuration.is_displayed = False
            configuration.save()

Elger Jonker's avatar
Elger Jonker committed
203
    hide.short_description = '🌑 Hide'
204 205 206 207 208 209 210 211
    actions.append(hide)

    def allow_scanning(self, request, queryset):

        for configuration in queryset:
            configuration.is_scanned = True
            configuration.save()

Elger Jonker's avatar
Elger Jonker committed
212
    allow_scanning.short_description = '❤️  Allow scanning'
213 214 215 216 217 218 219 220
    actions.append(allow_scanning)

    def stop_scanning(self, request, queryset):

        for configuration in queryset:
            configuration.is_scanned = False
            configuration.save()

Elger Jonker's avatar
Elger Jonker committed
221
    stop_scanning.short_description = '💔  Stop scanning'
222 223
    actions.append(stop_scanning)

224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
    def allow_reporting(self, request, queryset):

        for configuration in queryset:
            configuration.is_reported = True
            configuration.save()

    allow_reporting.short_description = '📄️  Allow Reporting'
    actions.append(allow_reporting)

    def stop_reporting(self, request, queryset):

        for configuration in queryset:
            configuration.is_reported = False
            configuration.save()

    stop_reporting.short_description = '📄  Stop Reporting'
    actions.append(stop_reporting)

Elger Jonker's avatar
Elger Jonker committed
242
    def set_default(self, request, queryset):
243 244

        for configuration in queryset:
Elger Jonker's avatar
Elger Jonker committed
245
            configuration.is_the_default_option = True
246 247
            configuration.save()

Elger Jonker's avatar
Elger Jonker committed
248 249
    set_default.short_description = '😀  Set default'
    actions.append(set_default)
250

Elger Jonker's avatar
Elger Jonker committed
251
    def remove_default(self, request, queryset):
252 253

        for configuration in queryset:
Elger Jonker's avatar
Elger Jonker committed
254
            configuration.is_the_default_option = False
255 256
            configuration.save()

Elger Jonker's avatar
Elger Jonker committed
257 258
    remove_default.short_description = '😭  Remove default'
    actions.append(remove_default)
259 260 261 262


@admin.register(VulnerabilityStatistic)
class VulnerabilityStatisticAdmin(ImportExportModelAdmin, admin.ModelAdmin):
263
    list_display = ('country', 'organization_type', 'scan_type', 'when', 'high', 'medium', 'low', 'urls', 'endpoints')
264 265
    list_filter = ('country', 'organization_type', 'scan_type', 'when', 'high', 'medium', 'low')
    search_fields = (['country', 'organization_type', 'scan_type'])
266 267 268 269 270 271 272 273 274 275 276 277 278


@admin.register(MapDataCache)
class MapDataCacheAdmin(ImportExportModelAdmin, admin.ModelAdmin):
    list_display = ('pk', 'country', 'organization_type', 'filters', 'when', 'length')
    list_filter = ('country', 'organization_type', 'filters', 'when')
    search_fields = (['country', 'organization_type', 'filters'])

    readonly_fields = ['cached_on']

    @staticmethod
    def length(obj):
        return len(str(obj.dataset))