Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
dscc-admin-ch committed Jun 10, 2024
1 parent b7f21e6 commit fbe53b7
Show file tree
Hide file tree
Showing 21 changed files with 1,232 additions and 185 deletions.
1 change: 1 addition & 0 deletions html/develop/en/_modules/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ <h1>All modules for which code is available</h1>
<li><a href="lomas_server/tests/test_api.html">lomas_server.tests.test_api</a></li>
<li><a href="lomas_server/tests/test_dummy_generation.html">lomas_server.tests.test_dummy_generation</a></li>
<li><a href="lomas_server/tests/test_mongodb_admin.html">lomas_server.tests.test_mongodb_admin</a></li>
<li><a href="lomas_server/tests/test_mongodb_admin_cli.html">lomas_server.tests.test_mongodb_admin_cli</a></li>
<li><a href="lomas_server/utils/anti_timing_att.html">lomas_server.utils.anti_timing_att</a></li>
<li><a href="lomas_server/utils/collections_models.html">lomas_server.utils.collections_models</a></li>
<li><a href="lomas_server/utils/config.html">lomas_server.utils.config</a></li>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -282,25 +282,6 @@ <h1>Source code for lomas_server.admin_database.admin_database</h1><div class="h
<span class="sd"> &quot;&quot;&quot;</span></div>


<div class="viewcode-block" id="AdminDatabase.may_user_query">
<a class="viewcode-back" href="../../../lomas_server.admin_database.html#lomas_server.admin_database.admin_database.AdminDatabase.may_user_query">[docs]</a>
<span class="nd">@abstractmethod</span>
<span class="nd">@user_must_exist</span>
<span class="k">def</span> <span class="nf">may_user_query</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">user_name</span><span class="p">:</span> <span class="nb">str</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">bool</span><span class="p">:</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Checks if a user may query the server.</span>
<span class="sd"> Cannot query if already querying.</span>

<span class="sd"> Wrapped by :py:func:`user_must_exist`.</span>

<span class="sd"> Args:</span>
<span class="sd"> user_name (str): name of the user</span>

<span class="sd"> Returns:</span>
<span class="sd"> bool: True if the user exists, False otherwise.</span>
<span class="sd"> &quot;&quot;&quot;</span></div>


<div class="viewcode-block" id="AdminDatabase.set_may_user_query">
<a class="viewcode-back" href="../../../lomas_server.admin_database.html#lomas_server.admin_database.admin_database.AdminDatabase.set_may_user_query">[docs]</a>
<span class="nd">@abstractmethod</span>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,25 +178,6 @@ <h1>Source code for lomas_server.admin_database.mongodb_database</h1><div class=
<span class="k">return</span> <span class="n">metadatas</span><span class="p">[</span><span class="n">dataset_name</span><span class="p">]</span> <span class="c1"># type: ignore</span></div>


<div class="viewcode-block" id="AdminMongoDatabase.may_user_query">
<a class="viewcode-back" href="../../../lomas_server.admin_database.html#lomas_server.admin_database.mongodb_database.AdminMongoDatabase.may_user_query">[docs]</a>
<span class="nd">@user_must_exist</span>
<span class="k">def</span> <span class="nf">may_user_query</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">user_name</span><span class="p">:</span> <span class="nb">str</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">bool</span><span class="p">:</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;Checks if a user may query the server.</span>
<span class="sd"> Cannot query if already querying.</span>

<span class="sd"> Wrapped by :py:func:`user_must_exist`.</span>

<span class="sd"> Args:</span>
<span class="sd"> user_name (str): name of the user</span>

<span class="sd"> Returns:</span>
<span class="sd"> bool: True if the user exists, False otherwise.</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="n">user</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">db</span><span class="o">.</span><span class="n">users</span><span class="o">.</span><span class="n">find_one</span><span class="p">({</span><span class="s2">&quot;user_name&quot;</span><span class="p">:</span> <span class="n">user_name</span><span class="p">})</span>
<span class="k">return</span> <span class="n">user</span><span class="p">[</span><span class="s2">&quot;may_query&quot;</span><span class="p">]</span> <span class="c1"># type: ignore</span></div>


<div class="viewcode-block" id="AdminMongoDatabase.set_may_user_query">
<a class="viewcode-back" href="../../../lomas_server.admin_database.html#lomas_server.admin_database.mongodb_database.AdminMongoDatabase.set_may_user_query">[docs]</a>
<span class="nd">@user_must_exist</span>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,28 +182,6 @@ <h1>Source code for lomas_server.admin_database.yaml_database</h1><div class="hi
<span class="k">return</span> <span class="n">metadata</span></div>


