Package: django-badgify
This Django application will help you to create your own badge system on your website.It has been used onUluleto create our ownbadge mechanism.Installation$pipinstalldjango-badgifyUsageAddbadgifyto yourINSTALLED_APPSinsettings.py:INSTALLED_APPS=(# ...'badgify',)Synchronize the database:$pythonmanage.pymigratebadgifyCreate abadgify_recipes.pyfile in your Django application:$cdpath/to/your/django/app$touchbadgify_recipes.pyOpen this file and importbadgify.recipe.BaseRecipeclass andbadgifymodule:frombadgify.recipeimportBaseRecipeimportbadgifyCreate and register your recipe classes:classPythonLoverRecipe(BaseRecipe):passclassJSLoverRecipe(BaseRecipe):pass# Per classbadgify.register(PythonLoverRecipe)badgify.register(JSLoverRecipe)# All at once in a listbadgify.register([PythonLoverRecipe,JSLoverRecipe])A recipe class must implement:nameclass attributeThe badge name (humanized).imagepropertyThe badge image/logo as a file object.A recipe class may implement:slugclass attributeThe badge slug (used internally and in URLs).
If not provided, it will be auto-generated based on the badge name.descriptionclass attributeThe badge description (short).
It not provided, value will be blank.user_idspropertyQuerySetreturning User IDs likely to be awarded. You must return aQuerySetand not just a Python list or tuple. You can usevalues_list('id',flat=True).db_readclass attributeThe database alias on which to perform read queries.
Defaults todjango.db.DEFAULT_DB_ALIAS.batch_sizeclass attributeHow manyAwardobjects to create at once.
Defaults toBADGIFY_BATCH_SIZE(500).Example:fromdjango.contrib.staticfiles.storageimportstaticfiles_storagefrombadgify.recipeimportBaseRecipeimportbadgifyfrom.modelsimportMyCustomUserclassPythonLoverRecipe(BaseRecipe):"""
    People loving Python.
    """name='Python Lover'slug='python-lover'description='People loving Python programming language'@propertydefimage(self):returnstaticfiles_storage.open('python-lover.png')@propertydefuser_ids(self):return(MyCustomUser.objects.filter(love_python=True).values_list('id',flat=True))classJSLoverRecipe(BaseRecipe):"""
    People loving JS.
    """name='JS Lover'slug='js-lover'description='People loving JS programming language'@propertydefimage(self):returnstaticfiles_storage.open('js-lover.png')@propertydefuser_ids(self):return(MyCustomUser.objects.filter(love_js=True).values_list('id',flat=True))classJavaLoverRecipe(BaseRecipe):"""
    People loving Java.
    """name='Java Lover'slug='java-lover'description='People loving Java programming language'@propertydefimage(self):returnstaticfiles_storage.open('java-lover.png')badgify.register([PythonLoverRecipe,JSLoverRecipe,JavaLoverRecipe,])Once you have implemented and registered your recipe classes, you can invoke
available commands bellow:# Create badges from recipes$pythonmanage.pybadgify_syncbadges# Update badges from recipes$pythonmanage.pybadgify_syncbadges--update# Create awards$pythonmanage.pybadgify_syncawards# Create awards bypassing signals (improve performances)$pythonmanage.pybadgify_syncawards--disable-signals# Only create awards for "python" badge$pythonmanage.pybadgify_syncawards--badgespython# Only create awards for "python" and "go" badges$pythonmanage.pybadgify_syncawards--badges"python go"# Create awards for all badges, except "php"$pythonmanage.pybadgify_syncawards--exclude-badgesphp# Create awards for all badges, except "php" and "java"$pythonmanage.pybadgify_syncawards--exclude-badges"php java"# Denormalize Badge.users.count() into Badge.users_count field$pythonmanage.pybadgify_synccounts# Only denormalize counts for "python" badge$pythonmanage.pybadgify_synccounts--badgespython# Denormalize counts for all badges, except "php"$pythonmanage.pybadgify_synccounts--exclude-badgesphp# Denormalize counts for all badges, except "php" and "java"$pythonmanage.pybadgify_synccounts--exclude-badges"php java"# Typical workflow for best performances$pythonmanage.pybadgify_syncbadges$pythonmanage.pybadgify_syncawards--disable-signals$pythonmanage.pybadgify_synccounts# WARNING: if you delete awards to start again with a fresh table
# don't forget to update Badge.users_count field. Or use this command:$pythonmanage.pybadgify_reset# Typical workflow for best performances if you want to recompute awards$pythonmanage.pybadgify_reset$pythonmanage.pybadgify_syncawards--disable-signals$pythonmanage.pybadgify_synccountsTemplatetagsbadgify_badgesTakes two optional arguments:user: aUserobjectusername: aUserusernameWithout any argument, displays all badges. Otherwise, badges awarded by the given user.{%loadbadgify_tags%}{%badgify_badgesasbadges%}{%badgify_badgesusername="johndoe"asbadges%}{%badgify_badgesuser=userasbadges%}{%forbadgeinbadges%}{{badge.name}}{%endfor%}Viewsdjango-badgifyprovides two views:badgify.views.BadgifyListView: displays all badges as paginated listbadgify.views.BadgifyDetailView: displays awarded users as paginated list for a given badgeThis application does not include templates. It lets you implement
templates as you like (see example project).To include these two views, include the providedbadgify.urls:# -*- coding: utf-8 -*-fromdjango.conf.urlsimportinclude,urlurlpatterns=[# Your other includesurl(r'^badges/',include('badgify.urls')),]See example project for more details.Custom Modelsdjango-badgifylets you define your own model classes forBadgeandAwardmodels. That can be pretty useful for i18n stuff
(example:django-transmettasupport),
adding custom fields, methods or properties.Your models must inherit frombadgify.models.basemodel classes:# yourapp.modelsfrombadgify.modelsimportbaseclassBadge(base.Badge):# you own fields / logic hereclassMeta(base.Badge.Meta):abstract=FalseclassAward(base.Award):# you own fields / logic hereclassMeta(base.Award.Meta):abstract=FalseThen tell the application to use them in place of default ones in yoursettings.pymodule:# yourapp.settingsBADGIFY_BADGE_MODEL='yourapp.models.Badge'BADGIFY_AWARD_MODEL='yourapp.models.Award'SettingsYou can altere the application behavior by defining settings in yoursettings.pymodule.All application settings are prefixed withBADGIFY_.BADGIFY_BADGE_IMAGE_UPLOAD_ROOTThe root path forBadgemodelImageField.BADGIFY_BADGE_IMAGE_UPLOAD_URLThe URLBadgemodelImageField.BADGIFY_BADGE_IMAGE_UPLOAD_STORAGEYour owndjango.core.files.storagestorage instance.BADGIFY_BADGE_LIST_VIEW_PAGINATE_BYNumber of badges to display on the badge list page.BADGIFY_BADGE_DETAIL_VIEW_PAGINATE_BYNumber of awarded users to display on the badge detail page.BADGIFY_BADGE_MODELYour own concreteBadgemodel class as module path.Example:yourapp.models.Badge.BADGIFY_AWARD_MODELYour own concreteAwardmodel class as module path.Example:yourapp.models.Award.BADGIFY_BATCH_SIZEMaximum number ofAwardobjects to create at once.Defaults to500.Contribute# Don't have pip?$sudoeasy_installpip# Don't already have virtualenv?$sudopipinstallvirtualenv# Clone and install dependencies$gitclonehttps://github.com/ulule/django-badgify.git$cddjango-badgify$makeinstall# Launch tests$maketest# Launch example project$makecreate_fixtures$makeserve