def update_linked_model(self, in_data): model = in_data['model'] type_ = model['type'] detail = model['detail'] detail_obj = wrap_detail(type_, detail) if detail else None master_link = get_domain_master_link(self.domain) error = "" try: update_model_type(master_link, type_, detail_obj) model_detail = detail_obj.to_json() if detail_obj else None master_link.update_last_pull(type_, self.request.couch_user._id, model_detail=model_detail) except (DomainLinkError, UnsupportedActionError) as e: error = str(e) track_workflow(self.request.couch_user.username, "Linked domain: updated '{}' model".format(type_)) timezone = get_timezone_for_request() return { 'success': not error, 'error': error, 'last_update': server_to_user_time(master_link.last_pull, timezone) }
def _make_row(self, record, link): row = [ '{} -> {}'.format(link.master_domain, link.linked_domain), server_to_user_time(record.date, self.timezone), self._make_model_cell(record), pretty_doc_info(get_doc_info_by_id(self.domain, record.user_id)) ] return row
def _link_context(self, link, timezone): return { 'linked_domain': link.linked_domain, 'master_domain': link.qualified_master, 'remote_base_url': link.remote_base_url, 'is_remote': link.is_remote, 'last_update': server_to_user_time(link.last_pull, timezone) if link.last_pull else 'Never', }
def _link_context(link, timezone=timezone): return { 'linked_domain': link.linked_domain, 'master_domain': link.qualified_master, 'remote_base_url': link.remote_base_url, 'is_remote': link.is_remote, 'last_update': server_to_user_time(link.last_pull, timezone) if link.last_pull else 'Never', }
def _make_row(self, record, link): row = [ '{} -> {}'.format(link.master_domain, link.linked_domain), server_to_user_time(record.date, self.timezone), self._make_model_cell(record), self._make_user_cell(record) ] return row
def build_pullable_view_models_from_data_models(domain, upstream_link, apps, fixtures, reports, keywords): """ Data models that originated in this domain's upstream domain that are available to pull :return: list of view models (dicts) used to render linked data models that can be pulled """ linked_data_view_models = [] if not upstream_link: return linked_data_view_models models_seen = set() timezone = get_timezone_for_request() history = get_actions_in_domain_link_history(upstream_link) for action in history: if action.row_number != 1: # first row is the most recent continue models_seen.add(action.model) last_update = server_to_user_time(action.date, timezone) if action.model == MODEL_APP: app = pop_app_for_action(action, apps) view_model = build_app_view_model(app, last_update=last_update) elif action.model == MODEL_FIXTURE: fixture = pop_fixture_for_action(action, fixtures, domain) view_model = build_fixture_view_model(fixture, last_update=last_update) elif action.model == MODEL_REPORT: report = pop_report_for_action(action, reports) view_model = build_report_view_model(report, last_update=last_update) elif action.model == MODEL_KEYWORD: keyword = pop_keyword_for_action(action, keywords) view_model = build_keyword_view_model(keyword, last_update=last_update) else: view_model = build_linked_data_view_model( model_type=action.model, name=LINKED_MODELS_MAP[action.model], detail=action.model_detail, last_update=last_update, ) linked_data_view_models.append(view_model) # Add data models that have never been pulled into the downstream domain before # ignoring any models we have already added via domain history linked_data_view_models.extend( build_view_models_from_data_models( domain, apps, fixtures, reports, keywords, ignore_models=models_seen) ) return linked_data_view_models
def _get_model_status(self, master_link, apps, reports): model_status = [] if not master_link: return model_status models_seen = set() history = DomainLinkHistory.objects.filter(link=master_link).annotate(row_number=RawSQL( 'row_number() OVER (PARTITION BY model, model_detail ORDER BY date DESC)', [] )) linked_models = dict(LINKED_MODELS) timezone = get_timezone_for_request() for action in history: models_seen.add(action.model) if action.row_number != 1: # first row is the most recent continue name = linked_models[action.model] update = { 'type': action.model, 'name': name, 'last_update': server_to_user_time(action.date, timezone), 'detail': action.model_detail, 'can_update': True } if action.model == 'app': app_name = ugettext('Unknown App') if action.model_detail: detail = action.wrapped_detail app = apps.pop(detail.app_id, None) app_name = app.name if app else detail.app_id if app: update['detail'] = action.model_detail else: update['can_update'] = False else: update['can_update'] = False update['name'] = '{} ({})'.format(name, app_name) model_status.append(update) if action.model == 'report': report_id = action.wrapped_detail.report_id try: report = reports.get(report_id) del reports[report_id] except KeyError: report = ReportConfiguration.get(report_id) update['name'] = f'{name} ({report.title})' # Add in models and apps that have never been synced model_status.extend(self._get_master_model_status(apps, reports, ignore_models=models_seen)) return model_status
def update_linked_model(self, in_data): model = in_data['model'] type_ = model['type'] detail = model['detail'] detail_obj = wrap_detail(type_, detail) if detail else None master_link = get_domain_master_link(self.domain) update_model_type(master_link, type_, detail_obj) master_link.update_last_pull(type_, self.request.couch_user._id, model_details=detail_obj) track_workflow(self.request.couch_user.username, "Linked domain: updated '{}' model".format(type_)) timezone = get_timezone_for_request() return { 'success': True, 'last_update': server_to_user_time(master_link.last_pull, timezone) }
def update_linked_model(self, in_data): model = in_data['model'] type_ = model['type'] detail = model['detail'] detail_obj = wrap_detail(type, detail) if detail else None master_link = get_domain_master_link(self.domain) update_model_type(master_link, type_, detail_obj) master_link.update_last_pull(type_, self.request.couch_user._id, model_details=detail_obj) track_workflow(self.request.couch_user.username, "Linked domain: updated '{}' model".format(type_)) timezone = get_timezone_for_request() return { 'success': True, 'last_update': server_to_user_time(master_link.last_pull, timezone) }
def _link_context(link, timezone=timezone): return { 'linked_domain': link.linked_domain, 'master_domain': link.master_domain, 'remote_base_url': link.remote_base_url, 'remote_username': link.remote_username, 'remote_api_key': link.remote_api_key, 'is_remote': link.is_remote, 'last_update': server_to_user_time(link.last_pull, timezone) if link.last_pull else 'Never', }
def build_domain_link_view_model(link, timezone): return { 'downstream_domain': link.linked_domain, 'upstream_domain': link.master_domain, 'upstream_url': link.upstream_url, 'downstream_url': link.downstream_url, 'is_remote': link.is_remote, 'last_update': server_to_user_time(link.last_pull, timezone) if link.last_pull else _('Never'), 'has_full_access': link.has_full_access(), }
def _get_model_status(self, master_link, apps, fixtures, reports, keywords): model_status = [] if not master_link: return model_status models_seen = set() history = DomainLinkHistory.objects.filter( link=master_link ).annotate(row_number=RawSQL( 'row_number() OVER (PARTITION BY model, model_detail ORDER BY date DESC)', [])) linked_models = dict(LINKED_MODELS) timezone = get_timezone_for_request() for action in history: models_seen.add(action.model) if action.row_number != 1: # first row is the most recent continue name = linked_models[action.model] update = { 'type': action.model, 'name': name, 'last_update': server_to_user_time(action.date, timezone), 'detail': action.model_detail, 'can_update': True } if action.model == 'app': app_name = ugettext('Unknown App') if action.model_detail: detail = action.wrapped_detail app = apps.pop(detail.app_id, None) app_name = app.name if app else detail.app_id if app: update['detail'] = action.model_detail else: update['can_update'] = False else: update['can_update'] = False update['name'] = '{} ({})'.format(name, app_name) if action.model == 'fixture': tag_name = ugettext('Unknown Table') can_update = False if action.model_detail: detail = action.wrapped_detail tag = action.wrapped_detail.tag try: fixture = fixtures.get(tag) del fixtures[tag] except KeyError: fixture = get_fixture_data_type_by_tag( self.domain, tag) tag_name = fixture.tag can_update = fixture.is_global update['name'] = f'{name} ({tag_name})' update['can_update'] = can_update if action.model == 'report': report_id = action.wrapped_detail.report_id try: report = reports.get(report_id) del reports[report_id] except KeyError: report = ReportConfiguration.get(report_id) update['name'] = f'{name} ({report.title})' if action.model == 'keyword': keyword_id = action.wrapped_detail.linked_keyword_id try: keyword = keywords[keyword_id].keyword del keywords[keyword_id] except KeyError: try: keyword = Keyword.objects.get(id=keyword_id).keyword except Keyword.DoesNotExist: keyword = ugettext_lazy("Deleted Keyword") update['can_update'] = False update['name'] = f'{name} ({keyword})' model_status.append(update) # Add in models and apps that have never been synced model_status.extend( self._get_master_model_status(apps, fixtures, reports, keywords, ignore_models=models_seen)) return model_status
def page_context(self): timezone = get_timezone_for_request() def _link_context(link, timezone=timezone): return { 'linked_domain': link.linked_domain, 'master_domain': link.master_domain, 'remote_base_url': link.remote_base_url, 'remote_username': link.remote_username, 'remote_api_key': link.remote_api_key, 'is_remote': link.is_remote, 'last_update': server_to_user_time(link.last_pull, timezone) if link.last_pull else 'Never', } model_status = [] linked_models = dict(LINKED_MODELS) master_link = get_domain_master_link(self.domain) if master_link: linked_apps = { app._id: app for app in get_brief_apps_in_domain(self.domain) if app.doc_type == 'LinkedApplication' } models_seen = set() history = DomainLinkHistory.objects.filter( link=master_link ).annotate(row_number=RawSQL( 'row_number() OVER (PARTITION BY model, model_detail ORDER BY date DESC)', [])) for action in history: models_seen.add(action.model) if action.row_number != 1: # first row is the most recent continue name = linked_models[action.model] update = { 'type': action.model, 'name': name, 'last_update': server_to_user_time(action.date, timezone), 'detail': action.model_detail, 'can_update': True } if action.model == 'app': app_name = 'Unknown App' if action.model_detail: detail = action.wrapped_detail app = linked_apps.pop(detail.app_id, None) app_name = app.name if app else detail.app_id if app: update['detail'] = action.model_detail else: update['can_update'] = False else: update['can_update'] = False update['name'] = '{} ({})'.format(name, app_name) model_status.append(update) # Add in models that have never been synced for model, name in LINKED_MODELS: if model not in models_seen and model != 'app': model_status.append({ 'type': model, 'name': name, 'last_update': ugettext('Never'), 'detail': None, 'can_update': True }) # Add in apps that have never been synced if linked_apps: for app in linked_apps.values(): update = { 'type': 'app', 'name': '{} ({})'.format(linked_models['app'], app.name), 'last_update': None, 'detail': AppLinkDetail(app_id=app._id).to_json(), 'can_update': True } model_status.append(update) return { 'domain': self.domain, 'timezone': timezone.localize(datetime.utcnow()).tzname(), 'view_data': { 'master_link': _link_context(master_link) if master_link else None, 'model_status': sorted(model_status, key=lambda m: m['name']), 'linked_domains': [ _link_context(link) for link in get_linked_domains(self.domain) ], 'models': [{ 'slug': model[0], 'name': model[1] } for model in LINKED_MODELS] }, }
def page_context(self): timezone = get_timezone_for_request() def _link_context(link, timezone=timezone): return { 'linked_domain': link.linked_domain, 'master_domain': link.qualified_master, 'remote_base_url': link.remote_base_url, 'is_remote': link.is_remote, 'last_update': server_to_user_time(link.last_pull, timezone) if link.last_pull else 'Never', } model_status = [] linked_models = dict(LINKED_MODELS) master_link = get_domain_master_link(self.domain) if master_link: linked_apps = { app._id: app for app in get_brief_apps_in_domain(self.domain) if app.doc_type == 'LinkedApplication' } models_seen = set() history = DomainLinkHistory.objects.filter(link=master_link).annotate(row_number=RawSQL( 'row_number() OVER (PARTITION BY model, model_detail ORDER BY date DESC)', [] )) for action in history: models_seen.add(action.model) if action.row_number != 1: # first row is the most recent continue name = linked_models[action.model] update = { 'type': action.model, 'name': name, 'last_update': server_to_user_time(action.date, timezone), 'detail': action.model_detail, 'can_update': True } if action.model == 'app': app_name = 'Unknown App' if action.model_detail: detail = action.wrapped_detail app = linked_apps.pop(detail.app_id, None) app_name = app.name if app else detail.app_id if app: update['detail'] = action.model_detail else: update['can_update'] = False else: update['can_update'] = False update['name'] = '{} ({})'.format(name, app_name) model_status.append(update) # Add in models that have never been synced for model, name in LINKED_MODELS: if model not in models_seen and model != 'app': model_status.append({ 'type': model, 'name': name, 'last_update': ugettext('Never'), 'detail': None, 'can_update': True }) # Add in apps that have never been synced if linked_apps: for app in linked_apps.values(): update = { 'type': 'app', 'name': '{} ({})'.format(linked_models['app'], app.name), 'last_update': None, 'detail': AppLinkDetail(app_id=app._id).to_json(), 'can_update': True } model_status.append(update) return { 'domain': self.domain, 'timezone': timezone.localize(datetime.utcnow()).tzname(), 'view_data': { 'master_link': _link_context(master_link) if master_link else None, 'model_status': sorted(model_status, key=lambda m: m['name']), 'linked_domains': [ _link_context(link) for link in get_linked_domains(self.domain) ], 'models': [ {'slug': model[0], 'name': model[1]} for model in LINKED_MODELS ] }, }