Exemple #1
0
  def prepare_file(self):
    zf = zipfile.ZipFile(self.data_liberation_path(), 'w')
    username = self.get_author_username()

    user_data = Query.sql("""SELECT * FROM `users`
                             WHERE `username` = %s""",
                             (username))

    content_data = Query.sql("""SELECT * FROM `content`
                                WHERE `username` = %s""",
                                (username))

    # sql data
    zf.writestr('user_data', repr(user_data))
    zf.writestr('content_data', repr(content_data))

    # files
    files = os.walk(url_factory.resource_directory(self))
    for f in files:
      for filename in f[2]:
        path = os.path.join(f[0], filename)
        arcname = path[len(
            url_factory.resource_directory(self)) - len(username):]
        zf.write(path, arcname)

    zf.close()
Exemple #2
0
 def delete(self):
     'Deletes record from database'
     query = 'DELETE FROM %s WHERE %s = %s' % (
         self.Meta.table_safe, self.Meta.pk, self.db.conn.placeholder)
     values = [getattr(self, self.Meta.pk)]
     Query.raw_sql(query, values, self.db)
     return True
Exemple #3
0
def table_exists(db, table_name):
    """
    Given an Autumn model, check to see if its table exists.
    """
    try:
        s_sql = "SELECT * FROM %s LIMIT 1;" % table_name
        Query.raw_sql(s_sql, db=db)
    except Exception:
        return False

    # if no exception, the table exists and we are done
    return True
Exemple #4
0
def table_exists(db, table_name):
    """
    Given an Autumn model, check to see if its table exists.
    """
    try:
        s_sql = "SELECT * FROM %s LIMIT 1;" % table_name
        Query.raw_sql(s_sql, db=db)
    except Exception:
        return False

    # if no exception, the table exists and we are done
    return True
Exemple #5
0
    def __new__(cls, name, bases, attrs):
        if name == "Model":
            return super(ModelBase, cls).__new__(cls, name, bases, attrs)

        new_class = type.__new__(cls, name, bases, attrs)

        if not getattr(new_class, "Meta", None):
            new_class.Meta = Empty

        if not getattr(new_class.Meta, "table", None):
            new_class.Meta.table = name.lower()
        new_class.Meta.table_safe = escape(new_class.Meta.table)

        # Assume id is the default
        if not getattr(new_class.Meta, "pk", None):
            new_class.Meta.pk = "id"

        # Create function to loop over iterable validations
        for k, v in getattr(new_class.Meta, "validations", {}).iteritems():
            if isinstance(v, (list, tuple)):
                new_class.Meta.validations[k] = ValidatorChain(*v)

        # See cursor.description
        # http://www.python.org/dev/peps/pep-0249/
        if not hasattr(new_class, "db"):
            new_class.db = autumn_db
        db = new_class.db
        q = Query.raw_sql("SELECT * FROM %s LIMIT 1" % new_class.Meta.table_safe, db=new_class.db)
        new_class._fields = [f[0] for f in q.description]

        cache.add(new_class)
        return new_class
Exemple #6
0
    def __new__(cls, name, bases, attrs):
        if name == 'Model':
            return super(ModelBase, cls).__new__(cls, name, bases, attrs)

        new_class = type.__new__(cls, name, bases, attrs)

        if not getattr(new_class, 'Meta', None):
            new_class.Meta = Empty

        if not getattr(new_class.Meta, 'table', None):
            new_class.Meta.table = name.lower()
        new_class.Meta.table_safe = escape(new_class.Meta.table)

        # Assume id is the default
        if not getattr(new_class.Meta, 'pk', None):
            new_class.Meta.pk = 'id'

        # Create function to loop over iterable validations
        for k, v in getattr(new_class.Meta, 'validations', {}).iteritems():
            if isinstance(v, (list, tuple)):
                new_class.Meta.validations[k] = ValidatorChain(*v)

        # See cursor.description
        # http://www.python.org/dev/peps/pep-0249/
        if not hasattr(new_class, "db"):
            new_class.db = autumn_db
        db = new_class.db
        q = Query.raw_sql('SELECT * FROM %s LIMIT 1' %
                          new_class.Meta.table_safe,
                          db=new_class.db)
        new_class._fields = [f[0] for f in q.description]

        cache.add(new_class)
        return new_class
