Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

380 lines (308 sloc) 13.591 kb
from itertools import chain
import random
import operator
import re
import logging
import time
from django.conf import settings
from django.core.cache import cache
from django.db import models
from django.db.models import Count
from django.db.models import signals
from django.db.models import Q
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes import generic
from django.core.urlresolvers import reverse
from django.template import defaultfilters
from django.contrib.auth.models import User
from geonode.core.models import AUTHENTICATED_USERS
from geonode.core.models import ANONYMOUS_USERS
from geonode.maps.models import Contact
from geonode.maps.models import Map
from geonode.maps.models import Layer
from geonode.maps.models import LayerManager
from geonode.upload.signals import upload_complete
from mapstory import gwc_config
from hitcount.models import HitCount
from agon_ratings.models import OverallRating
from agon_ratings.categories import RATING_CATEGORY_LOOKUP
_logger = logging.getLogger('mapstory.models')
def _debug(msg,*args):
_debug_enabled = _logger.isEnabledFor(logging.DEBUG)
'''Settings API - allow regular expressions to filter our layer name results'''
if hasattr(settings,'LAYER_EXCLUSIONS'):
_exclude_patterns = settings.LAYER_EXCLUSIONS
_exclude_regex = [ re.compile(e) for e in _exclude_patterns ]
_layer_name_filter = reduce(operator.or_,[ Q(name__regex=f) for f in _exclude_patterns])
def filtered_layers_query(self):
return self.get_query_set().exclude(_layer_name_filter)
LayerManager.filtered = filtered_layers_query
def get_view_cnt_for(obj):
'''Provide cached access to view cnts'''
return get_view_cnts(type(obj)).get(,0)
def get_view_cnts(model):
'''Provide cached access to view counts for a given model.
The current approach is to cache all values. An alternate approach, should
there be too many, is to partition by 'id mod some bucket size'.
key = 'view_cnt_%s' % model.__name__
cached = cache.get(key)
hit = True
if _debug_enabled:
ts = time.time()
if not cached:
hit = False
ctype = ContentType.objects.get_for_model(model)
hits = HitCount.objects.filter(content_type=ctype)
cached = dict([ (int(h.object_pk),h.hits) for h in hits])
if _debug_enabled:
_debug('view cnts for %s in %s, cached: %s',model.__name__,time.time() - ts,hit)
return cached
def get_ratings(model):
'''cached results for an objects rating'''
key = 'overall_rating_%s' % model.__name__
results = cache.get(key)
if not results:
# this next big is some hacky stuff related to rankings
choice = model.__name__.lower()
"%s.%s-%s" % (model._meta.app_label, model._meta.object_name, choice)
ct = ContentType.objects.get_for_model(model)
ratings = OverallRating.objects.filter(
content_type = ct,
category = category
results = dict([ (r.object_id, r.rating) or 0 for r in ratings])
cache.set(key, results)
except OverallRating.DoesNotExist:
return {}
return results
class SectionManager(models.Manager):
def sections_with_maps(self):
'''@todo this is broken - Get only those sections that have maps'''
return self.all().annotate(num_maps=Count('maps')).filter(num_maps__gt=0)
class TopicManager(models.Manager):
def tag(self, obj, topic, allow_create=False):
if allow_create:
topic,_ = self.get_or_create(name=topic)
topic = self.get(pk=int(topic))
related = isinstance(obj, Map) and topic.maps or topic.layers
# @todo - only allowing one topic per item (UI work needed)
class Topic(models.Model):
objects = TopicManager()
name = models.CharField(max_length=64)
layers = models.ManyToManyField(Layer,blank=True)
maps = models.ManyToManyField(Map,blank=True)
def __unicode__(self):
return 'Topic - %s' %
class Section(models.Model):
objects = SectionManager()
name = models.CharField(max_length=64)
slug = models.SlugField(max_length=64,blank=True)
text = models.TextField(null=True)
topics = models.ManyToManyField(Topic,blank=True)
order = models.IntegerField(null=True,blank=True)
def _children(self, att, **kw):
field = lambda t: getattr(t,att).filter(**kw)
return set(chain(*[ field(t) for t in self.topics.all()]))
def all_children(self):
x = self.get_maps() | self.get_layers()
return x
def get_maps(self):
return self._children('maps', publish__status=PUBLISHING_STATUS_PUBLIC)
def get_layers(self):
return self._children('layers', publish__status=PUBLISHING_STATUS_PUBLIC)
def save(self,*args,**kw):
slugtext ='&','and')
self.slug = defaultfilters.slugify(slugtext)
if self.order is None:
self.order =
def get_absolute_url(self):
return reverse('section_detail',args=[self.slug])
def __unicode__(self):
return 'Section %s' %
class Link(models.Model):
name = models.CharField(max_length=64)
href = models.CharField(max_length=256)
class VideoLinkManager(models.Manager):
def front_page_video(self):
videos = VideoLink.objects.filter(publish=True,location=_VIDEO_LOCATION_FRONT_PAGE)
if not videos:
videos = VideoLink.objects.all()
if not videos:
return None
return random.choice(videos)
def how_to_videos(self):
return VideoLink.objects.filter(publish=True,location=_VIDEO_LOCATION_HOW_TO)
class VideoLink(Link):
objects = VideoLinkManager()
title = models.CharField(max_length=32)
text = models.CharField(max_length=350)
publish = models.BooleanField(default=False)
location = models.CharField(max_length=2, choices=_VIDEO_LOCATION_CHOICES)
class ContactDetail(Contact):
'''Additional User details'''
blurb = models.CharField(max_length=140, null=True)
biography = models.CharField(max_length=1024, null=True, blank=True)
education = models.CharField(max_length=512, null=True, blank=True)
expertise = models.CharField(max_length=256, null=True, blank=True)
links = models.ManyToManyField(Link)
class Resource(models.Model):
name = models.CharField(max_length=64)
slug = models.SlugField(max_length=64,blank=True)
order = models.IntegerField(null=True,blank=True)
text = models.TextField(null=True)
def save(self,*args,**kw):
slugtext ='&','and')
self.slug = defaultfilters.slugify(slugtext)
if self.order is None:
self.order =
def get_absolute_url(self):
return reverse('mapstory_resource',args=[self.slug])
class FavoriteManager(models.Manager):
def favorites_for_user(self, user):
return self.filter(user=user)
def favorite_maps_for_user(self, user):
content_type = ContentType.objects.get_for_model(Map)
return self.favorites_for_user(user).filter(content_type=content_type)
def create_favorite(self, content_object, user):
content_type = ContentType.objects.get_for_model(type(content_object))
favorite = Favorite(
return favorite
class Favorite(models.Model):
user = models.ForeignKey(User)
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()
content_object = generic.GenericForeignKey('content_type', 'object_id')
created_on = models.DateTimeField(auto_now_add=True)
objects = FavoriteManager()
class Meta:
verbose_name = 'favorite'
verbose_name_plural = 'favorites'
unique_together = (('user', 'content_type', 'object_id'),)
def __unicode__(self):
return "%s likes %s" % (self.user, self.content_object)
class PublishingStatusMananger(models.Manager):
def get_public(self, user, model):
return model.objects.filter(owner=user, publish__status=PUBLISHING_STATUS_PUBLIC)
def get_in_progress(self, user, model):
if model == Layer:
# don't show annotations
q = model.objects.filtered()
q = model.objects.all()
q = q.filter(owner=user)
return q.exclude(publish__status=PUBLISHING_STATUS_PUBLIC)
def get_or_create_for(self, obj):
if isinstance(obj, Map):
status, _ = self.get_or_create(map=obj)
status, _ = self.get_or_create(layer=obj)
return status
def set_status(self, obj, status):
stat = self.get_or_create_for(obj)
stat.status = status
class PublishingStatus(models.Model):
'''This is a denormalized model - could have gone with a content-type
but it seemed to make the queries much more complex than needed.
Each entry should either have a map or a layer.
objects = PublishingStatusMananger()
map = models.OneToOneField(Map,related_name='publish',null=True)
layer = models.OneToOneField(Layer,related_name='publish',null=True)
status = models.CharField(max_length=8,choices=PUBLISHING_STATUS_CHOICES,default=PUBLISHING_STATUS_PRIVATE)
def check_related(self):
layers =
owner =
if len(set([owner]) | set([l.owner for l in layers])) > 1:
return [ l for l in layers if l.owner == owner]
def update_related(self):
for l in
if l.owner ==
l.publish.status = self.status
def save(self,*args,**kw):
obj = self.layer or
level = obj.LEVEL_READ
level = obj.LEVEL_NONE
obj.set_gen_level(ANONYMOUS_USERS, level)
obj.set_gen_level(AUTHENTICATED_USERS, level)
obj.set_user_level(obj.owner, obj.LEVEL_ADMIN),*args)
def create_profile(instance, sender, **kw):
if kw['created']:
ContactDetail.objects.create(user = instance)
def create_publishing_status(instance, sender, **kw):
if kw['created']:
def set_publishing_private(**kw):
instance = kw.get('layer')
PublishingStatus.objects.set_status(instance, PUBLISHING_STATUS_PRIVATE)
def configure_gwc(**kw):
instance = kw.get('layer')
def create_hitcount(instance, sender, **kw):
if kw['created']:
content_type = ContentType.objects.get_for_model(instance)
def clear_acl_cache(instance, sender, **kw):
if kw['created']:
# this will only handle the owner's cached acls - other users will be
# out of luck for the timeout period - likely not to be an issue
key = 'layer_acls_%s' %
def remove_favorites(instance, sender, **kw):
ct = ContentType.objects.get_for_model(instance)
# make sure any favorites are also deleted
signals.pre_delete.connect(remove_favorites, sender=Map)
signals.pre_delete.connect(remove_favorites, sender=Layer)
signals.post_save.connect(create_profile, sender=User)
signals.post_save.connect(create_publishing_status, sender=Map)
signals.post_save.connect(create_publishing_status, sender=Layer)
# @annoyatron - core upload sets permissions after saving the layer
# this signal allows layering the publishing status behavior on top
# this is not needed for maps
upload_complete.connect(set_publishing_private, sender=None)
upload_complete.connect(configure_gwc, sender=None)
# @annoyatron2 - after map is saved, set_default_permissions is called - hack this
Map.set_default_permissions = lambda s: None
# ensure hit count records are created up-front
signals.post_save.connect(create_hitcount, sender=Map)
signals.post_save.connect(create_hitcount, sender=Layer)
# @todo hackity hack - throw out acl cache on Layer addition
signals.post_save.connect(clear_acl_cache, sender=Layer)
Jump to Line
Something went wrong with that request. Please try again.