<div class="viewcode-block" id="AdminYamlDatabase.may_user_query">
<a class="viewcode-back" href="../../../lomas_server.admin_database.html#lomas_server.admin_database.yaml_database.AdminYamlDatabase.may_user_query">[docs]</a>
<span class="nd">@user_must_exist</span>
<span class="k">def</span> <span class="nf">may_user_query</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">user_name</span><span class="p">:</span> <span class="nb">str</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">bool</span><span class="p">:</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;Checks if a user may query the server.</span>
<span class="sd"> Cannot query if already querying.</span>

<span class="sd"> Wrapped by :py:func:`user_must_exist`.</span>

<span class="sd"> Args:</span>
<span class="sd"> user_name (str): name of the user</span>

<span class="sd"> Returns:</span>
<span class="sd"> bool: True if the user exists, False otherwise.</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="k">for</span> <span class="n">user</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">database</span><span class="p">[</span><span class="s2">&quot;users&quot;</span><span class="p">]:</span>
<span class="k">if</span> <span class="n">user</span><span class="p">[</span><span class="s2">&quot;user_name&quot;</span><span class="p">]</span> <span class="o">==</span> <span class="n">user_name</span><span class="p">:</span>
<span class="k">return</span> <span class="n">user</span><span class="p">[</span><span class="s2">&quot;may_query&quot;</span><span class="p">]</span>
<span class="c1"># if user not found, return false</span>
<span class="k">return</span> <span class="kc">False</span></div>


<div class="viewcode-block" id="AdminYamlDatabase.set_may_user_query">
<a class="viewcode-back" href="../../../lomas_server.admin_database.html#lomas_server.admin_database.yaml_database.AdminYamlDatabase.set_may_user_query">[docs]</a>
<span class="nd">@user_must_exist</span>
Expand Down
37 changes: 14 additions & 23 deletions html/develop/en/_modules/lomas_server/mongodb_admin.html
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,7 @@ <h1>Source code for lomas_server.mongodb_admin</h1><div class="highlight"><pre>
<span class="k">if</span> <span class="n">overwrite</span><span class="p">:</span>
<span class="k">for</span> <span class="n">user</span> <span class="ow">in</span> <span class="n">existing_users</span><span class="p">:</span>
<span class="n">user_filter</span> <span class="o">=</span> <span class="p">{</span><span class="s2">&quot;user_name&quot;</span><span class="p">:</span> <span class="n">user</span><span class="o">.</span><span class="n">user_name</span><span class="p">}</span>
<span class="n">update_operation</span> <span class="o">=</span> <span class="p">{</span><span class="s2">&quot;$set&quot;</span><span class="p">:</span> <span class="n">user</span><span class="o">.</span><span class="n">dict</span><span class="p">()}</span>
<span class="n">update_operation</span> <span class="o">=</span> <span class="p">{</span><span class="s2">&quot;$set&quot;</span><span class="p">:</span> <span class="n">user</span><span class="o">.</span><span class="n">model_dump</span><span class="p">()}</span>
<span class="n">res</span><span class="p">:</span> <span class="n">_WriteResult</span> <span class="o">=</span> <span class="n">db</span><span class="o">.</span><span class="n">users</span><span class="o">.</span><span class="n">update_many</span><span class="p">(</span>
<span class="n">user_filter</span><span class="p">,</span> <span class="n">update_operation</span>
<span class="p">)</span>
Expand All @@ -586,7 +586,7 @@ <h1>Source code for lomas_server.mongodb_admin</h1><div class="highlight"><pre>

