-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
42b6165
commit a5769e0
Showing
115 changed files
with
18,048 additions
and
1,110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
from django.http import HttpResponse | ||
from django.contrib.auth import authenticate, login | ||
from functools import wraps | ||
from geonode.contrib.dataverse_connect.dv_utils import MessageHelperJSON | ||
|
||
|
||
def http_basic_auth_for_api(view_func): | ||
""" | ||
Decorator for an API request, use HTTP basic authentication | ||
Based on Django snippets: https://djangosnippets.org/snippets/1304/ and https://djangosnippets.org/snippets/2313/ | ||
On failure, return a 401 and JSON message similar to: | ||
{"message": "Login Failed", "success": false} | ||
:param view_func: | ||
:return: | ||
""" | ||
@wraps(view_func) | ||
def _decorator(request, *args, **kwargs): | ||
|
||
# (1) Does this request have HTTP_AUTHORIZATION? | ||
# | ||
if not 'HTTP_AUTHORIZATION' in request.META: | ||
# json_msg = json.dumps(dict(success=False, message="Login required")) | ||
json_msg = MessageHelperJSON.get_json_fail_msg("Login required") | ||
return HttpResponse(status=401, content=json_msg, content_type="application/json") | ||
|
||
# (2) Get username and password from the HTTP_AUTHORIZATION | ||
# and try to login | ||
# | ||
authmeth, auth = request.META['HTTP_AUTHORIZATION'].split(' ', 1) | ||
if authmeth.lower() == 'basic': | ||
auth = auth.strip().decode('base64') | ||
username, password = auth.split(':', 1) | ||
# (2a) Attempt to authenticate | ||
# | ||
user = authenticate(username=username, password=password) | ||
if user is not None and user.is_active: | ||
# (2b) Authentication success, now log in | ||
# | ||
login(request, user) | ||
|
||
# (2c) return to the view logged in | ||
# | ||
return view_func(request, *args, **kwargs) | ||
|
||
# Nope, the log in Failed | ||
# | ||
json_msg = MessageHelperJSON.get_json_fail_msg("Login failed") | ||
#json_msg = json.dumps(dict(success=False, message="Login failed")) | ||
return HttpResponse(status=401, content=json_msg, content_type="application/json") | ||
|
||
return _decorator |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,98 @@ | ||
from django.contrib import admin | ||
from geonode.layers.models import Attribute | ||
from .models import DataTable, TableJoin, JoinTarget, JoinTargetFormatType, GeocodeType | ||
from django import forms | ||
from geonode.layers.models import Layer, Attribute as LayerAttribute | ||
from .models import DataTable, DataTableAttribute, TableJoin, JoinTarget, JoinTargetFormatType, GeocodeType, LatLngTableMappingRecord | ||
from .admin_forms import JoinTargetAdminForm, TableJoinAdminForm | ||
import string | ||
|
||
class DataTableAdmin(admin.ModelAdmin): | ||
model = DataTable | ||
search_fields = ('title',) | ||
|
||
list_display = ( | ||
'id', | ||
'title', | ||
'owner', | ||
'created', | ||
'table_name', | ||
'tablespace') | ||
|
||
list_display_links = ('title',) | ||
|
||
list_filter = ('tablespace', | ||
'owner') | ||
|
||
class DataTableAttributeAdmin(admin.ModelAdmin): | ||
list_display = ('attribute', | ||
'attribute_label', | ||
'datatable', | ||
'attribute_type', | ||
'searchable') | ||
|
||
list_filter = ('datatable', | ||
'searchable', | ||
'attribute_type') | ||
|
||
class LatLngTableMappingRecordAdmin(admin.ModelAdmin): | ||
search_fields = ('title',) | ||
list_display = ('datatable', | ||
'lat_attribute', | ||
'lng_attribute', | ||
'layer', | ||
'mapped_record_count', | ||
'unmapped_record_count', | ||
'created') | ||
list_filter = ('datatable',)# 'layer', ) | ||
|
||
|
||
|
||
class TableJoinAdmin(admin.ModelAdmin): | ||
model = TableJoin | ||
model = TableJoin | ||
form = TableJoinAdminForm | ||
list_display = ('id', 'join_layer', | ||
'datatable', 'table_attribute', | ||
'source_layer', 'layer_attribute', | ||
'created', 'modified') | ||
|
||
search_fields = ('source_layer__name', | ||
'datatable__title', | ||
'datatable__table_name', | ||
'join_layer__name') | ||
|
||
|
||
class JoinTargetAdmin(admin.ModelAdmin): | ||
model = JoinTarget | ||
form = JoinTargetAdminForm | ||
list_display = ('name', | ||
'layer', | ||
'attribute', | ||
'geocode_type', | ||
'expected_format', | ||
'year') | ||
|
||
readonly_fields = ('return_to_layer_admin', ) | ||
|
||
search_fields = ('name', 'layer__name') | ||
|
||
def formfield_for_foreignkey(self, db_field, request, **kwargs): | ||
if db_field.name == 'attribute': | ||
kwargs["queryset"] = Attribute.objects.filter( | ||
resource=request.GET.get('layer')) | ||
return super(JoinTargetAdmin, self).formfield_for_foreignkey( | ||
db_field, request, **kwargs) | ||
layer_id = request.GET.get('layer', 'nope') | ||
if layer_id.isdigit(): | ||
kwargs["queryset"] = LayerAttribute.objects.filter( | ||
layer=request.GET.get('layer')) | ||
return super(JoinTargetAdmin, self).formfield_for_foreignkey(\ | ||
db_field, request, **kwargs) | ||
|
||
|
||
class JoinTargetFormatTypeAdmin(admin.ModelAdmin): | ||
search_fields = ('name',) | ||
readonly_fields = ('created', 'modified') | ||
|
||
|
||
admin.site.register(DataTable, DataTableAdmin) | ||
admin.site.register(DataTableAttribute, DataTableAttributeAdmin) | ||
admin.site.register(TableJoin, TableJoinAdmin) | ||
admin.site.register(JoinTarget, JoinTargetAdmin) | ||
admin.site.register(JoinTargetFormatType) | ||
admin.site.register(JoinTargetFormatType, JoinTargetFormatTypeAdmin) | ||
admin.site.register(GeocodeType) | ||
admin.site.register(LatLngTableMappingRecord, LatLngTableMappingRecordAdmin) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
from django import forms | ||
from geonode.layers.models import Layer, Attribute as LayerAttribute | ||
from geonode.contrib.datatables.models import DataTable, DataTableAttribute | ||
|
||
class TableJoinAdminForm(forms.ModelForm): | ||
""" | ||
Limit the layer and attribute choices. | ||
""" | ||
def __init__(self, *args, **kwargs): | ||
super(TableJoinAdminForm, self).__init__(*args, **kwargs) | ||
|
||
if self.instance and self.instance.id: | ||
# Yes, limit choices | ||
|
||
# Set the datatable and attribute | ||
self.fields['datatable'].queryset = DataTable.objects.filter(\ | ||
pk=self.instance.datatable.id) | ||
|
||
self.fields['table_attribute'].queryset = DataTableAttribute.objects.filter(\ | ||
datatable=self.instance.datatable.id) | ||
|
||
# Set the source layer and attribute | ||
self.fields['source_layer'].queryset = Layer.objects.filter(\ | ||
pk=self.instance.source_layer.id) | ||
|
||
self.fields['layer_attribute'].queryset = LayerAttribute.objects.filter(\ | ||
layer=self.instance.source_layer.id) | ||
|
||
# Set the Join layer | ||
self.fields['join_layer'].queryset = Layer.objects.filter(\ | ||
pk=self.instance.join_layer.id) | ||
|
||
elif 'initial' in kwargs: | ||
# These objects can't be created through the admin | ||
# Also, production would create dropdowns with 1000s of listings | ||
# | ||
self.fields['datatable'].queryset = DataTable.objects.none() | ||
self.fields['table_attribute'].queryset = DataTableAttribute.objects.none() | ||
|
||
self.fields['source_layer'].queryset = Layer.objects.none() | ||
self.fields['layer_attribute'].queryset = LayerAttribute.objects.none() | ||
|
||
self.fields['join_layer'].queryset = Layer.objects.none() | ||
|
||
|
||
class JoinTargetAdminForm(forms.ModelForm): | ||
""" | ||
Limit the JoinTarget Layer and LayerAttribute choices. | ||
(If not limited, the page can take many minutes--or never load | ||
e.g. listing 20k+ layers and all of the attributes in those layers | ||
) | ||
""" | ||
def __init__(self, *args, **kwargs): | ||
super(JoinTargetAdminForm, self).__init__(*args, **kwargs) | ||
|
||
selected_layer_id = kwargs.get('initial', {}).get('layer', None) | ||
|
||
# Is this an existing/saved Join Target? | ||
# | ||
if self.instance and self.instance.id: | ||
# Yes, limit choices to the chosen layer and its attributes | ||
# | ||
self.fields['layer'].queryset = Layer.objects.filter(\ | ||
pk=self.instance.layer.id) | ||
self.fields['attribute'].queryset = LayerAttribute.objects.filter(\ | ||
layer=self.instance.layer.id) | ||
|
||
elif selected_layer_id and selected_layer_id.isdigit(): | ||
self.fields['layer'].queryset = Layer.objects.filter(\ | ||
pk=selected_layer_id) | ||
self.fields['attribute'].queryset = LayerAttribute.objects.filter(\ | ||
layer=selected_layer_id) | ||
|
||
|
||
elif 'initial' in kwargs: | ||
# We can't "afford" to list everything. | ||
# Don't list any layers or their attributes | ||
# - An admin template instructs the user on how | ||
# to add a new JoinTarget via the Layer admin | ||
# | ||
self.fields['layer'].queryset = Layer.objects.none() | ||
self.fields['attribute'].queryset = LayerAttribute.objects.none() |
Oops, something went wrong.