def index(request): """ Page that allows to select a template to start composing. Args: request: Returns: """ assets = { "js": [], } global_active_template_list = template_version_manager_api.get_active_global_version_manager(_cls=True) user_active_template_list = template_version_manager_api.get_active_version_manager_by_user_id(request.user.id, _cls=True) global_active_type_list = type_version_manager_api.get_active_global_version_manager() user_active_type_list = type_version_manager_api.get_active_version_manager_by_user_id(request.user.id) context = { 'global_templates': global_active_template_list, 'global_types': global_active_type_list, 'user_templates': user_active_template_list, 'user_types': user_active_type_list, } return render(request, 'core_composer_app/user/index.html', assets=assets, context=context)
def test_get_active_global_version_manager_as_superuser_returns_global_tvm( self): mock_request = create_mock_request(user=self.superuser1) list_tvm = template_vm_api.get_active_global_version_manager( request=mock_request) for tvm in list_tvm: self.assertEqual(tvm.user, None)
def test_get_active_global_version_managers_returns_templates_not_disable(self, mock_get_active_global_version_managers): # Arrange mock_template1 = _create_template_version_manager("test1") mock_template2 = _create_template_version_manager("test2") queryset = MagicMock() queryset.filter.return_value = queryset queryset.all.return_value = [mock_template1, mock_template2] mock_get_active_global_version_managers.return_value = queryset result = version_manager_api.get_active_global_version_manager() # Assert self.assertTrue(all(item.is_disabled is False for item in result))
def _get_templates_manager(): """ Get templates manager. Returns: List of templates manager. """ templates_manager = [] try: list_ = template_version_manager_api.get_active_global_version_manager( ) for elt in list_: templates_manager.append((elt.id, elt.title)) except Exception: pass return templates_manager
def _get_templates_manager(request): """Get templates manager. Args: request: Returns: List of templates manager. """ templates_manager = [] list_ = template_version_manager_api.get_active_global_version_manager( request=request ) for elt in list_: templates_manager.append((elt.id, elt.title)) return templates_manager
def index(request): """Page that allows to select a template to start curating. Args: request: Returns: """ assets = { "js": [ { "path": "core_curate_app/user/js/select_template.js", "is_raw": False }, { "path": "core_curate_app/user/js/select_template.raw.js", "is_raw": True }, ], "css": [ "core_curate_app/user/css/common.css", "core_curate_app/user/css/style.css", ], } global_active_template_list = ( template_version_manager_api.get_active_global_version_manager( request=request)) user_active_template_list = ( template_version_manager_api.get_active_version_manager_by_user_id( request=request)) context = { "templates_version_manager": global_active_template_list, "userTemplates": user_active_template_list, } return render( request, "core_curate_app/user/curate.html", # modals=modals, assets=assets, context=context, )
def __init__(self, *args, **kwargs): """ Init Keyword form """ super(KeywordForm, self).__init__(*args, **kwargs) # initialize template filters global_templates = [ (template_version_manager.id, template_version_manager.title) for template_version_manager in template_version_manager_api.get_active_global_version_manager() ] user_templates = [ (template_version_manager.id, template_version_manager.title) for template_version_manager in template_version_manager_api. get_active_version_manager_by_user_id(self.data['user_id']) ] self.fields['global_templates'].choices = global_templates self.fields['user_templates'].choices = user_templates
def __init__(self, *args, **kwargs): """ Init function for query. :param args: Used by the super class init. :param kwargs: Used by the super class init. """ super(Query, self).__init__(*args, **kwargs) self.initial['group'] = 'admin' template_version_managers = template_version_manager_api.get_active_global_version_manager( ) list_templates = list() for template_version_manager in template_version_managers: for version_id in template_version_manager.versions: template = template_api.get(version_id) list_templates.append([version_id, template.display_name]) self.fields['schema'].choices = list_templates
def _get_templates_versions(): """ Get templates versions. Returns: List of templates versions. """ templates = [] try: list_ = template_version_manager_api.get_active_global_version_manager( ) for elt in list_: for version in elt.versions: template = template_api.get(version) version_name = template.display_name templates.append((version, version_name)) except Exception: pass return templates
def _get_templates_versions(request): """Get templates versions. Returns: List of templates versions. """ templates = [] try: list_ = template_version_manager_api.get_active_global_version_manager( request=request ) for elt in list_: for version in elt.versions: template = template_api.get(version, request=request) version_name = template.display_name templates.append((version, version_name)) except exceptions.DoesNotExist as e: logger.warning("_get_templates_versions threw an exception: {0}".format(str(e))) return templates
def index(request): """Page that allows to select a template to start composing. Args: request: Returns: """ assets = {"js": [], "css": []} global_active_template_list = ( template_version_manager_api.get_active_global_version_manager( request=request, _cls=True ) ) user_active_template_list = ( template_version_manager_api.get_active_version_manager_by_user_id( request=request, _cls=True ) ) global_active_type_list = ( type_version_manager_api.get_active_global_version_manager(request=request) ) user_active_type_list = ( type_version_manager_api.get_active_version_manager_by_user_id(request=request) ) context = { "global_templates": global_active_template_list, "global_types": global_active_type_list, "user_templates": user_active_template_list, "user_types": user_active_type_list, } return render( request, "core_composer_app/user/index.html", assets=assets, context=context )
def index(request): """ Page that allows to select a template to start curating. Args: request: Returns: """ assets = { "js": [ { "path": 'core_curate_app/user/js/select_template.js', "is_raw": False }, { "path": 'core_curate_app/user/js/select_template.raw.js', "is_raw": True }, ], "css": ['core_curate_app/user/css/style.css'] } global_active_template_list = template_api.get_active_global_version_manager( ) user_active_template_list = template_api.get_active_version_manager_by_user_id( request.user.id) context = { 'templates_version_manager': global_active_template_list, 'userTemplates': user_active_template_list, } return render( request, 'core_curate_app/user/curate.html', # modals=modals, assets=assets, context=context)
def choose_query(request): """ Select a query from the one into the database to be able to use it. Used by the user. If there is no query into the database, an error message is shown to the user. If no query has been selected, an error message is shown to the user. Send to the query_step if a query has been selected. If history queries are available, they are show with the following option: _History deletion _History recovery Args: request: Returns: """ formset = None formset_history = None history_queries = None # Come back from the form with the query selected if request.method == 'POST': # Get the forms variable query_id = request.POST.get('query', None) to_recover = request.POST.get('to_recover', None) number_forms = request.POST.get('form-TOTAL_FORMS', None) # if a variable is none, come back with an error message if query_id is None and to_recover is None and number_forms is None: messages.add_message(request, messages.INFO, "You have to select a query.") else: if query_id is not None: return redirect( reverse("core_custom_queries_app_steps", kwargs={'query_id': query_id})) # History selected elif to_recover is not None and to_recover != '': return redirect( reverse("core_custom_queries_app_recover_steps", kwargs={'history_id': to_recover})) # Delete history query else: number_forms = int(number_forms) formset_history = formset_factory(FormHistory, extra=number_forms, can_delete=True) formset = formset_history(request.POST, request.FILES) if formset.is_valid(): if len(formset.deleted_forms) == 0: messages.add_message(request, messages.ERROR, "Please select a query first.") for form in formset.deleted_forms: data = form.cleaned_data if 'DELETE' in data: id_history = ObjectId(data['id_history']) history = history_query_api.get_by_id(id_history) temp_query_id = history.query_id query = temp_user_query_api.get_by_id( temp_query_id) query.delete_database() messages.add_message(request, messages.SUCCESS, "Queries successfully deleted.") # Create the query form from the ones into the database id_list = list() # Get the available templates available_template_version_managers = template_version_manager_api.get_active_global_version_manager( ) for available_template_version_manager in available_template_version_managers: for available_template_id in available_template_version_manager.versions: id_list.append(str(available_template_id)) # Get the queries available for the current user if request.user.is_staff: # FIXME: call specialized api queries = dyn_query_api.get_all().filter( schema__in=id_list).values_list('pk', 'name') else: # FIXME: call specialized api queries = dyn_query_api.get_all().filter(schema__in=id_list).filter( group='user').values_list('pk', 'name') number_queries = len(queries) form = None if number_queries != 0: form = FormChooseQuery(query=queries) else: # If no query available, set an error message message = "No query available." messages.add_message(request, messages.ERROR, message) try: # If histories exist load them history_queries = history_query_api.get_all_by_user_id( str(request.user.id)) number_history = len(history_queries) except Exception: number_history = 0 if number_history > 0: # Create the formset with the histories values formset_history = formset_factory(FormHistory, extra=number_history, can_delete=True) data = dict() data['form-TOTAL_FORMS'] = number_history data['form-INITIAL_FORMS'] = number_history data['form-MAX_NUM_FORMS'] = number_history position = 0 for history_query in history_queries: try: query_id = str(history_query.query_id) data['form-' + str(position) + '-id_history'] = str( history_query.id) query = temp_user_query_api.get_by_id(query_id) data['form-' + str(position) + '-query_name'] = query.query.name data['form-' + str(position) + '-query_last_modified'] = query.last_modified h_message = history_query.message if h_message == "Pending.": redis_server = Redis.from_url(REDIS_URL) try: if redis_server.exists("current_id"): current_id = redis_server.get( "current_id").decode() if current_id == query_id: h_message = "Pending: The file creation will begin shortly." if redis_server.exists("list_ids"): list_id = redis_server.lrange('list_ids', 0, -1) try: position_id = list_id.index(query_id) + 1 h_message = "Pending - Waiting list: " + str( position_id) + "/" + str(len(list_id)) except ValueError as e: logger.warning( "choose_query threw an exception: {0}". format(str(e))) except ConnectionError as e: log_file = LogFile( application="Custom Queries", message="Redis not reachable, is it running?", additionalInformation={'message': str(e)}, timestamp=datetime.now()) log_file_api.upsert(log_file) data['form-' + str(position) + '-query_message'] = h_message data['form-' + str(position) + '-status'] = history_query.status data['form-' + str(position) + '-DELETE'] = '' position += 1 except DoesNotExist as e: logger.warning("choose_query threw an exception: {0}".format( str(e))) formset = formset_history(data) context = { 'number_queries': number_queries, 'number_history': number_history, 'form': form, 'formset': formset, 'formset_history': formset_history } assets = { "js": [ { "path": 'core_custom_queries_app/user/js/custom_queries.js', "is_raw": False }, ], } return render(request, 'core_custom_queries_app/user/select_query.html', assets=assets, context=context)
def test_get_active_global_version_manager_as_anonymous_raises_acces_control_error( self, ): mock_request = create_mock_request(user=self.anonymous_user) with self.assertRaises(AccessControlError): template_vm_api.get_active_global_version_manager( request=mock_request)