add working karma module to django bot site

This commit is contained in:
Mike Bloy 2011-01-09 14:11:24 -06:00
parent 154eb9753f
commit 69aa4016bc
8 changed files with 62 additions and 4 deletions

View File

@ -8,6 +8,10 @@ class LogEntry(models.Model):
userhost = models.CharField(max_length=512)
timestamp = models.DateTimeField(db_column='karmatime')
def __unicode__(self):
return "%s given %s by %s on %s" % (self.key, self.delta,
self.who, self.timestamp)
class Meta:
db_table = 'karma_log'
ordering = ['timestamp']
@ -18,6 +22,13 @@ class User(models.Model):
pos = models.IntegerField()
neg = models.IntegerField()
def _get_total(self):
return self.pos + self.neg
total = property(_get_total)
def __unicode__(self):
return "Karma User %s (%s/%s)" % (self.who, self,pos, self.neg)
class Meta:
db_table = 'karma_users'
ordering = ['who']
@ -27,6 +38,9 @@ class Value(models.Model):
key = models.CharField(max_length=255, primary_key=True)
value = models.IntegerField()
def __unicode__(self):
return "Karma Value %s (%s)" % (self.key, self.value)
class Meta:
db_table = 'karma_values'
ordering = ['value']

7
botweb/karma/urls.py Executable file
View File

@ -0,0 +1,7 @@
from django.conf.urls.defaults import *
urlpatterns = patterns('karma.views',
(r'^$', 'index'),
(r'^givers/$', 'givers'),
(r'^stats/$', 'stats'),
)

View File

@ -1 +1,16 @@
# Create your views here.
from django.shortcuts import render_to_response
from karma.models import Value, User
def index(request):
karma_values = len(Value.objects.all())
karma_users = len(User.objects.all())
return render_to_response('karma/index.html', {'value_count': karma_values,
'user_count': karma_users})
def stats(request):
values = Value.objects.all().order_by('-value')
return render_to_response('karma/stats.html', {'values': values})
def givers(request):
users = User.objects.all().order_by('who')
return render_to_response('karma/givers.html', {'users': users})

View File

@ -83,10 +83,15 @@ MIDDLEWARE_CLASSES = (
ROOT_URLCONF = 'botweb.urls'
TEMPLATE_BASE_LOC = os.path.normpath(
os.path.join(os.path.abspath(os.path.dirname(__file__)),
'templates'))
TEMPLATE_DIRS = (
# Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
TEMPLATE_BASE_LOC,
)
INSTALLED_APPS = (
@ -104,3 +109,4 @@ INSTALLED_APPS = (
if __name__ == "__main__":
print DB_LOCATION
print TEMPLATE_DIRS

View File

@ -0,0 +1,7 @@
<ul>
{% for user in users %}
<li>
{{ user.who }} - {{ user.pos }} positive, {{ user.neg }} negative, {{ user.total }} total
</li>
{% endfor %}
</ul>

View File

@ -0,0 +1,4 @@
<ul>
<li><a href="/karma/stats/">Karma Stats</a> ({{ value_count }} karma entries)</li>
<li><a href="/karma/givers/">Karma Giver Stats</a> ({{ user_count }} karma givers)</li>
</ul>

View File

@ -0,0 +1,7 @@
<ol>
{% for value in values %}
<li>
{{ value.key }} ({{ value.value }})
</li>
{% endfor %}
</ol>

View File

@ -6,9 +6,7 @@ from django.conf.urls.defaults import *
urlpatterns = patterns(
'',
(r'^karma/$', 'karma.views.index'),
(r'^karma/givers/$', 'karma.views.givers'),
(r'^karma/stats/$', 'karma.views.stats'),
(r'^karma/', include('karma.urls')),
# Example:
# (r'^botweb/', include('botweb.foo.urls')),