-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
91d8654
commit 09fb478
Showing
2 changed files
with
54 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |