parent
8d87945107
commit
d63f2896ee
@ -0,0 +1 @@
|
||||
"""Karma dives appreciation/etc type points to any text"""
|
@ -14,12 +14,10 @@ log = logging.getLogger('karma.ircplugin')
|
||||
|
||||
|
||||
class Karma(Plugin):
|
||||
|
||||
"""Track karma and report on it."""
|
||||
|
||||
def start(self):
|
||||
"""Set up the handlers."""
|
||||
|
||||
self.connection.add_global_handler('pubmsg', self.handle_chatter, -20)
|
||||
self.connection.add_global_handler('privmsg', self.handle_chatter, -20)
|
||||
|
||||
@ -38,7 +36,6 @@ class Karma(Plugin):
|
||||
|
||||
def stop(self):
|
||||
"""Tear down handlers."""
|
||||
|
||||
self.connection.remove_global_handler('pubmsg', self.handle_chatter)
|
||||
self.connection.remove_global_handler('privmsg', self.handle_chatter)
|
||||
|
||||
@ -51,7 +48,6 @@ class Karma(Plugin):
|
||||
@staticmethod
|
||||
def handle_chatter(connection, event):
|
||||
"""Watch karma from IRC chatter."""
|
||||
|
||||
what = event.arguments[0].lower()
|
||||
karma_pattern = r'(?:\((.+?)\)|(\S+))(\+\+|--|\+-|-\+)(\s+|$)'
|
||||
|
||||
@ -82,7 +78,6 @@ class Karma(Plugin):
|
||||
|
||||
def handle_rank(self, connection, event, match):
|
||||
"""Report on the rank of a karma item."""
|
||||
|
||||
where = event.target
|
||||
if where in settings.KARMA_IGNORE_COMMAND_TARGETS:
|
||||
log.debug("ignoring command in {0:s}".format(where))
|
||||
@ -99,7 +94,6 @@ class Karma(Plugin):
|
||||
|
||||
def handle_report(self, connection, event, match):
|
||||
"""Provide some karma reports."""
|
||||
|
||||
where = event.target
|
||||
if where in settings.KARMA_IGNORE_COMMAND_TARGETS:
|
||||
log.debug("ignoring command in {0:s}".format(where))
|
||||
@ -135,7 +129,6 @@ class Karma(Plugin):
|
||||
|
||||
def handle_stats(self, connection, event, match):
|
||||
"""Provide stats on a karma user."""
|
||||
|
||||
where = event.target
|
||||
if where in settings.KARMA_IGNORE_COMMAND_TARGETS:
|
||||
log.debug("ignoring command in {0:s}".format(where))
|
||||
|
@ -14,6 +14,7 @@ log = logging.getLogger('karma.models')
|
||||
|
||||
|
||||
def perdelta(start, end, delta):
|
||||
"""Return karma between two dates."""
|
||||
curr = start
|
||||
while curr < end:
|
||||
yield curr
|
||||
@ -22,20 +23,20 @@ def perdelta(start, end, delta):
|
||||
|
||||
|
||||
class KarmaKeyManager(models.Manager):
|
||||
|
||||
"""Manage handy queries for KarmaKey."""
|
||||
|
||||
def ranked_scored_order(self):
|
||||
"""Provide karma keys in rank order, with numbered position."""
|
||||
keys = self.annotate(karma_score=models.Sum('karmalogentry__delta')).order_by('-karma_score')
|
||||
return keys
|
||||
|
||||
def ranked_scored_reverse_order(self):
|
||||
"""Provide karma keys in reverse rank order, with numbered position."""
|
||||
keys = self.annotate(karma_score=models.Sum('karmalogentry__delta')).order_by('karma_score')
|
||||
return keys
|
||||
|
||||
|
||||
class KarmaKey(models.Model):
|
||||
|
||||
"""Track a thing being karmaed."""
|
||||
|
||||
key = models.CharField(max_length=200, unique=True)
|
||||
@ -44,26 +45,22 @@ class KarmaKey(models.Model):
|
||||
|
||||
def __str__(self):
|
||||
"""String representation."""
|
||||
|
||||
return "{0:s} ({1:d})".format(self.key, self.score())
|
||||
|
||||
def score(self):
|
||||
"""Determine the score for this karma entry."""
|
||||
|
||||
return KarmaLogEntry.objects.filter(key=self).aggregate(models.Sum('delta'))['delta__sum']
|
||||
|
||||
def rank(self):
|
||||
"""Determine the rank of this karma entry relative to the others."""
|
||||
|
||||
sorted_keys = KarmaKey.objects.ranked_scored_order()
|
||||
for rank, key in enumerate(sorted_keys):
|
||||
if key == self:
|
||||
return rank+1
|
||||
return rank + 1
|
||||
return None
|
||||
|
||||
def history(self, mode='delta'):
|
||||
"""Determine the score for this karma entry at every delta."""
|
||||
|
||||
history = []
|
||||
|
||||
if mode == 'delta':
|
||||
@ -81,7 +78,7 @@ class KarmaKey(models.Model):
|
||||
slice_end = timezone.now().date()
|
||||
for timeslice in perdelta(slice_begin, slice_end, timedelta(days=1)):
|
||||
score = KarmaLogEntry.objects.filter(key=self).filter(
|
||||
created__lte=timeslice+timedelta(days=1)).aggregate(models.Sum('delta'))['delta__sum']
|
||||
created__lte=timeslice + timedelta(days=1)).aggregate(models.Sum('delta'))['delta__sum']
|
||||
if not score:
|
||||
score = 0
|
||||
try:
|
||||
@ -97,24 +94,25 @@ class KarmaKey(models.Model):
|
||||
|
||||
|
||||
class KarmaLogEntryManager(models.Manager):
|
||||
|
||||
"""Manage handy queries for KarmaLogEntry."""
|
||||
|
||||
def optimists(self):
|
||||
"""Return karma users who gave the most positive karma."""
|
||||
karmaers = self.values('nickmask').annotate(karma_outlook=models.Sum('delta')).order_by('-karma_outlook')
|
||||
return karmaers
|
||||
|
||||
def pessimists(self):
|
||||
"""Return karma users who gave the most negative karma."""
|
||||
karmaers = self.values('nickmask').annotate(karma_outlook=models.Sum('delta')).order_by('karma_outlook')
|
||||
return karmaers
|
||||
|
||||
def most_opinionated(self):
|
||||
"""Return karma users who gave the most karma."""
|
||||
karmaers = self.values('nickmask').annotate(karma_count=models.Count('delta')).order_by('-karma_count')
|
||||
return karmaers
|
||||
|
||||
|
||||
class KarmaLogEntry(models.Model):
|
||||
|
||||
"""Track each karma increment/decrement."""
|
||||
|
||||
key = models.ForeignKey('KarmaKey')
|
||||
@ -125,11 +123,12 @@ class KarmaLogEntry(models.Model):
|
||||
objects = KarmaLogEntryManager()
|
||||
|
||||
class Meta:
|
||||
"""Meta options."""
|
||||
|
||||
verbose_name_plural = 'karma log entries'
|
||||
|
||||
def __str__(self):
|
||||
"""String representation."""
|
||||
|
||||
tz = pytz.timezone(settings.TIME_ZONE)
|
||||
return "{0:s}{1:s} @ {2:s} by {3:s}".format(self.key.key, '++' if self.delta > 0 else '--',
|
||||
self.created.astimezone(tz).strftime('%Y-%m-%d %H:%M:%S %Z'),
|
||||
|
@ -6,7 +6,10 @@ from karma.models import KarmaKey
|
||||
|
||||
|
||||
class KarmaKeySerializer(serializers.ModelSerializer):
|
||||
"""Serializer for the REST API."""
|
||||
|
||||
class Meta:
|
||||
"""Meta options."""
|
||||
|
||||
model = KarmaKey
|
||||
fields = ('id', 'key', 'score', 'rank')
|
||||
|
@ -13,7 +13,6 @@ log = logging.getLogger('karma.views')
|
||||
|
||||
def index(request):
|
||||
"""Display all karma keys."""
|
||||
|
||||
entries = KarmaKey.objects.all().order_by('key')
|
||||
|
||||
return render(request, 'karma/index.html', {'entries': entries})
|
||||
@ -21,14 +20,12 @@ def index(request):
|
||||
|
||||
def key_detail(request, karma_key):
|
||||
"""Display the requested karma key."""
|
||||
|
||||
entry = get_object_or_404(KarmaKey, key=karma_key.lower())
|
||||
|
||||
return render(request, 'karma/karma_key.html', {'entry': entry, 'entry_history': entry.history(mode='date')})
|
||||
|
||||
|
||||
class KarmaKeyViewSet(viewsets.ReadOnlyModelViewSet):
|
||||
|
||||
"""Provide list and detail actions for karma keys."""
|
||||
|
||||
queryset = KarmaKey.objects.all()
|
||||
|
Loading…
Reference in New Issue
Block a user