Exemple #7
0
 def __get__(self, instance, owner):
     super(ForeignKey, self)._set_up(instance, owner)
     if not instance:
         return self.model
     if not self.field:
         self.field = '%s_id' % self.model.Meta.table
     conditions = {self.model.Meta.pk: getattr(instance, self.field)}
     return Query(model=self.model, conditions=conditions)[0]
Exemple #8
0
 def get_or_create(cls, **kwargs):
     'Returns Model, if not exists then create'
     query = Query(model=cls, conditions=kwargs)
     obj = query[0]
     if obj is None:
         obj = cls(**kwargs)
         obj.save()
     return obj
Exemple #9
0
 def __get__(self, instance, owner):
     super(OneToMany, self)._set_up(instance, owner)
     if not instance:
         return self.model
     if not self.field:
         self.field = '%s_id' % instance.Meta.table
     conditions = {self.field: getattr(instance, instance.Meta.pk)}
     return Query(model=self.model, conditions=conditions)
Exemple #10
0
    def _update(self):
        "Uses SQL UPDATE to update record"
        query = "UPDATE %s SET " % self.Meta.table_safe
        query += ", ".join(["%s = %s" % (escape(f), self.db.conn.placeholder) for f in self._changed])
        query += " WHERE %s = %s " % (escape(self.Meta.pk), self.db.conn.placeholder)

        values = [getattr(self, f) for f in self._changed]
        values.append(self._get_pk())

        cursor = Query.raw_sql(query, values, self.db)
Exemple #11
0
def query():
    name = ('童晓白', 'tinyfool')
    print name
    sql = 'select * from weibo where screen_name in %s' % str(name)
    print 'sql = %s' % sql
    sql = 'select * from weibo where screen_name in("童晓白","tinyfool")'
    print 'sql = %s' % sql
    q = Query(model=Weibo).sql(sql)
    for qu in q:
        print 'qu = %s' % qu['screen_name']
    return render_template('query.html')
Exemple #12
0
def create_table(db, s_create_sql):
    """
    Create a table for an Autumn class.
    """
    Query.begin(db=db)
    Query.raw_sqlscript(s_create_sql, db=db)
    Query.commit(db=db)
Exemple #13
0
def create_table(db, s_create_sql):
    """
    Create a table for an Autumn class.
    """
    Query.begin(db=db)
    Query.raw_sqlscript(s_create_sql, db=db)
    Query.commit(db=db)
Exemple #14
0
    def _update(self):
        'Uses SQL UPDATE to update record'
        query = 'UPDATE %s SET ' % self.Meta.table_safe
        query += ', '.join([
            '%s = %s' % (escape(f), self.db.conn.placeholder)
            for f in self._changed
        ])
        query += ' WHERE %s = %s ' % (escape(
            self.Meta.pk), self.db.conn.placeholder)

        values = [getattr(self, f) for f in self._changed]
        values.append(self._get_pk())

        cursor = Query.raw_sql(query, values, self.db)
Exemple #15
0
    def _new_save(self):
        "Uses SQL INSERT to create new record"
        # if pk field is set, we want to insert it too
        # if pk field is None, we want to auto-create it from lastrowid
        auto_pk = 1 and (self._get_pk() is None) or 0
        fields = [escape(f) for f in self._fields if f != self.Meta.pk or not auto_pk]
        query = "INSERT INTO %s (%s) VALUES (%s)" % (
            self.Meta.table_safe,
            ", ".join(fields),
            ", ".join([self.db.conn.placeholder] * len(fields)),
        )
        values = [getattr(self, f, None) for f in self._fields if f != self.Meta.pk or not auto_pk]
        cursor = Query.raw_sql(query, values, self.db)

        if self._get_pk() is None:
            self._set_pk(cursor.lastrowid)
        return True
