Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_map_json(self):
map_obj = Map.objects.all().first()
map_id = map_obj.id
# Test that saving a map when not logged in gives 401
response = self.client.put(
reverse(
'map_json',
args=(
str(map_id),
)),
data=self.viewer_config,
content_type="text/json")
self.assertEqual(response.status_code, 401)
self.client.login(username=self.user, password=self.passwd)
response = self.client.put(
reverse(
'map_json',
def test_document_isuploaded(self):
"""/documents/upload -> Test uploading a document"""
f = SimpleUploadedFile(
'test_img_file.gif',
self.imgfile.read(),
'image/gif')
m = Map.objects.all()[0]
self.client.login(username='admin', password='admin')
response = self.client.post(
reverse('document_upload'),
data={
'file': f,
'title': 'uploaded_document',
'q': m.id,
'type': 'map',
'permissions': '{"users":{"AnonymousUser": ["view_resourcebase"]}}'},
follow=True)
self.assertEquals(response.status_code, 200)
def test_create_document_with_rel(self):
"""Tests the creation of a document with no a map related"""
f = SimpleUploadedFile(
'test_img_file.gif',
self.imgfile.read(),
'image/gif')
superuser = get_user_model().objects.get(pk=2)
c = Document.objects.create(
doc_file=f,
owner=superuser,
title='theimg')
m = Map.objects.all()[0]
ctype = ContentType.objects.get_for_model(m)
_l = DocumentResourceLink.objects.create(
document_id=c.id,
content_type=ctype,
object_id=m.id)
self.assertEquals(Document.objects.get(pk=c.id).title, 'theimg')
self.assertEquals(DocumentResourceLink.objects.get(pk=_l.id).object_id,
m.id)
def _get_search_context():
cache_key = 'simple_search_context'
context = cache.get(cache_key)
if context: return context
counts = {
'maps' : Map.objects.count(),
'layers' : Layer.objects.count(),
'vector' : Layer.objects.filter(storeType='dataStore').count(),
'raster' : Layer.objects.filter(storeType='coverageStore').count(),
'documents': Document.objects.count(),
'users' : Profile.objects.count(),
}
if "geonode.contrib.groups" in settings.INSTALLED_APPS:
counts['groups'] = Group.objects.count()
topics = Layer.objects.all().values_list('topic_category',flat=True)
topic_cnts = {}
for t in topics: topic_cnts[t] = topic_cnts.get(t,0) + 1
context = {
'viewer_config': _viewer_config,
"site" : settings.SITEURL,
'counts' : counts,
def fetch_map(map_id):
try:
return Map.objects.get(pk=map_id)
except Map.DoesNotExist:
print 'No map found with id: %s' % map_id
return None
def map_detail(request, mapid, snapshot=None, template='maps/map_detail.html'):
'''
The view that show details of each map
'''
map_obj = _resolve_map(
request,
mapid,
'base.view_resourcebase',
_PERMISSION_MSG_VIEW)
# Update count for popularity ranking,
# but do not includes admins or resource owners
if request.user != map_obj.owner and not request.user.is_superuser:
Map.objects.filter(
id=map_obj.id).update(
popular_count=F('popular_count') + 1)
if snapshot is None:
config = map_obj.viewer_json(request)
else:
config = snapshot_config(snapshot, map_obj, request)
register_event(request, EventType.EVENT_VIEW, map_obj.title)
config = json.dumps(config)
layers = MapLayer.objects.filter(map=map_obj.id)
links = map_obj.link_set.download()
group = None
if map_obj.group:
def curated(request):
maps = Map.objects.filter(featured=True)[:5]
context = RequestContext(request, dict(maps=maps), [resource_urls])
return render_to_response('curated.html', context_instance=context)
formatted_layers.append(formatted_map_layer)
formatted_obj['layers'] = formatted_layers
# replace thumbnail_url with curated_thumbs
if hasattr(obj, 'curatedthumbnail'):
if hasattr(obj.curatedthumbnail.img_thumbnail, 'url'):
formatted_obj['thumbnail_url'] = obj.curatedthumbnail.img_thumbnail.url
else:
formatted_obj['thumbnail_url'] = ''
formatted_objects.append(formatted_obj)
return formatted_objects
class Meta(CommonMetaApi):
paginator_class = CrossSiteXHRPaginator
queryset = Map.objects.distinct().order_by('-date')
resource_name = 'maps'
authentication = MultiAuthentication(SessionAuthentication(),
OAuthAuthentication(),
GeonodeApiKeyAuthentication())
class DocumentResource(CommonModelApi):
"""Documents API"""
def format_objects(self, objects):
"""
Formats the objects and provides reference to list of layers in map
resources.
:param objects: Map objects
mapid,
'base.view_resourcebase',
_PERMISSION_MSG_VIEW)
return HttpResponse(
json.dumps(
map_obj.viewer_json(request)))
elif request.method == 'PUT':
if not request.user.is_authenticated():
return HttpResponse(
_PERMISSION_MSG_LOGIN,
status=401,
content_type="text/plain"
)
map_obj = Map.objects.get(id=mapid)
if not request.user.has_perm(
'change_resourcebase',
map_obj.get_self_resource()):
return HttpResponse(
_PERMISSION_MSG_SAVE,
status=401,
content_type="text/plain"
)
try:
map_obj.update_from_viewer(request.body, context={'request': request, 'mapId': mapid, 'map': map_obj})
update_ext_map(request, map_obj)
MapSnapshot.objects.create(
config=clean_config(
request.body),
map=map_obj,
user=request.user)