Example #1
0
def searches(request):
    if request.GET.has_key('max-age'):
        max_age = int(request.GET['max-age'])
        oldest_dt = datetime.datetime.now() - datetime.timedelta(days=max_age)
        oldest = "now() - interval '%d day'" % max_age
    else:
        oldest_dt = None
        oldest = "'-infinity'"

    c = db.cursor()
    # Pity the database server . . .
    c.execute('SELECT s.id, s.name, s.href '
              'FROM scryer_pageviews p, scryer_searchengines s '
              'WHERE p.referrer ~ r.regex AND NOT r.ignore '
              "AND p.served > %s "
              'GROUP BY r.id, r.name, r.href '
              'UNION '
              'SELECT 0, referrer, referrer, COUNT(*) '
              'FROM scryer_pageviews '
              'WHERE NOT EXISTS (SELECT * FROM scryer_aggregatedreferrers WHERE referrer ~ regex) '
              "AND referrer <> '' "
              "AND served > %s "
              'GROUP BY referrer '
              'ORDER BY 4 DESC' % (oldest, oldest))
    referrers = [ { 'url': r[2],
                    'text': r[1],
                    'count': r[3], }
                  for r in c.fetchall() ]
    c.close()

    return render_to_response('scryer/referrers', request, {
            'referrers': referrers,
            'oldest': oldest_dt, })
Example #2
0
 def set(self, key, value, timeout=None):
     if timeout is None:
         timeout = self.default_timeout
     cursor = db.cursor()
     cursor.execute("SELECT COUNT(*) FROM %s" % self._table)
     num = cursor.fetchone()[0]
     now = datetime.now().replace(microsecond=0)
     exp = datetime.fromtimestamp(time.time() + timeout).replace(microsecond=0)
     if num > self._max_entries:
         self._cull(cursor, now)
     encoded = base64.encodestring(pickle.dumps(value, 2)).strip()
     cursor.execute("SELECT cache_key FROM %s WHERE cache_key = %%s" % self._table, [key])
     try:
         if cursor.fetchone():
             cursor.execute(
                 "UPDATE %s SET value = %%s, expires = %%s WHERE cache_key = %%s" % self._table,
                 [encoded, str(exp), key],
             )
         else:
             cursor.execute(
                 "INSERT INTO %s (cache_key, value, expires) VALUES (%%s, %%s, %%s)" % self._table,
                 [key, encoded, str(exp)],
             )
     except DatabaseError:
         # To be threadsafe, updates/inserts are allowed to fail silently
         pass
     else:
         db.commit()
def clean_up():
    # Clean up old database records
    cursor = db.cursor()
    cursor.execute("DELETE FROM %s WHERE %s < NOW()" % \
        (db.quote_name('core_sessions'), db.quote_name('expire_date')))
    cursor.execute("DELETE FROM %s WHERE %s < NOW() - INTERVAL '1 week'" % \
        (db.quote_name('registration_challenges'), db.quote_name('request_date')))
    db.commit()
Example #4
0
def sessions(request):
    c = db.cursor()
    c.execute('SELECT session_key FROM scryer_pageviews '
              'GROUP BY session_key ORDER BY MAX(served) DESC '
              'LIMIT 10')
    session_keys = c.fetchall()
    sessions = [ pageviews.get_session(sk) for sk, in session_keys ]
    return render_to_response('scryer/sessions', request, locals())
Example #5
0
 def get(self, key, default=None):
     cursor = db.cursor()
     cursor.execute("SELECT cache_key, value, expires FROM %s WHERE cache_key = %%s" % self._table, [key])
     row = cursor.fetchone()
     if row is None:
         return default
     now = datetime.now()
     if row[2] < now:
         cursor.execute("DELETE FROM %s WHERE cache_key = %%s" % self._table, [key])
         db.commit()
         return default
     return pickle.loads(base64.decodestring(row[1]))
Example #6
0
 def _module_get_unread(user):
     from django.core.db import db
     c = db.cursor()
     c.execute('''
             SELECT aggemam_posts.id
             FROM aggemam_posts, aggemam_subscriptions
             WHERE aggemam_subscriptions.user_id = %d
             AND aggemam_subscriptions.feed_id = aggemam_posts.feed_id
             AND NOT EXISTS (SELECT * FROM aggemam_userpostmarks
                             WHERE aggemam_userpostmarks.user_id = aggemam_subscriptions.user_id
                             AND aggemam_userpostmarks.post_id = aggemam_posts.id
                             AND aggemam_userpostmarks.mark = 'rd')
             ORDER BY aggemam_posts.posted ASC
               ''' % user.id)
     return [ get_object(pk=post_id) for post_id, in c.fetchall() ]
Example #7
0
def archive_index(request):
    c = db.cursor()
    # FIXME -- use db abstraction layer for DATE_TRUNC
    c.execute("""SELECT DISTINCT DATE_TRUNC('month', posted) AS posted
                 FROM blog_posts ORDER BY posted DESC""")
    rows = c.fetchall()
    c.close()

    c = Context(request, months=[ {
                    'date': date,
                    'posts': len(posts.get_list(listed__exact=True,
                                                posted__year=date.year,
                                                posted__month=date.month)),
                    } for date, in rows ])
    t = template_loader.get_template('blog/archive-index')
    return HttpResponse(t.render(c))
