예제 #1
0
 def counters(self):
     if self._counters is None:
         self._counters = ListCounter.all().ancestor(self.key()).get()
         if self._counters is None:
             self._counters = ListCounter(parent=self)
             self._counters.put()
     return self._counters
예제 #2
0
 def put(self, from_comment=False):
     if self.is_saved():
         super(List, self).put()
         if from_comment:
             return self
         from watchers import modified_list, deleted_list
         if not self.active:
             list_deleted.send(sender=self)
         else:
             list_modified.send(sender=self)
     else:
         super(List, self).put()
         counter = ListCounter(parent=self)
         a = db.put_async(counter)
         from watchers import new_list
         list_new.send(sender=self)
         a.get_result()
예제 #3
0
class List(db.polymodel.PolyModel, model_plus.Model):
    '''
        NO USAR ESTA LISTA, USAR LOS MODELOS ESPECIFICOS :D
    '''
    name = db.StringProperty(required=True)
    description = db.TextProperty()
    keys = db.ListProperty(db.Key)
    created = db.DateTimeProperty(auto_now_add = True)
    modified = db.DateTimeProperty(auto_now=True)
    active = db.BooleanProperty(default=True)
    _short_url = db.URLProperty(indexed=False)
    count = db.IntegerProperty(default=0)  # numero de sugerencias en la lista

    _counters = None
    _new = False

    @property
    def id(self):
        return int(self.key().id())
    
    @property
    def short_url(self):
        from os import environ
        if environ['HTTP_HOST'] == 'localhost:8080':
            return 'http://%s%s' % (environ['HTTP_HOST'], self.get_absolute_url())
        if self._short_url is None:
            self._get_short_url()
            if self._short_url is not None:
                self.put()
            else:
                from os import environ
                return 'http://%s%s' % (environ['HTTP_HOST'], self.get_absolute_url())
        return self._short_url

    @property
    def counters(self):
        if self._counters is None:
            self._counters = ListCounter.all().ancestor(self.key()).get()
            if self._counters is None:
                self._counters = ListCounter(parent=self)
                self._counters.put()
        return self._counters

    @classproperty
    def objects(self):
        return ListHelper()

    def _pre_put(self):
        self.count = len(self.keys)
        if not self.is_saved():
            self._get_short_url()
            self._new = True
            
    def put(self, from_comment=False):
        if self.is_saved():
            super(List, self).put()
            if from_comment:
                return self
            from watchers import modified_list, deleted_list
            if not self.active:
                list_deleted.send(sender=self)
            else:
                list_modified.send(sender=self)
        else:
            super(List, self).put()
            counter = ListCounter(parent=self)
            a = db.put_async(counter)
            from watchers import new_list
            list_new.send(sender=self)
            a.get_result()

    def delete(self):
        children = db.query_descendants(self).fetch(100)
        for c in children:
            c.delete()
        return db.delete_async(self)

    def to_dict(self, resolve=False, instances=None):
            dict = {'id': self.id,
                    'name': self.name,
                    'description': self.description,
                    'modified': self.modified if self.modified is not None else 0,
                    'created': self.created if self.created is not None else 0,
                    'tags': self.tags if hasattr(self, 'tags') else None,
                    'count': self.count,
                    'counters': self.counters.to_dict() if self.counters is not None else None,
                    'keys': [i.id() for i in self.keys],
                    'visibility': self._get_visibility(),
                    'get_absolute_url': self.get_absolute_url(),
                    'get_absolute_fburl': self.get_absolute_fburl(),
                    'short_url': self.short_url,
                    }
            if resolve:
                if instances is not None:
                    dict['instances'] = [instances[k] for k in self.keys]
                    dict['user'] = instances.get(ListSuggestion.user.get_value_for_datastore(self), self.user)
                else:
                    dict['instances'] = db.get(self.keys)
            else:
                dict['user'] = self.user.username
            return dict

    def to_json(self):
        from libs.jsonrpc.jsonencoder import JSONEncoder
        return simplejson.dumps(self.to_dict(), cls=JSONEncoder)

    def __str__(self):
        return unicode(self.name).encode('utf-8')

    def __unicode__(self):
        return self.name
    
    def get_absolute_url(self):
        return '/list/%s/' % str(self.id)
    
    def get_absolute_fburl(self):
        return '/fb%s' % self.get_absolute_url()
    
    def _get_short_url(self):
        from libs.vavag import VavagRequest
        from os import environ
        try:
            # parche hasta conseguir que se cachee variable global
            client = VavagRequest(settings.SHORTENER_ACCESS['user'], settings.SHORTENER_ACCESS['key'])
            response =  client.set_pack('http://%s%s' % (environ['HTTP_HOST'], self.get_absolute_url()))
            self._short_url = response['packUrl']
        except Exception, e:
            import logging
            logging.error('ERROR EN VAVAG: %s' % e.message)
            self._short_url = None