Exemple #16
0
def search(profile, query, begin, page_size):
    return Query.sql(
        """SELECT *,
      (( length(title + view)
           - length(replace(title + view, %s, '')) )
           / length(%s) )
           /(length(title + view) + 1
           - length(replace(title + view, ' ', '')) )
           as ratio_of_keyword_occurence_to_other_words
         FROM `content`
         WHERE `username` = %s
           AND hidden = 0
           AND (`title` REGEXP %s
           OR  `view` REGEXP %s)
           ORDER BY ratio_of_keyword_occurence_to_other_words DESC
           LIMIT %s, %s""",
        (query, query, profile, '[[:<:]]' + query + '[[:>:]]',
         '[[:<:]]' + query + '[[:>:]]', begin, page_size))
Exemple #17
0
    def __new__(cls, name, bases, attrs):
        def __pluralize(name):
            return name + 's'
            
        def __to_table(name):
            table = name[0].lower()
            for i in range(len(name)-1):
                if name[i+1].isupper():
                    table += '_'
                table += name[i+1].lower()
            return table

        if name == 'Model':
            return super(ModelBase, cls).__new__(cls, name, bases, attrs)
            
        new_class = type.__new__(cls, name, bases, attrs)
        
        if not getattr(new_class, 'Meta', None):
            new_class.Meta = Empty
        
        new_class.Meta.raw_name = __to_table(name)
        if not getattr(new_class.Meta, 'table', None):
            new_class.Meta.table = __pluralize(new_class.Meta.raw_name)
        new_class.Meta.table_safe = escape(new_class.Meta.table)
        
        # Assume id is the default 
        if not getattr(new_class.Meta, 'pk', None):
            new_class.Meta.pk = 'id'
        
        # Create function to loop over iterable validations
        for k, v in getattr(new_class.Meta, 'validations', {}).iteritems():
            if isinstance(v, (list, tuple)):
                new_class.Meta.validations[k] = ValidatorChain(*v)
        
        # See cursor.description
        # http://www.python.org/dev/peps/pep-0249/
        if not hasattr(new_class, "db"):
            new_class.db = autumn_db
        db = new_class.db
        q = Query.raw_sql('SELECT * FROM %s LIMIT 1' % new_class.Meta.table_safe, db=new_class.db)
        new_class._fields = [f[0] for f in q.description]
        
        cache.add(new_class)
        return new_class
Exemple #18
0
def search(profile, query, begin, page_size):
  return Query.sql(
      """SELECT *,
      (( length(title + view)
           - length(replace(title + view, %s, '')) )
           / length(%s) )
           /(length(title + view) + 1
           - length(replace(title + view, ' ', '')) )
           as ratio_of_keyword_occurence_to_other_words
         FROM `content`
         WHERE `username` = %s
           AND hidden = 0
           AND (`title` REGEXP %s
           OR  `view` REGEXP %s)
           ORDER BY ratio_of_keyword_occurence_to_other_words DESC
           LIMIT %s, %s""",
           (query, query, profile,
            '[[:<:]]' + query + '[[:>:]]', '[[:<:]]' + query + '[[:>:]]',
            begin, page_size))
Exemple #19
0
    def _new_save(self):
        'Uses SQL INSERT to create new record'
        # if pk field is set, we want to insert it too
        # if pk field is None, we want to auto-create it from lastrowid
        auto_pk = 1 and (self._get_pk() is None) or 0
        fields = [
            escape(f) for f in self._fields if f != self.Meta.pk or not auto_pk
        ]
        query = 'INSERT INTO %s (%s) VALUES (%s)' % (
            self.Meta.table_safe, ', '.join(fields), ', '.join(
                [self.db.conn.placeholder] * len(fields)))
        values = [
            getattr(self, f, None) for f in self._fields
            if f != self.Meta.pk or not auto_pk
        ]
        cursor = Query.raw_sql(query, values, self.db)

        if self._get_pk() is None:
            self._set_pk(cursor.lastrowid)
        return True
