def reload_period_from_analytics(cls, period, verbose=False): """Replace the stats for the given period from Google Analytics.""" counts = googleanalytics.pageviews_by_document(*period_dates(period), verbose=verbose) if counts: # Close any existing connections because our load balancer times # them out at 5 minutes and the GA calls take forever. close_old_connections() # Delete and remake the rows: # Horribly inefficient until # http://code.djangoproject.com/ticket/9519 is fixed. # cls.objects.filter(period=period).delete() # Instead, we use raw SQL! cursor = connection.cursor() cursor.execute( "DELETE FROM `dashboards_wikidocumentvisits`" " WHERE `period` = %s", [period] ) # Now we create them again with fresh data. for doc_id, visits in counts.items(): cls.objects.create(document=Document(pk=doc_id), visits=visits, period=period) else: # Don't erase interesting data if there's nothing to replace it: log.warning("Google Analytics returned no interesting data," " so I kept what I had.")
def document(**kwargs): """Return an empty document with enough stuff filled out that it can be saved.""" defaults = { 'category': CATEGORIES[0][0], 'title': u'đ' + str(datetime.now()) } defaults.update(kwargs) if 'slug' not in kwargs: defaults['slug'] = slugify(defaults['title']) return Document(**defaults)
def reload_period_from_analytics(cls, period): """Replace the stats for the given period from Google Analytics.""" counts = googleanalytics.pageviews_by_document(*period_dates(period)) if counts: # Delete and remake the rows: # Horribly inefficient until # http://code.djangoproject.com/ticket/9519 is fixed. cls.objects.filter(period=period).delete() for doc_id, visits in counts.iteritems(): cls.objects.create(document=Document(pk=doc_id), visits=visits, period=period) else: # Don't erase interesting data if there's nothing to replace it: log.warning('Google Analytics returned no interesting data,' ' so I kept what I had.')
class UntranslatedReadout(Readout): title = _lazy(u'Untranslated') description = _lazy( u'This indicates there are no approved translations of these articles. ' 'Some of the articles may have proposed translations waiting to be ' 'reviewed and will appear in the Unreviewed Changes section as well.') short_title = _lazy(u'Untranslated') details_link_text = _lazy(u'All untranslated articles...') slug = 'untranslated' column4_label = _lazy(u'Updated') def _query_and_params(self, max): # Filter by product if specified. if self.product: extra_joins = PRODUCT_FILTER params = (self.locale, LAST_30_DAYS, self.product.id, settings.WIKI_DEFAULT_LANGUAGE) else: extra_joins = '' params = (self.locale, LAST_30_DAYS, settings.WIKI_DEFAULT_LANGUAGE) # Incidentally, we tried this both as a left join and as a search # against an inner query returning translated docs, and the left join # yielded a faster-looking plan (on a production corpus). # # Find non-archived, localizable documents in categories 10, # 20 and 60 having at least one ready- for-localization # revision. Of those, show the ones that have no translation. query = ( 'SELECT engdoc.slug, engdoc.title, ' 'wiki_revision.reviewed, dashboards_wikidocumentvisits.visits ' 'FROM wiki_document engdoc ' 'INNER JOIN wiki_revision ON ' 'engdoc.latest_localizable_revision_id=wiki_revision.id ' 'LEFT JOIN wiki_document translated ON ' 'engdoc.id=translated.parent_id AND translated.locale=%s ' 'LEFT JOIN dashboards_wikidocumentvisits ON ' 'engdoc.id=dashboards_wikidocumentvisits.document_id AND ' 'dashboards_wikidocumentvisits.period=%s ' + extra_joins + 'WHERE ' '(translated.id IS NULL OR translated.current_revision_id IS NULL) ' 'AND engdoc.is_localizable AND ' 'engdoc.category in (10, 20, 60) AND ' 'engdoc.locale=%s AND NOT engdoc.is_archived ' 'AND wiki_revision.content NOT LIKE "REDIRECT%%" ' + self._order_clause() + self._limit_clause(max)) return query, params def _order_clause(self): return ('ORDER BY wiki_revision.reviewed DESC, engdoc.title ASC' if self.mode == MOST_RECENT else 'ORDER BY dashboards_wikidocumentvisits.visits DESC, ' 'engdoc.title ASC') def _format_row(self, (slug, title, reviewed, visits)): # Run the data through the model to (potentially) format it and # take advantage of SPOTs (like for get_absolute_url()): d = Document(slug=slug, title=title, locale=settings.WIKI_DEFAULT_LANGUAGE) return dict(title=d.title, url=d.get_absolute_url(), visits=visits, updated=reviewed)