Example #8
0
def main(argv):
	print time.strftime("[%F %T]"), "breeder.py started"
	cursor = db.cursor()
	try:
		while True:
			gen = organisms.get_current_generation()
			cursor.execute("""
				SELECT sum(upvotes+downvotes) FROM art_organisms
					WHERE generation=%s
			""", [gen])
			votes = cursor.fetchone()[0]
			if votes is None: raise ValueError("No current generation?")
			else: votes = int(votes)
			needed = settings.GENERATION_SIZE * settings.VOTES_NEEDED
			if votes >= needed:
				set_status("Breeding generation %s, please wait!" % (gen+1))
				breed()
			else:
				set_status("Generation %(gen)s is in voting: %(votes)s votes out of %(needed)s needed" % locals())
			time.sleep(DELAY)
	finally:
		cursor.close()
Example #9
0
 def has_key(self, key):
     cursor = db.cursor()
     cursor.execute("SELECT cache_key FROM %s WHERE cache_key = %%s" % self._table, [key])
     return cursor.fetchone() is not None
Example #10
0
 def delete(self, key):
     cursor = db.cursor()
     cursor.execute("DELETE FROM %s WHERE cache_key = %%s" % self._table, [key])
     db.commit()
Example #11
0
    def run_tests(self):
        from django.conf import settings
        from django.core.db import db
        from django.core import management, meta

        # Manually set INSTALLED_APPS to point to the test app.
        settings.INSTALLED_APPS = (APP_NAME,)

        # Determine which models we're going to test.
        test_models = get_test_models()
        if self.which_tests:
            # Only run the specified tests.
            bad_models = [m for m in self.which_tests if m not in test_models]
            if bad_models:
                sys.stderr.write("Models not found: %s\n" % bad_models)
                sys.exit(1)
            else:
                test_models = self.which_tests

        self.output(0, "Running tests with database %r" % settings.DATABASE_ENGINE)

        # If we're using SQLite, it's more convenient to test against an
        # in-memory database.
        if settings.DATABASE_ENGINE == "sqlite3":
            global TEST_DATABASE_NAME
            TEST_DATABASE_NAME = ":memory:"
        else:
            # Create the test database and connect to it. We need autocommit()
            # because PostgreSQL doesn't allow CREATE DATABASE statements
            # within transactions.
            cursor = db.cursor()
            try:
                db.connection.autocommit(1)
            except AttributeError:
                pass
            self.output(1, "Creating test database")
            try:
                cursor.execute("CREATE DATABASE %s" % TEST_DATABASE_NAME)
            except:
                confirm = raw_input("The test database, %s, already exists. Type 'yes' to delete it, or 'no' to cancel: " % TEST_DATABASE_NAME)
                if confirm == 'yes':
                    cursor.execute("DROP DATABASE %s" % TEST_DATABASE_NAME)
                    cursor.execute("CREATE DATABASE %s" % TEST_DATABASE_NAME)
                else:
                    print "Tests cancelled."
                    return
        db.close()
        old_database_name = settings.DATABASE_NAME
        settings.DATABASE_NAME = TEST_DATABASE_NAME

        # Initialize the test database.
        cursor = db.cursor()
        self.output(1, "Initializing test database")
        management.init()

        # Run the tests for each test model.
        self.output(1, "Running app tests")
        for model_name in test_models:
            self.output(1, "%s model: Importing" % model_name)
            try:
                mod = meta.get_app(model_name)
            except Exception, e:
                log_error(model_name, "Error while importing", ''.join(traceback.format_exception(*sys.exc_info())[1:]))
                continue
            self.output(1, "%s model: Installing" % model_name)
            management.install(mod)

            # Run the API tests.
            p = doctest.DocTestParser()
            test_namespace = dict([(m._meta.module_name, getattr(mod, m._meta.module_name)) for m in mod._MODELS])
            dtest = p.get_doctest(mod.API_TESTS, test_namespace, model_name, None, None)
            # Manually set verbose=False, because "-v" command-line parameter
            # has side effects on doctest TestRunner class.
            runner = DjangoDoctestRunner(verbosity_level=verbosity_level, verbose=False)
            self.output(1, "%s model: Running tests" % model_name)
            try:
                runner.run(dtest, clear_globs=True, out=sys.stdout.write)
            finally:
                # Rollback, in case of database errors. Otherwise they'd have
                # side effects on other tests.
                db.rollback()
Example #12
0
                if hasattr(mod, "run_tests") and callable(mod.run_tests):
                    self.output(1, "%s module: running tests" % module)
                    try:
                        mod.run_tests(verbosity_level)
                    except Exception, e:
                        log_error(module, "Exception running tests", ''.join(traceback.format_exception(*sys.exc_info())[1:]))
                        continue

        # Unless we're using SQLite, remove the test database to clean up after
        # ourselves. Connect to the previous database (not the test database)
        # to do so, because it's not allowed to delete a database while being
        # connected to it.
        if settings.DATABASE_ENGINE != "sqlite3":
            db.close()
            settings.DATABASE_NAME = old_database_name
            cursor = db.cursor()
            self.output(1, "Deleting test database")
            try:
                db.connection.autocommit(1)
            except AttributeError:
                pass
            else:
                time.sleep(1) # To avoid "database is being accessed by other users" errors.
            cursor.execute("DROP DATABASE %s" % TEST_DATABASE_NAME)

        # Display output.
        if error_list:
            for d in error_list:
                print
                print d['title']
                print "=" * len(d['title'])
Example #13
0
def clean_formkeys():
	cursor = db.cursor()
	ret = cursor.execute("DELETE FROM formkeys_formkeys WHERE timestamp + %s < now()", [DELAY])
	cursor.close()
	return ret