Exemple #20
0
    def testmodel(self):
        # Create tables

        ### MYSQL ###
        #
        # DROP TABLE IF EXISTS author;
        # CREATE TABLE author (
        #     id INT(11) NOT NULL auto_increment,
        #     first_name VARCHAR(40) NOT NULL,
        #     last_name VARCHAR(40) NOT NULL,
        #     bio TEXT,
        #     PRIMARY KEY (id)
        # );
        # DROP TABLE IF EXISTS books;
        # CREATE TABLE books (
        #     id INT(11) NOT NULL auto_increment,
        #     title VARCHAR(255),
        #     author_id INT(11),
        #     FOREIGN KEY (author_id) REFERENCES author(id),
        #     PRIMARY KEY (id)
        # );

        ### SQLITE ###
        #
        # DROP TABLE IF EXISTS author;
        # DROP TABLE IF EXISTS books;
        # CREATE TABLE author (
        #   id INTEGER PRIMARY KEY AUTOINCREMENT,
        #   first_name VARCHAR(40) NOT NULL,
        #   last_name VARCHAR(40) NOT NULL,
        #   bio TEXT
        # );
        # CREATE TABLE books (
        #   id INTEGER PRIMARY KEY AUTOINCREMENT,
        #   title VARCHAR(255),
        #   author_id INT(11),
        #   FOREIGN KEY (author_id) REFERENCES author(id)
        # );

        for table in ('author', 'books'):
            Query.raw_sql('DELETE FROM %s' % escape(table))

        # Test Creation
        james = Author(first_name='James', last_name='Joyce')
        james.save()

        kurt = Author(first_name='Kurt', last_name='Vonnegut')
        kurt.save()

        tom = Author(first_name='Tom', last_name='Robbins')
        tom.save()

        Book(title='Ulysses', author_id=james.id).save()
        Book(title='Slaughter-House Five', author_id=kurt.id).save()
        Book(title='Jitterbug Perfume', author_id=tom.id).save()
        slww = Book(title='Still Life with Woodpecker', author_id=tom.id)
        slww.save()

        # Test ForeignKey
        self.assertEqual(slww.author.first_name, 'Tom')

        # Test OneToMany
        self.assertEqual(len(list(tom.books)), 2)

        kid = kurt.id
        del (james, kurt, tom, slww)

        # Test retrieval
        b = Book.get(title='Ulysses')[0]

        a = Author.get(id=b.author_id)[0]
        self.assertEqual(a.id, b.author_id)

        a = Author.get(id=b.id)[:]
        self.assert_(isinstance(a, list))

        # Test update
        new_last_name = 'Vonnegut, Jr.'
        a = Author.get(id=kid)[0]
        a.last_name = new_last_name
        a.save()

        a = Author.get(kid)
        self.assertEqual(a.last_name, new_last_name)

        # Test count
        self.assertEqual(Author.get().count(), 3)

        self.assertEqual(len(Book.get()[1:4]), 3)

        # Test delete
        a.delete()
        self.assertEqual(Author.get().count(), 2)

        # Test validation
        a = Author(first_name='', last_name='Ted')
        try:
            a.save()
            raise Exception('Validation not caught')
        except Model.ValidationError:
            pass

        # Test defaults
        a.first_name = 'Bill and'
        a.save()
        self.assertEqual(a.bio, 'No bio available')

        try:
            Author(first_name='I am a', last_name='BadGuy!').save()
            raise Exception('Validation not caught')
        except Model.ValidationError:
            pass
