umap/umap/management/commands/anonymous_edit_url.py

38 lines
1 KiB
Python
Raw Normal View History

2018-05-18 14:40:38 -05:00
import sys
from django.conf import settings
2018-05-18 14:40:38 -05:00
from django.core.management.base import BaseCommand
from django.utils import translation
2018-05-18 14:40:38 -05:00
from umap.models import Map
class Command(BaseCommand):
help = (
"Retrieve anonymous edit url of a map. "
"Eg.: python manage.py anonymous_edit_url 1234"
)
2018-05-18 14:40:38 -05:00
def add_arguments(self, parser):
parser.add_argument("pk", help="PK of the map to retrieve.")
parser.add_argument(
"--lang",
help="Language code to use in the URL.",
default=settings.LANGUAGE_CODE,
)
2018-05-18 14:40:38 -05:00
def abort(self, msg):
self.stderr.write(msg)
sys.exit(1)
def handle(self, *args, **options):
translation.activate(options["lang"])
pk = options["pk"]
2018-05-18 14:40:38 -05:00
try:
map_ = Map.objects.get(pk=pk)
except Map.DoesNotExist:
self.abort("Map with pk {} not found".format(pk))
2018-05-18 14:40:38 -05:00
if map_.owner:
self.abort("Map is not anonymous (owner: {})".format(map_.owner))
print(map_.get_anonymous_edit_url())