def get_all_latest_builds_for_user(user): app_ids = [(domain, app_id) for domain in user.domains for app_id in get_app_ids_in_domain(domain)] app_docs = [ get_latest_released_app_doc(app_id[0], app_id[1]) for app_id in app_ids ] return [wrap_app(app_doc) for app_doc in app_docs if app_doc is not None]
def handle(self, **options): domains = [] app_ids = [] self.force = options["force"] self.migrate_usercase = options["usercase"] for ident in options["app_id_or_domain"]: if not self.migrate_usercase: try: Application.get(ident) app_ids.append(ident) continue except ResourceNotFound: pass ids = get_app_ids_in_domain(ident) app_ids.extend(ids) if ids: domains.append(ident) logger.info('migrating {} apps in domain {}'.format(len(ids), ident)) for app_id in app_ids: logger.info('migrating app {}'.format(app_id)) self.migrate_app(app_id) if self.migrate_usercase: for domain in domains: USER_PROPERTY_EASY_REFS.set(domain, True, NAMESPACE_DOMAIN) logger.info("enabled USER_PROPERTY_EASY_REFS for domains: %s", ", ".join(domains)) logger.info('done with migrate_app_to_cmitfb')
def get(self, request, domain): app_access = ApplicationAccess.get_by_domain(domain) app_ids = get_app_ids_in_domain(domain) apps = map( lambda app_id: self.fetch_app(domain, app_id), app_ids, ) apps = filter(None, apps) apps = filter(lambda app: app.get('cloudcare_enabled') or self.preview, apps) apps = filter(lambda app: app_access.user_can_access_app(request.couch_user, app), apps) apps = sorted(apps, key=lambda app: app['name']) def _default_lang(): try: return apps[0]['langs'][0] except Exception: return 'en' # default language to user's preference, followed by # first app's default, followed by english language = request.couch_user.language or _default_lang() context = { "domain": domain, "language": language, "apps": apps, "maps_api_key": settings.GMAPS_API_KEY, "username": request.couch_user.username, "formplayer_url": settings.FORMPLAYER_URL, "single_app_mode": False, "home_url": reverse(self.urlname, args=[domain]), "environment": WEB_APPS_ENVIRONMENT, } return render(request, "cloudcare/formplayer_home.html", context)
def get(self, request, domain): app_access = ApplicationAccess.get_by_domain(domain) app_ids = get_app_ids_in_domain(domain) apps = map( lambda app_id: self.fetch_app(domain, app_id), app_ids, ) apps = filter(None, apps) apps = filter(lambda app: app.get('cloudcare_enabled') or self.preview, apps) apps = filter(lambda app: app_access.user_can_access_app(request.couch_user, app), apps) apps = sorted(apps, key=lambda app: app['name']) def _default_lang(): try: return apps[0]['langs'][0] except Exception: return 'en' # default language to user's preference, followed by # first app's default, followed by english language = request.couch_user.language or _default_lang() context = { "domain": domain, "language": language, "apps": apps, "maps_api_key": settings.GMAPS_API_KEY, "username": request.user.username, "formplayer_url": settings.FORMPLAYER_URL, "single_app_mode": False, "home_url": reverse(self.urlname, args=[domain]), "environment": WEB_APPS_ENVIRONMENT, } return render(request, "cloudcare/formplayer_home.html", context)
def get_web_apps_available_to_user(self, domain, user): app_access = get_application_access_for_domain(domain) app_ids = get_app_ids_in_domain(domain) apps = list( map( lambda app_id: self.fetch_app(domain, app_id), app_ids, )) apps = filter(None, apps) apps = filter(lambda app: app.get('cloudcare_enabled') or self.preview, apps) apps = filter(lambda app: app_access.user_can_access_app(user, app), apps) role = None try: role = user.get_role(domain) except DomainMembershipError: # User has access via domain mirroring pass if role: apps = [ _format_app(app) for app in apps if role.permissions.view_web_app(app) ] apps = sorted(apps, key=lambda app: app['name']) return apps
def handle(self, **options): app_ids_by_domain = defaultdict(set) self.force = options["force"] self.dry = "DRY RUN " if options["dry_run"] else "" self.fup_caseref = options["fix_user_props_caseref"] self.fix_user_props = options["fix_user_properties"] or self.fup_caseref self.migrate_usercase = options["usercase"] for ident in options["app_id_or_domain"]: if not (self.migrate_usercase or self.fix_user_props): try: app = Application.get(ident) app_ids_by_domain[app.domain].add(ident) continue except ResourceNotFound: pass app_ids_by_domain[ident].update(get_app_ids_in_domain(ident)) for domain, app_ids in sorted(app_ids_by_domain.items()): logger.info('migrating %s: %s apps', domain, len(app_ids)) for app_id in app_ids: try: app = get_app(domain, app_id) if app.doc_type == "Application": if self.fix_user_props: self.fix_user_properties(app) else: self.migrate_app(app) else: logger.info("Skipping %s/%s because it is a %s", domain, app_id, app.doc_type) except Exception: logger.exception("skipping app %s/%s", domain, app_id) logger.info('done with migrate_app_to_cmitfb %s', self.dry)
def get_apps(domain): for app_id in get_app_ids_in_domain(domain): try: app = get_current_app(domain, app_id) except Exception: pass # It's not gonna load in app manager anyways if not app.is_remote_app(): yield app
def get_all_latest_builds_for_user(user): app_ids = [ (domain, app_id) for domain in user.domains for app_id in get_app_ids_in_domain(domain) ] app_docs = [get_latest_released_app_doc(app_id[0], app_id[1]) for app_id in app_ids] return [wrap_app(app_doc) for app_doc in app_docs if app_doc is not None]
def get_form_list(domain): form_list = [] for app_id in get_app_ids_in_domain(domain): latest_app = get_app(domain, app_id, latest=True) if latest_app.doc_type == "Application": for m in latest_app.get_modules(): for f in m.get_forms(): form_list.append({"code": f.unique_id, "name": f.full_path_name}) return form_list
def get_form_list(domain): form_list = [] for app_id in get_app_ids_in_domain(domain): latest_app = get_app(domain, app_id, latest=True) if not is_remote_app(latest_app): for m in latest_app.get_modules(): for f in m.get_forms(): form_list.append({ "code": f.unique_id, "name": f.full_path_name }) return form_list
def get_form_list(domain): form_list = [] for app_id in get_app_ids_in_domain(domain): latest_app = get_app(domain, app_id, latest=True) if latest_app.doc_type == "Application": for m in latest_app.get_modules(): for f in m.get_forms(): form_list.append({ "code": f.unique_id, "name": f.full_path_name }) return form_list
def handle(self, add_on_name, *args, **options): add_to_toggle = options.get('add_to_toggle') if add_to_toggle: add_to_toggle = find_static_toggle(add_to_toggle) if not add_to_toggle: raise CommandError('Toggle %s not found.' % add_to_toggle) with open("apps_with_feature_%s.csv" % add_on_name, "w", encoding='utf-8') as csvfile: writer = csv.DictWriter(csvfile, fieldnames=[ 'domain', 'application_id', 'app_name', 'all_add_ons_enabled', 'status' ]) writer.writeheader() for domain_obj in self._iter_domains(options): application_ids = get_app_ids_in_domain(domain_obj.name) for application_id in application_ids: application = Application.get(application_id) if not application.is_remote_app(): all_add_ons_enabled = toggles.ENABLE_ALL_ADD_ONS.enabled( domain_obj.name) if add_on_name in application.add_ons or all_add_ons_enabled: try: writer.writerow({ 'domain': domain_obj.name.encode('utf-8'), 'application_id': application.get_id, 'app_name': application.name.encode('utf-8'), 'all_add_ons_enabled': all_add_ons_enabled, 'status': application.add_ons.get(add_on_name) }) if add_to_toggle: add_to_toggle.set(domain_obj.name, True, NAMESPACE_DOMAIN) except UnicodeEncodeError: print('encode error') print({ 'domain': domain_obj.name, 'application_id': application.get_id, 'app_name': application.name, 'all_add_ons_enabled': all_add_ons_enabled, 'status': application.add_ons.get(add_on_name) })
def test_get_built_app_ids_with_submissions_for_app_ids_and_versions(self): app_ids_in_domain = get_app_ids_in_domain(self.domain) app_ids = get_built_app_ids_with_submissions_for_app_ids_and_versions( self.domain, app_ids_in_domain, {self.normal_app._id: self.first_saved_version}, ) self.assertEqual(len(app_ids), 0) # Should skip the one that has_submissions app_ids = get_built_app_ids_with_submissions_for_app_ids_and_versions( self.domain, app_ids_in_domain ) self.assertEqual(len(app_ids), 1) # Should get the one that has_submissions
def handle(self, *args, **options): app_ids = [] try: Application.get(args[0]) app_ids = [args[0]] except ResourceNotFound: app_ids = get_app_ids_in_domain(args[0]) logger.info('migrating {} apps in domain {}'.format(len(app_ids), args[0])) for app_id in app_ids: logger.info('migrating app {}'.format(app_id)) self.migrate_app(app_id) logger.info('done with migrate_app_to_cmitfb')
def get_web_apps_available_to_user(self, domain, user): app_access = ApplicationAccess.get_by_domain(domain) app_ids = get_app_ids_in_domain(domain) apps = list(map( lambda app_id: self.fetch_app(domain, app_id), app_ids, )) apps = filter(None, apps) apps = filter(lambda app: app.get('cloudcare_enabled') or self.preview, apps) apps = filter(lambda app: app_access.user_can_access_app(user, app), apps) role = user.get_role(domain) if role: apps = [app for app in apps if role.permissions.view_web_app(app)] apps = sorted(apps, key=lambda app: app['name']) return apps
def get_web_apps_available_to_user(self, domain, user): app_access = get_application_access_for_domain(domain) app_ids = get_app_ids_in_domain(domain) apps = list( map( lambda app_id: self.fetch_app(domain, app_id), app_ids, )) apps = filter(None, apps) apps = filter(lambda app: app.get('cloudcare_enabled') or self.preview, apps) apps = filter(lambda app: app_access.user_can_access_app(user, app), apps) apps = [_format_app_doc(app) for app in apps] apps = sorted(apps, key=lambda app: app['name']) return apps
def get_visit_scheduler_forms(domain, timestamp): """ The timestamp is set once at the beginning of each loading of the page, so that this result is only calculated once per page load. """ result = [] for app_id in get_app_ids_in_domain(domain): app = get_latest_released_app(domain, app_id) if app and app.doc_type == 'Application': for module in app.get_modules(): for form in module.get_forms(): if isinstance(form, AdvancedForm) and form.schedule and form.schedule.enabled: result.append({ 'id': get_combined_id(app_id, form.unique_id), 'text': form.full_path_name, }) return result
def get_visit_scheduler_forms(domain, timestamp): """ The timestamp is set once at the beginning of each loading of the page, so that this result is only calculated once per page load. """ result = [] for app_id in get_app_ids_in_domain(domain): app = get_latest_released_app(domain, app_id) if app and not app.is_deleted() and not app.is_remote_app(): for module in app.get_modules(): for form in module.get_forms(): if isinstance(form, AdvancedForm) and form.schedule and form.schedule.enabled: result.append({ 'id': get_combined_id(app_id, form.unique_id), 'text': form.full_path_name, }) return result
def get_apps_modules_by_id(domain): """ Return a dictionary of { <app id>: { 'name': <app name>, 'modules': { <module id>: {'name': <module name>} } } } """ apps = {} for app_id in get_app_ids_in_domain(domain): app = get_app(domain, app_id) modules = {} for module in app.get_modules(): modules[module.unique_id] = {'name': module.default_name(app)} apps[app_id] = {'name': app.name, 'modules': modules} return apps
def handle(self, add_on_name, *args, **options): add_to_toggle = options.get('add_to_toggle') if add_to_toggle: add_to_toggle = find_static_toggle(add_to_toggle) if not add_to_toggle: raise CommandError('Toggle %s not found.' % add_to_toggle) with open("apps_with_feature_%s.csv" % add_on_name, "w", encoding='utf-8') as csvfile: writer = csv.DictWriter(csvfile, fieldnames=[ 'domain', 'application_id', 'app_name', 'all_add_ons_enabled', 'status' ]) writer.writeheader() for domain_obj in self._iter_domains(options): application_ids = get_app_ids_in_domain(domain_obj.name) for application_id in application_ids: application = Application.get(application_id) if not application.is_remote_app(): all_add_ons_enabled = toggles.ENABLE_ALL_ADD_ONS.enabled(domain_obj.name) if add_on_name in application.add_ons or all_add_ons_enabled: try: writer.writerow({ 'domain': domain_obj.name.encode('utf-8'), 'application_id': application.get_id, 'app_name': application.name.encode('utf-8'), 'all_add_ons_enabled': all_add_ons_enabled, 'status': application.add_ons.get(add_on_name) }) if add_to_toggle: add_to_toggle.set(domain_obj.name, True, NAMESPACE_DOMAIN) except UnicodeEncodeError: print('encode error') print({ 'domain': domain_obj.name, 'application_id': application.get_id, 'app_name': application.name, 'all_add_ons_enabled': all_add_ons_enabled, 'status': application.add_ons.get(add_on_name) })
def handle(self, **options): logger.setLevel('DEBUG') app_ids_by_domain = defaultdict(set) self.force = options["force"] self.dry = "DRY RUN " if options["dry_run"] else "" self.fail_hard = options["fail_hard"] self.fup_caseref = options["fix_user_props_caseref"] self.fix_user_props = options["fix_user_properties"] or self.fup_caseref self.migrate_usercase = options["usercase"] for ident in options["app_id_or_domain"]: if not (self.migrate_usercase or self.fix_user_props): try: app = Application.get(ident) app_ids_by_domain[app.domain].add(ident) continue except ResourceNotFound: pass app_ids_by_domain[ident].update(get_app_ids_in_domain(ident)) for domain, app_ids in sorted(app_ids_by_domain.items()): logger.info('migrating %s: %s apps', domain, len(app_ids)) for app_id in app_ids: try: app = get_app(domain, app_id) if app.doc_type == "Application": if self.fix_user_props: self.fix_user_properties(app) else: self.migrate_app(app) else: logger.info("Skipping %s/%s because it is a %s", domain, app_id, app.doc_type) except Exception as e: logger.exception("skipping app %s/%s", domain, app_id) if self.fail_hard: raise e logger.info('done with migrate_app_to_cmitfb %s', self.dry)
def post(self, request, *args, **kwargs): action = self.request.POST['action'] # Ajax request if action == 'notes': migration = VCMMigration.objects.get( domain=self.request.POST['domain']) migration.notes = self.request.POST['notes'] migration.save() return json_response({'success': 'success'}) # Form submission if action == 'add': emails = self.request.POST['items'].split(",") notes = self.request.POST['notes'] errors = [] successes = [] for email in emails: user = CouchUser.get_by_username(email) if not user: errors.append("User {} not found".format(email)) else: for domain in user.domains: try: migration = VCMMigration.objects.get(domain=domain) successes.append("Updated domain {} for {}".format( domain, email)) except VCMMigration.DoesNotExist: migration = VCMMigration.objects.create( domain=domain) successes.append("Added domain {} for {}".format( domain, email)) finally: if notes: if migration.notes: migration.notes = migration.notes + '; ' else: migration.notes = '' migration.notes = migration.notes + notes migration.save() if len(successes): messages.success(request, mark_safe("<br>".join(successes)), extra_tags='html') if len(errors): messages.error(request, mark_safe("<br>".join(errors)), extra_tags='html') else: domains = self.request.POST['items'].split(",") errors = set([]) successes = set([]) for domain in domains: migration = VCMMigration.objects.get(domain=domain) if not migration.notes: migration.notes = '' app_count = 0 if action == 'email': email_context = { 'domain': domain, 'migration_date': self.migration_date, 'email': self.email_from, } html_content = render_to_string(self.email_template_html, email_context) text_content = render_to_string(self.email_template_txt, email_context) send_html_email_async.delay(self.email_subject, migration.admins, html_content, text_content=text_content, email_from=self.email_from) migration.emailed = datetime.now() migration.save() successes.add(domain) elif action == 'migrate': for app_id in get_app_ids_in_domain(domain): try: management.call_command('migrate_app_to_cmitfb', app_id) app_count = app_count + 1 except Exception: if migration.notes: migration.notes = migration.notes + '; ' migration.notes = migration.notes + "failed on app {}".format( app_id) errors.add(domain) if domain not in errors: if migration.notes: migration.notes = migration.notes + '; ' migration.notes = migration.notes + "successfully migrated {} domains".format( app_count) successes.add(domain) migration.migrated = datetime.now() migration.save() if len(successes): messages.success( request, "Succeeded with the following {} domains: {}".format( len(successes), ", ".join(successes))) if len(errors): messages.error( request, "Errors in the following {} domains: {}".format( len(errors), ", ".join(errors))) return self.get(request, *args, **kwargs)