Exemple #21
0
    def testmodel(self):
        # Create tables

        ### MYSQL ###
        #
        # DROP TABLE IF EXISTS author;
        # CREATE TABLE author (
        #     id INT(11) NOT NULL auto_increment,
        #     first_name VARCHAR(40) NOT NULL,
        #     last_name VARCHAR(40) NOT NULL,
        #     bio TEXT,
        #     PRIMARY KEY (id)
        # );
        # DROP TABLE IF EXISTS books;
        # CREATE TABLE books (
        #     id INT(11) NOT NULL auto_increment,
        #     title VARCHAR(255),
        #     author_id INT(11),
        #     FOREIGN KEY (author_id) REFERENCES author(id),
        #     PRIMARY KEY (id)
        # );

        ### SQLITE ###
        #
        # DROP TABLE IF EXISTS author;
        # DROP TABLE IF EXISTS books;
        # CREATE TABLE author (
        #   id INTEGER PRIMARY KEY AUTOINCREMENT,
        #   first_name VARCHAR(40) NOT NULL,
        #   last_name VARCHAR(40) NOT NULL,
        #   bio TEXT
        # );
        # CREATE TABLE books (
        #   id INTEGER PRIMARY KEY AUTOINCREMENT,
        #   title VARCHAR(255),
        #   author_id INT(11),
        #   FOREIGN KEY (author_id) REFERENCES author(id)
        # );

        for table in ("author", "books"):
            Query.raw_sql("DELETE FROM %s" % escape(table))

        # Test Creation
        james = Author(first_name="James", last_name="Joyce")
        james.save()

        kurt = Author(first_name="Kurt", last_name="Vonnegut")
        kurt.save()

        tom = Author(first_name="Tom", last_name="Robbins")
        tom.save()

        Book(title="Ulysses", author_id=james.id).save()
        Book(title="Slaughter-House Five", author_id=kurt.id).save()
        Book(title="Jitterbug Perfume", author_id=tom.id).save()
        slww = Book(title="Still Life with Woodpecker", author_id=tom.id)
        slww.save()

        # Test ForeignKey
        self.assertEqual(slww.author.first_name, "Tom")

        # Test OneToMany
        self.assertEqual(len(list(tom.books)), 2)

        kid = kurt.id
        del (james, kurt, tom, slww)

        # Test retrieval
        b = Book.get(title="Ulysses")[0]

        a = Author.get(id=b.author_id)[0]
        self.assertEqual(a.id, b.author_id)

        a = Author.get(id=b.id)[:]
        self.assert_(isinstance(a, list))

        # Test update
        new_last_name = "Vonnegut, Jr."
        a = Author.get(id=kid)[0]
        a.last_name = new_last_name
        a.save()

        a = Author.get(kid)
        self.assertEqual(a.last_name, new_last_name)

        # Test count
        self.assertEqual(Author.get().count(), 3)

        self.assertEqual(len(Book.get()[1:4]), 3)

        # Test delete
        a.delete()
        self.assertEqual(Author.get().count(), 2)

        # Test validation
        a = Author(first_name="", last_name="Ted")
        try:
            a.save()
            raise Exception("Validation not caught")
        except Model.ValidationError:
            pass

        # Test defaults
        a.first_name = "Bill and"
        a.save()
        self.assertEqual(a.bio, "No bio available")

        try:
            Author(first_name="I am a", last_name="BadGuy!").save()
            raise Exception("Validation not caught")
        except Model.ValidationError:
            pass
Exemple #22
0
 def delete(self):
     "Deletes record from database"
     query = "DELETE FROM %s WHERE %s = %s" % (self.Meta.table_safe, self.Meta.pk, self.db.conn.placeholder)
     values = [getattr(self, self.Meta.pk)]
     Query.raw_sql(query, values, self.db)
     return True
