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