Skip to content

Commit

Permalink
Added badges commands
Browse files Browse the repository at this point in the history
  • Loading branch information
noirbizarre committed Jul 7, 2015
1 parent 91d8654 commit 09fb478
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
1 change: 1 addition & 0 deletions udata/commands/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ def register_commands(manager):
import udata.core.search.commands
import udata.core.spatial.commands
import udata.core.jobs.commands
import udata.core.badges.commands
import udata.api.commands

# Dynamic module commands loading
Expand Down
53 changes: 53 additions & 0 deletions udata/core/badges/commands.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

import logging

from os.path import exists

from udata.commands import submanager
from udata.models import Organization, OrganizationBadge

log = logging.getLogger(__name__)


m = submanager('badges',
help='Badges related operations',
description='Handle all badges related operations and maintenance'
)


def toggle_badge(id_or_slug, badge_kind):
organization = Organization.objects.get_by_id_or_slug(id_or_slug)
if not organization:
log.error('No organization found for %s', id_or_slug)
return
log.info('Toggling badge {kind} for organization {org}'.format(
kind=badge_kind,
org=organization.name
))
existed = False
for badge in organization.badges:
if badge.kind == badge_kind:
organization.badges.remove(badge)
existed = True
break
if not existed:
badge = OrganizationBadge(kind=badge_kind)
organization.badges.append(badge)
organization.save()


@m.command
def toggle(path_or_id, badge_kind):
'''Toggle a `badge_kind` for a given `path_or_id`
The `path_or_id` is either an id, a slug or a file containing a list
of ids or slugs.
'''
if exists(path_or_id):
with open(path_or_id) as open_file:
for id_or_slug in open_file.readlines():
toggle_badge(id_or_slug.strip(), badge_kind)
else:
toggle_badge(path_or_id, badge_kind)

0 comments on commit 09fb478

Please sign in to comment.