<span class="k">if</span> <span class="n">new_users</span><span class="p">:</span>
<span class="c1"># Insert new users</span>
<span class="n">new_users_dicts</span> <span class="o">=</span> <span class="p">[</span><span class="n">user</span><span class="o">.</span><span class="n">dict</span><span class="p">()</span> <span class="k">for</span> <span class="n">user</span> <span class="ow">in</span> <span class="n">new_users</span><span class="p">]</span>
<span class="n">new_users_dicts</span> <span class="o">=</span> <span class="p">[</span><span class="n">user</span><span class="o">.</span><span class="n">model_dump</span><span class="p">()</span> <span class="k">for</span> <span class="n">user</span> <span class="ow">in</span> <span class="n">new_users</span><span class="p">]</span>
<span class="n">res</span> <span class="o">=</span> <span class="n">db</span><span class="o">.</span><span class="n">users</span><span class="o">.</span><span class="n">insert_many</span><span class="p">(</span><span class="n">new_users_dicts</span><span class="p">)</span>
<span class="n">check_result_acknowledged</span><span class="p">(</span><span class="n">res</span><span class="p">)</span>
<span class="n">LOG</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s2">&quot;Added user data from yaml.&quot;</span><span class="p">)</span>
Expand Down Expand Up @@ -649,15 +649,11 @@ <h1>Source code for lomas_server.mongodb_admin</h1><div class="highlight"><pre>
<span class="sd"> user_datasets (list): list of names of all users</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="n">user_data</span> <span class="o">=</span> <span class="n">db</span><span class="o">.</span><span class="n">users</span><span class="o">.</span><span class="n">find_one</span><span class="p">({</span><span class="s2">&quot;user_name&quot;</span><span class="p">:</span> <span class="n">user</span><span class="p">})</span>
<span class="k">if</span> <span class="n">user_data</span><span class="p">:</span>
<span class="n">LOG</span><span class="o">.</span><span class="n">info</span><span class="p">(</span>
<span class="p">[</span><span class="n">dataset</span><span class="p">[</span><span class="s2">&quot;dataset_name&quot;</span><span class="p">]</span> <span class="k">for</span> <span class="n">dataset</span> <span class="ow">in</span> <span class="n">user_data</span><span class="p">[</span><span class="s2">&quot;datasets_list&quot;</span><span class="p">]]</span>
<span class="p">)</span>
<span class="k">return</span> <span class="p">[</span>
<span class="n">dataset</span><span class="p">[</span><span class="s2">&quot;dataset_name&quot;</span><span class="p">]</span> <span class="k">for</span> <span class="n">dataset</span> <span class="ow">in</span> <span class="n">user_data</span><span class="p">[</span><span class="s2">&quot;datasets_list&quot;</span><span class="p">]</span>
<span class="p">]</span>
<span class="n">LOG</span><span class="o">.</span><span class="n">info</span><span class="p">([])</span>
<span class="k">return</span> <span class="p">[]</span></div>
<span class="k">assert</span> <span class="n">user_data</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">,</span> <span class="s2">&quot;User must exist&quot;</span>
<span class="n">LOG</span><span class="o">.</span><span class="n">info</span><span class="p">(</span>
<span class="p">[</span><span class="n">dataset</span><span class="p">[</span><span class="s2">&quot;dataset_name&quot;</span><span class="p">]</span> <span class="k">for</span> <span class="n">dataset</span> <span class="ow">in</span> <span class="n">user_data</span><span class="p">[</span><span class="s2">&quot;datasets_list&quot;</span><span class="p">]]</span>
<span class="p">)</span>
<span class="k">return</span> <span class="p">[</span><span class="n">dataset</span><span class="p">[</span><span class="s2">&quot;dataset_name&quot;</span><span class="p">]</span> <span class="k">for</span> <span class="n">dataset</span> <span class="ow">in</span> <span class="n">user_data</span><span class="p">[</span><span class="s2">&quot;datasets_list&quot;</span><span class="p">]]</span></div>



Expand Down Expand Up @@ -833,7 +829,7 @@ <h1>Source code for lomas_server.mongodb_admin</h1><div class="highlight"><pre>
<span class="k">if</span> <span class="n">overwrite_datasets</span><span class="p">:</span>
<span class="k">for</span> <span class="n">d</span> <span class="ow">in</span> <span class="n">existing_datasets</span><span class="p">:</span>
<span class="n">dataset_filter</span> <span class="o">=</span> <span class="p">{</span><span class="s2">&quot;dataset_name&quot;</span><span class="p">:</span> <span class="n">d</span><span class="o">.</span><span class="n">dataset_name</span><span class="p">}</span>
<span class="n">update_operation</span> <span class="o">=</span> <span class="p">{</span><span class="s2">&quot;$set&quot;</span><span class="p">:</span> <span class="n">d</span><span class="o">.</span><span class="n">dict</span><span class="p">()}</span>
<span class="n">update_operation</span> <span class="o">=</span> <span class="p">{</span><span class="s2">&quot;$set&quot;</span><span class="p">:</span> <span class="n">d</span><span class="o">.</span><span class="n">model_dump</span><span class="p">()}</span>
<span class="n">res</span><span class="p">:</span> <span class="n">_WriteResult</span> <span class="o">=</span> <span class="n">db</span><span class="o">.</span><span class="n">datasets</span><span class="o">.</span><span class="n">update_many</span><span class="p">(</span>
<span class="n">dataset_filter</span><span class="p">,</span> <span class="n">update_operation</span>
<span class="p">)</span>
Expand All @@ -847,7 +843,7 @@ <h1>Source code for lomas_server.mongodb_admin</h1><div class="highlight"><pre>

