Skip to content

Instantly share code, notes, and snippets.

@toolness
Created September 26, 2012 13:38
Show Gist options
  • Select an option

  • Save toolness/3788091 to your computer and use it in GitHub Desktop.

Select an option

Save toolness/3788091 to your computer and use it in GitHub Desktop.
Attempt at a Django-style model representation of what's needed for Thimble-badges integration.
from django.db import models
from django.contrib.auth.models import User
class Behavior(models.Model):
"""
Represents a type of user behavior, such as logging in, publishing, or
creating an HTML tag.
"""
short_name = models.CharField(max_length=50, unique=True)
name = models.CharField(max_length=100)
description = models.TextField(max_length=10000)
credit_limit = models.IntegerField(
help_text='Number of seconds that must pass between incrementing '
'the score for this behavior.'
)
class BadgeClass(models.Model):
"""
Represents a type of badge and the criteria for its awarding.
"""
short_name = models.CharField(max_length=50, unique=True)
name = models.CharField(max_length=200)
description = models.TextField(
max_length=10000,
help_text='HTML is allowed.'
)
# In the future, we can potentially have a badge be awarded on
# the basis of multiple behavior criteria, but for now we will
# only support one.
behavior = models.ForeignKey(Behavior)
behavior_score = models.IntegerField()
class UserProfile(models.Model):
"""
Additional information to store about users.
For more information, see:
https://docs.djangoproject.com/en/1.4/topics/auth/#storing-additional-information-about-users
"""
user = models.OneToOneField(User)
badges = models.ManyToManyField(BadgeClass, through='BadgeInstance')
behaviors = models.ManyToManyField(Behavior, through='BehaviorScore')
class BehaviorScore(models.Model):
"""
A user's score for a particular behavior (i.e., how many times the
user has been credited with the behavior).
"""
user = models.ForeignKey(UserProfile)
behavior = models.ForeignKey(Behavior)
score = models.IntegerField()
class BadgeInstance(models.Model):
"""
A specific issuance of a badge type to a user.
"""
user = models.ForeignKey(UserProfile)
badge_class = models.ForeignKey(BadgeClass)
is_read = models.BooleanField(
help_text='Whether the user has seen their badge yet.'
)
issue_date = models.DateTimeField()
@toolness
Copy link
Author

This is an attempt at what a "real" model for the backend behind the Thimble badges spike might look like. The current "fake" backend, gapingbadger, is mostly just blob storage.

@toolness
Copy link
Author

This is an attempt at what a "real" model for the backend behind the Thimble badges spike might look like. The current "fake" backend, gapingbadger, is mostly just blob storage.

@toolness
Copy link
Author

This is an attempt at what a "real" model for the backend behind the Thimble badges spike might look like. The current "fake" backend, gapingbadger, is mostly just blob storage.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment