def prompt_and_cleanup(): are_you_sure("Enter any key to finalize cleanup. This should be done after the router has processed all messages") migration_connections = Connection.objects.filter(backend__name="migration") push_backend = Backend.objects.get_or_create(name="push_backend")[0] for conn in migration_connections: conn.backend = push_backend conn.save() print "moved %s migration connections to push backend" % migration_connections.count()
def handle(self, *args, **options): if not are_you_sure(WARNING_TEXT): print "Okay. Good call -- safety first! Goodbye." sys.exit() else: all_patients = get_db().view("patient/ids").all() successfully_processed = [] failed = [] NOTIFICATION_INCREMENT = 10 count = 0 total_pats = len(all_patients) for result in all_patients: if count % NOTIFICATION_INCREMENT == 0: print "processing patient %s of %s" % (count, total_pats) pat_id = result["key"] count = count + 1 if reprocess(pat_id): successfully_processed.append(pat_id) else: failed.append(pat_id) if failed: print "======= failed patients ========\n" + "\n".join(failed) print "Unable to process the above patients. Lookup these " \ "patient ids by visiting the urls like so: %s" % (reverse("single_patient", args=(failed[0],))) print "Successfully processed %s patients. There were problems with %s patients." % \ (len(successfully_processed), len(failed))
def clear_migration_backends(): migration_connections = Connection.objects.filter(backend__name="migration") if migration_connections.count(): if are_you_sure(("Do you really want to delete %s migration connections? " "This may have unintended consequences.") %migration_connections.count()): migration_connections.all().delete() else: print "You have to clear migration connections before starting." sys.exit()
def handle(self, *args, **options): should_proceed = are_you_sure(WARNING) if options["interactive"] else True if not should_proceed: print "Ok, aborting" sys.exit() db = get_db() for doc in get_design_docs(db): logging.debug("compacting views for app: %s" % doc.name) print("compacting views for app: %s" % doc.name) db.compact(doc.name)
def handle(self, *args, **options): if len(args) > 1: raise CommandError('Usage: manage.py delete_db [database]') if len(args) == 1: dbname = args[0] else: dbname = get_db().dbname should_proceed = are_you_sure(WARNING % dbname) if options["interactive"] else True if not should_proceed: print "Ok, aborting" sys.exit() server = get_db().server try: server.delete_db(dbname) print "deleted %s" % dbname except ResourceNotFound: print "database %s not found. nothing done." % dbname
def handle(self, *args, **options): if len(args) != 1: raise CommandError('Usage: manage.py backup_db <backup>') source = get_db() server = source.server backupname = args[0] if backupname in server: should_proceed = are_you_sure(WARNING % backupname) if options["interactive"] else True if not should_proceed: print "Ok, aborting" sys.exit() server.delete_db(backupname) print "deleted %s" % backupname print "backing up %s to %s" % (source.dbname, backupname) server.create_db(backupname) server.replicate(source.dbname, backupname) print "successfully backed up %s to %s" % (source.dbname, backupname) backup = server.get_or_create_db(backupname) print "source: %s\n\nbackup: %s" % (source.info(), backup.info())
def delete_permitted(self): return not self.test_run and are_you_sure()
class Command(BaseCommand): help = "Import messages from a csv dump of Push gateway's inbound message log" def handle(self, *args, **options): def clear_migration_backends(): migration_connections = Connection.objects.filter(backend__name="migration") if migration_connections.count(): if are_you_sure(("Do you really want to delete %s migration connections? " "This may have unintended consequences.") %migration_connections.count()): migration_connections.all().delete() else: print "You have to clear migration connections before starting." sys.exit() def load_push_messages(message_file): print "loading messages from %s" % message_file print "started" migration_backend = Backend.objects.get_or_create(name="migration")[0] with open(message_file, 'r') as f: reader = unicode_csv_reader(f, delimiter=',', quotechar='"') inbound_count = -1 max = 9999999999 for row in reader: inbound_count = inbound_count + 1 if inbound_count == 0: continue # ignore the first line of headers msg, number, timestamp = row # if we already have a registered contact here # then make sure we associate them with the migration # backend before pumping the message through try: push_conn = Connection.objects.get(backend__name="push_backend", identity=number) push_conn.backend = migration_backend push_conn.save() except Connection.DoesNotExist: # maybe they're newly created, no problem pass parsed_date = datetime.strptime(timestamp, "%d/%m/%Y %H:%M:%S") try: utils.send_test_message(identity=number, text=msg, timestamp=str(parsed_date)) except RouterError, e: print e.code print e.content_type print e.response raise if inbound_count % 100 == 0: print "processed %s inbound messages." % (inbound_count) if inbound_count >= max: break print "Complete. Processed %s incoming messages" % (inbound_count) def prompt_and_cleanup(): are_you_sure("Enter any key to finalize cleanup. This should be done after the router has processed all messages") migration_connections = Connection.objects.filter(backend__name="migration") push_backend = Backend.objects.get_or_create(name="push_backend")[0] for conn in migration_connections: conn.backend = push_backend conn.save() print "moved %s migration connections to push backend" % migration_connections.count() print "checking router.." if not check_router(): sys.exit() print "router running." if True or are_you_sure("really run the import? (yes or no): "): print "Starting migration!" datapath = os.path.join(os.path.dirname\ (os.path.dirname\ (os.path.dirname\ (os.path.dirname\ (os.path.dirname\ (os.path.abspath(os.path.dirname(__file__))))))), "static", "tanzania", "push") message_file = os.path.join(datapath, "2011-09-09-PUSH-Messages.csv") clear_migration_backends() load_push_messages(message_file) prompt_and_cleanup() else: print "Migration canceled."
print "contact %s has %s as default instead of push" % contact.default_connection failed += 1 if contact.connection_set.count() > 1: print "found multiple connections" print "checked %s contacts" % real_contacts.count() if failed: print "Check FAILED for %s contacts. See output for more details" else: print "Check PASSED" print "checking router.." if not check_router(): sys.exit() print "router running." if True or are_you_sure("really run the migration? (yes or no): "): cleanup() print "Starting migration!" datapath = os.path.join(os.path.dirname\ (os.path.dirname\ (os.path.dirname\ (os.path.dirname\ (os.path.dirname\ (os.path.abspath(os.path.dirname(__file__))))))), "static", "tanzania", "migration") contact_file = os.path.join(datapath, "contacts.csv") load_contacts(contact_file) messages_file = os.path.join(datapath, "messages.csv") load_messages(messages_file) print "cleaning up"