Exemple #1
0
 def clean(self):
     if 'geom' in self._get_changed_fields():
         if self.zones:
             raise db.ValidationError(
                 'The spatial coverage already has a Geozone')
     if 'zones' in self._get_changed_fields():
         if self.geom:
             raise db.ValidationError(
                 'The spatial coverage already has a Geometry')
Exemple #2
0
    def add_badge(self, kind):
        '''Perform an atomic prepend for a new badge'''
        badge = self.get_badge(kind)
        if badge:
            return badge
        if kind not in getattr(self, '__badges__', {}):
            msg = 'Unknown badge type for {model}: {kind}'
            raise db.ValidationError(
                msg.format(model=self.__class__.__name__, kind=kind))
        badge = Badge(kind=kind)
        if current_user.is_authenticated:
            badge.created_by = current_user.id

        self.update(__raw__={
            '$push': {
                'badges': {
                    '$each': [badge.to_mongo()],
                    '$position': 0
                }
            }
        })
        self.reload()
        post_save.send(self.__class__, document=self)
        on_badge_added.send(self, kind=kind)
        return self.get_badge(kind)
Exemple #3
0
 def validate(self, value):
     if not isinstance(value, dict):
         raise db.ValidationError('Should be a dict instance')
Exemple #4
0
 def validate(self, value):
     kinds = [b.kind for b in value]
     if len(kinds) > len(set(kinds)):
         raise db.ValidationError(
             'Duplicate badges for a given kind is not allowed')
     return super(BadgesList, self).validate(value)
Exemple #5
0
 def validate(self, clean=True):
     badges = getattr(self._instance, '__badges__', {})
     if self.kind not in badges.keys():
         raise db.ValidationError('Unknown badge type %s' % self.kind)
     return super(Badge, self).validate(clean=clean)