Exemple #23
0
def dashboard_feed(profile, begin, page_size, sort_type, read_all_mode,
    specific_feed, just_local_feed, local_entry=None, remote_entry=None,
    spam=False, favorite=False, comments=False, external=None, query=None,
    from_local_date=None, from_remote_date=None):
  content_local_restrict = ""
  content_remote_restrict = ""
  parameters = [profile, profile]
  just_remote_feed = False

  sort_query = 'DESC'
  if sort_type == 'oldest':
    sort_query = 'ASC'

  if specific_feed:
    content_remote_restrict += """ AND `from_user` = %s """
    parameters.append(specific_feed)
  if local_entry:
    content_local_restrict += """ AND `id` = %s """
    parameters.append(local_entry)
  if remote_entry:
    content_remote_restrict += """ AND `id` = %s """
    parameters.append(remote_entry)
  elif begin != 0:
    # TODO should also limit lower limit when sorting by newest
    comparison = '>=' if sort_type == 'oldest' else '<='
    if from_local_date:
      content_local_restrict += (""" AND `date_created` """ +
          comparison + """ %s """)
      parameters.append(from_local_date)
    if from_remote_date:
      content_remote_restrict += (""" AND `date_created` """ +
          comparison + """ %s """)
      parameters.append(from_remote_date)
  elif (not spam and not favorite and not comments and not query
      and read_all_mode == 0):
    content_remote_restrict += """ AND `read` = 0 """
  if spam:
    just_remote_feed = True
    content_remote_restrict += """ AND `is_spam` = 1 """
  if favorite:
    just_remote_feed = True
    content_remote_restrict += """ AND `favorited` = 1 """
  if external:
    just_remote_feed = True
    content_remote_restrict += """ AND `type` = %s """
    parameters.append(external)
  if comments:
    just_remote_feed = True
    content_remote_restrict += """ AND `type` = %s """
    parameters.append('comment')
  elif (not just_local_feed and not local_entry and not remote_entry
      and not external
      and not favorite and not spam and not comments and not query):
    content_remote_restrict += """ AND `type` = %s """
    parameters.append('post')
  if query:
    just_remote_feed = True
    content_remote_restrict += """ AND (`title` LIKE %s or `view` LIKE %s)"""
    parameters.append('%' + query + '%')
    parameters.append('%' + query + '%')
  parameters += [begin, page_size]

  local_query = """
         (SELECT `id`, `username`, `title`, `view`, `date_created`,
          `favorited`, `is_spam`, `deleted`,
          `count`, `count_robot`, `date_updated`, `hidden`, `date_start`,
          `date_end`, `date_repeats`, `section`, `album`, `name`, `thumb`,
          `thread`,
          '' as `to_username`, '' as `creator`, '' as `type`,
          '' as `from_user`, '' as `post_id`, '' as `link`, 0 as `read`,
          `comments_count`, '' as `avatar`
          FROM `content`
          WHERE `username` = %s """ \
      +      content_local_restrict \
      +  """ AND `redirect` = 0
             AND `section` != 'comments'
          ORDER BY date_created """ + sort_query + """)"""

  remote_query = """
          (SELECT `id`, `username`, `title`, `view`, `date_created`,
           `favorited`, `is_spam`, `deleted`,
           0 as `count`, 0 as `count_robot`, `date_updated`, 0 as `hidden`,
           now() as `date_start`, now() as `date_end`, 0 as `date_repeats`,
           '' as `section`, '' as `album`, '' as `name`, '' as `thumb`,
           '' as `thread`,
           `to_username`, `creator`, `type`, `from_user`, `post_id`, `link`,
           `read`, `comments_count`, `avatar`
           FROM `content_remote`
           WHERE `to_username` = %s """ \
      +      content_remote_restrict \
      +  """ AND `is_spam` = 0
             AND `deleted` = 0
           ORDER BY date_created """ + sort_query + """)
           ORDER BY date_created """ + sort_query

  limit_fragment = """ LIMIT %s, %s """

  if not just_remote_feed and (just_local_feed or local_entry):
    parameters.pop(0)  # remove first profile
    return Query.sql(local_query + limit_fragment, parameters)

  if just_remote_feed or specific_feed or remote_entry:
    parameters.pop(0)  # remove first profile
    return Query.sql(remote_query + limit_fragment, parameters)

  return Query.sql(local_query + """ UNION """ + remote_query + limit_fragment,
      parameters)