<span class="c1"># Add dataset collection</span>
<span class="k">if</span> <span class="n">new_datasets</span><span class="p">:</span>
<span class="n">new_datasets_dicts</span> <span class="o">=</span> <span class="p">[</span><span class="n">d</span><span class="o">.</span><span class="n">dict</span><span class="p">()</span> <span class="k">for</span> <span class="n">d</span> <span class="ow">in</span> <span class="n">new_datasets</span><span class="p">]</span>
<span class="n">new_datasets_dicts</span> <span class="o">=</span> <span class="p">[</span><span class="n">d</span><span class="o">.</span><span class="n">model_dump</span><span class="p">()</span> <span class="k">for</span> <span class="n">d</span> <span class="ow">in</span> <span class="n">new_datasets</span><span class="p">]</span>
<span class="n">res</span> <span class="o">=</span> <span class="n">db</span><span class="o">.</span><span class="n">datasets</span><span class="o">.</span><span class="n">insert_many</span><span class="p">(</span><span class="n">new_datasets_dicts</span><span class="p">)</span>
<span class="n">check_result_acknowledged</span><span class="p">(</span><span class="n">res</span><span class="p">)</span>
<span class="n">LOG</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s2">&quot;Added datasets collection from yaml.&quot;</span><span class="p">)</span>
Expand Down Expand Up @@ -962,14 +958,12 @@ <h1>Source code for lomas_server.mongodb_admin</h1><div class="highlight"><pre>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="c1"># Retrieve the document containing metadata for the specified dataset</span>
<span class="n">metadata_document</span> <span class="o">=</span> <span class="n">db</span><span class="o">.</span><span class="n">metadata</span><span class="o">.</span><span class="n">find_one</span><span class="p">({</span><span class="n">dataset</span><span class="p">:</span> <span class="p">{</span><span class="s2">&quot;$exists&quot;</span><span class="p">:</span> <span class="kc">True</span><span class="p">}})</span>
<span class="k">assert</span> <span class="n">metadata_document</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">,</span> <span class="s2">&quot;Metadata must exist&quot;</span>

<span class="k">if</span> <span class="n">metadata_document</span><span class="p">:</span>
<span class="c1"># Extract metadata for the specified dataset</span>
<span class="n">metadata_info</span> <span class="o">=</span> <span class="n">metadata_document</span><span class="p">[</span><span class="n">dataset</span><span class="p">]</span>
<span class="n">LOG</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="n">metadata_info</span><span class="p">)</span>
<span class="k">return</span> <span class="n">metadata_info</span>

<span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="sa">f</span><span class="s2">&quot;No metadata found for dataset: </span><span class="si">{</span><span class="n">dataset</span><span class="si">}</span><span class="s2">&quot;</span><span class="p">)</span></div>
<span class="c1"># Extract metadata for the specified dataset</span>
<span class="n">metadata_info</span> <span class="o">=</span> <span class="n">metadata_document</span><span class="p">[</span><span class="n">dataset</span><span class="p">]</span>
<span class="n">LOG</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="n">metadata_info</span><span class="p">)</span>
<span class="k">return</span> <span class="n">metadata_info</span></div>



Expand Down Expand Up @@ -1023,9 +1017,6 @@ <h1>Source code for lomas_server.mongodb_admin</h1><div class="highlight"><pre>
<span class="sd"> None</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="n">collection_query</span> <span class="o">=</span> <span class="n">db</span><span class="p">[</span><span class="n">collection</span><span class="p">]</span><span class="o">.</span><span class="n">find</span><span class="p">({})</span>
<span class="k">if</span> <span class="ow">not</span> <span class="n">collection_query</span><span class="p">:</span>
<span class="k">return</span> <span class="p">[]</span>

<span class="n">collections</span> <span class="o">=</span> <span class="p">[]</span>
<span class="k">for</span> <span class="n">document</span> <span class="ow">in</span> <span class="n">collection_query</span><span class="p">:</span>
<span class="n">document</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="s2">&quot;_id&quot;</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
Expand Down
Loading

0 comments on commit fbe53b7

Please sign in to comment.