def get_user_tags(self, use_solr=True): if use_solr: query = SolrQuery() query.set_dismax_query('') filter_query = 'username:\"%s\"' % self.user.username query.set_query_options(field_list=["id"], filter_query=filter_query) query.add_facet_fields("tag") query.set_facet_options("tag", limit=10, mincount=1) solr = Solr(settings.SOLR_URL) try: results = SolrResponseInterpreter(solr.select(unicode(query))) except SolrException as e: return False except Exception as e: return False return [{'name': tag, 'count': count} for tag, count in results.facets['tag']] else: return DelayedQueryExecuter(""" SELECT tags_tag.name AS name, X.c AS count FROM ( SELECT tag_id, count(*) as c FROM tags_taggeditem LEFT JOIN sounds_sound ON object_id=sounds_sound.id WHERE tags_taggeditem.user_id=%d AND sounds_sound.moderation_state='OK' AND sounds_sound.processing_state='OK' GROUP BY tag_id ORDER BY c DESC LIMIT 10) AS X LEFT JOIN tags_tag ON tags_tag.id=X.tag_id ORDER BY tags_tag.name;""" % self.user_id)
def latest_additions(self, num_sounds, period='2 weeks', use_interval=True): interval_query = ("and created > now() - interval '%s'" % period) if use_interval else "" query = """ select username, sound_id, extra from ( select (select username from auth_user where auth_user.id = user_id) as username, max(id) as sound_id, max(created) as created, count(*) - 1 as extra from sounds_sound where processing_state = 'OK' and moderation_state = 'OK' %s group by user_id ) as X order by created desc limit %d;""" % (interval_query, num_sounds) return DelayedQueryExecuter(query)
def get_tagcloud(self): return DelayedQueryExecuter(""" select tags_tag.name as name, X.c as count from ( select tag_id, count(*) as c from tags_taggeditem left join sounds_sound on object_id=sounds_sound.id where tags_taggeditem.user_id=%d and sounds_sound.moderation_state='OK' and sounds_sound.processing_state='OK' group by tag_id order by c desc limit 10 ) as X left join tags_tag on tags_tag.id=X.tag_id order by tags_tag.name;""" % self.user_id)
except Exception, e: return False return [{ 'name': tag, 'count': count } for tag, count in results.facets['tag']] else: return DelayedQueryExecuter(""" SELECT tags_tag.name AS name, X.c AS count FROM ( SELECT tag_id, count(*) as c FROM tags_taggeditem LEFT JOIN sounds_sound ON object_id=sounds_sound.id WHERE tags_taggeditem.user_id=%d AND sounds_sound.moderation_state='OK' AND sounds_sound.processing_state='OK' GROUP BY tag_id ORDER BY c DESC LIMIT 10) AS X LEFT JOIN tags_tag ON tags_tag.id=X.tag_id ORDER BY tags_tag.name;""" % self.user_id) def can_post_in_forum(self): user_has_posts_pending_to_moderate = self.user.post_set.filter( moderation_state="NM").count() > 0 if user_has_posts_pending_to_moderate: return False, "We're sorry but you can't post to the forum because you have previous posts still " \ "pending to moderate" if self.user.post_set.all().count() >= 1 and self.user.sounds.all(