Exemple #24
0
def dashboard_feed(profile,
                   begin,
                   page_size,
                   sort_type,
                   read_all_mode,
                   specific_feed,
                   just_local_feed,
                   local_entry=None,
                   remote_entry=None,
                   spam=False,
                   favorite=False,
                   comments=False,
                   external=None,
                   query=None,
                   from_local_date=None,
                   from_remote_date=None):
    content_local_restrict = ""
    content_remote_restrict = ""
    parameters = [profile, profile]
    just_remote_feed = False

    sort_query = 'DESC'
    if sort_type == 'oldest':
        sort_query = 'ASC'

    if specific_feed:
        content_remote_restrict += """ AND `from_user` = %s """
        parameters.append(specific_feed)
    if local_entry:
        content_local_restrict += """ AND `id` = %s """
        parameters.append(local_entry)
    if remote_entry:
        content_remote_restrict += """ AND `id` = %s """
        parameters.append(remote_entry)
    elif begin != 0:
        # TODO should also limit lower limit when sorting by newest
        comparison = '>=' if sort_type == 'oldest' else '<='
        if from_local_date:
            content_local_restrict += (""" AND `date_created` """ +
                                       comparison + """ %s """)
            parameters.append(from_local_date)
        if from_remote_date:
            content_remote_restrict += (""" AND `date_created` """ +
                                        comparison + """ %s """)
            parameters.append(from_remote_date)
    elif (not spam and not favorite and not comments and not query
          and read_all_mode == 0):
        content_remote_restrict += """ AND `read` = 0 """
    if spam:
        just_remote_feed = True
        content_remote_restrict += """ AND `is_spam` = 1 """
    if favorite:
        just_remote_feed = True
        content_remote_restrict += """ AND `favorited` = 1 """
    if external:
        just_remote_feed = True
        content_remote_restrict += """ AND `type` = %s """
        parameters.append(external)
    if comments:
        just_remote_feed = True
        content_remote_restrict += """ AND `type` = %s """
        parameters.append('comment')
    elif (not just_local_feed and not local_entry and not remote_entry
          and not external and not favorite and not spam and not comments
          and not query):
        content_remote_restrict += """ AND `type` = %s """
        parameters.append('post')
    if query:
        just_remote_feed = True
        content_remote_restrict += """ AND (`title` LIKE %s or `view` LIKE %s)"""
        parameters.append('%' + query + '%')
        parameters.append('%' + query + '%')
    parameters += [begin, page_size]

    local_query = """
         (SELECT `id`, `username`, `title`, `view`, `date_created`,
          `favorited`, `is_spam`, `deleted`,
          `count`, `count_robot`, `date_updated`, `hidden`, `date_start`,
          `date_end`, `date_repeats`, `section`, `album`, `name`, `thumb`,
          `thread`,
          '' as `to_username`, '' as `creator`, '' as `type`,
          '' as `from_user`, '' as `post_id`, '' as `link`, 0 as `read`,
          `comments_count`, '' as `avatar`
          FROM `content`
          WHERE `username` = %s """ \
        +      content_local_restrict \
        +  """ AND `redirect` = 0
             AND `section` != 'comments'
          ORDER BY date_created """ + sort_query + """)"""

    remote_query = """
          (SELECT `id`, `username`, `title`, `view`, `date_created`,
           `favorited`, `is_spam`, `deleted`,
           0 as `count`, 0 as `count_robot`, `date_updated`, 0 as `hidden`,
           now() as `date_start`, now() as `date_end`, 0 as `date_repeats`,
           '' as `section`, '' as `album`, '' as `name`, '' as `thumb`,
           '' as `thread`,
           `to_username`, `creator`, `type`, `from_user`, `post_id`, `link`,
           `read`, `comments_count`, `avatar`
           FROM `content_remote`
           WHERE `to_username` = %s """ \
        +      content_remote_restrict \
        +  """ AND `is_spam` = 0
             AND `deleted` = 0
           ORDER BY date_created """ + sort_query + """)
           ORDER BY date_created """ + sort_query

    limit_fragment = """ LIMIT %s, %s """

    if not just_remote_feed and (just_local_feed or local_entry):
        parameters.pop(0)  # remove first profile
        return Query.sql(local_query + limit_fragment, parameters)

    if just_remote_feed or specific_feed or remote_entry:
        parameters.pop(0)  # remove first profile
        return Query.sql(remote_query + limit_fragment, parameters)

    return Query.sql(
        local_query + """ UNION """ + remote_query + limit_fragment,
        parameters)
Exemple #25
0
    def get(cls, _obj_pk=None, **kwargs):
        'Returns Query object'
        if _obj_pk is not None:
            return cls.get(**{cls.Meta.pk: _obj_pk})[0]

        return Query(model=cls, conditions=kwargs)