Commit 47edbab7 authored by Elger Jonker's avatar Elger Jonker

checking and adding urls in bulk using the command line

Former-commit-id: e47b8c7b
parent f8728a39
import logging
from datetime import datetime
import pytz
from import BaseCommand
from failmap.organizations.models import Organization, Url
log = logging.getLogger(__package__)
class Command(BaseCommand):
Allows you to check the existence of urls and allows you to add them to several organizations by name.
Asks to add the urls to organizations.
def add_arguments(self, parser):
"""Add arguments."""
parser.add_argument('-u', '--urls', type=str, nargs='+', help='space separated urls')
super(Command, self).add_arguments(parser)
def handle(self, *args, **options):
non_existing = get_non_existing(options['urls'])
if non_existing:"Found new urls, to what organizations do you want to add these?")"The new urls are: %s" % " ".join(non_existing))
organizations = inquire_organization()
for url in non_existing:
new_url = Url()
new_url.url = url
new_url.created_on =
for organization in organizations:
new_url.organization.add(organization)"Added %s to %s" % (url, organization))
def get_non_existing(urls):
not_exists = []
for url in urls:
exists = Url.objects.all().filter(url=url).exists()
if exists:
log.debug("Exists: %s" % url)
log.debug("Does NOT exist: %s" % url)
if not_exists:
log.debug("These urls do not exist:")
log.debug(" ".join(not_exists))
return not_exists
def inquire_organization():
Get one or more organizations to add urls to.
organizations = []
while True:
if organizations:"Currently adding to: %s" % organizations)
possible_organization = input("Type the name of an organization, type - to be done: ")
if possible_organization in ["-", ""]:
# does not take into account country or organization type, so it's not perfect.
real_organization = Organization.objects.all().filter(name__iexact=possible_organization, is_dead=False).first()
# don't add None search result.
if real_organization:
return organizations
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment