File size: 22,329 Bytes
66b3b8e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 |
<!DOCTYPE html>
<html lang="en" data-content_root="./">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Code Overview — pgAdmin 4 8.6 documentation</title>
<link rel="stylesheet" type="text/css" href="_static/pygments.css?v=fa44fd50" />
<link rel="stylesheet" type="text/css" href="_static/style.css?v=d36593c3" />
<script src="_static/documentation_options.js?v=d4c83366"></script>
<script src="_static/doctools.js?v=9a2dae69"></script>
<script src="_static/sphinx_highlight.js?v=dc90522c"></script>
<script src="_static/sidebar.js"></script>
<link rel="index" title="Index" href="genindex.html" />
<link rel="search" title="Search" href="search.html" />
<link rel="next" title="Coding Standards" href="coding_standards.html" />
<link rel="prev" title="Submitting Pull Requests" href="submitting_pull_requests.html" />
</head><body>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="coding_standards.html" title="Coding Standards"
accesskey="N">next</a> |</li>
<li class="right" >
<a href="submitting_pull_requests.html" title="Submitting Pull Requests"
accesskey="P">previous</a> |</li>
<li class="nav-item nav-item-0"><a href="index.html">pgAdmin 4 8.6 documentation</a> »</li>
<li class="nav-item nav-item-1"><a href="contributions.html" accesskey="U">pgAdmin Project Contributions</a> »</li>
<li class="nav-item nav-item-this"><a href="">Code Overview</a></li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<section id="code-overview">
<span id="id1"></span><h1><span class="target" id="index-0"></span>Code Overview<a class="headerlink" href="#code-overview" title="Link to this heading">¶</a></h1>
<p>The bulk of pgAdmin is a Python web application written using the Flask framework
on the backend, and HTML5 with CSS3,ReactJS on the front end. A
desktop runtime is also included for users that prefer a desktop application to
a web application, which is written using NWjs (Node Webkit).</p>
<section id="runtime">
<h2>Runtime<a class="headerlink" href="#runtime" title="Link to this heading">¶</a></h2>
<p>The runtime is based on NWjs which integrates a browser and the Python server
creating a standalone application. The source code can be found in the
<strong>/runtime</strong> directory in the source tree.</p>
</section>
<section id="web-application">
<h2>Web Application<a class="headerlink" href="#web-application" title="Link to this heading">¶</a></h2>
<p>The web application forms the bulk of pgAdmin and can be found in the <strong>/web</strong>
directory in the source tree. The main file is <strong>pgAdmin4.py</strong> which can be used
to run the built-in standalone web server, or as a WSGI application for production
use.</p>
<section id="configuration">
<h3>Configuration<a class="headerlink" href="#configuration" title="Link to this heading">¶</a></h3>
<p>The core application configuration is found in <strong>config.py</strong>. This file includes
all configurable settings for the application, along with descriptions of their
use. It is essential that various settings are configured prior to deployment on
a web server; these can be overridden in <strong>config_local.py</strong> or
<strong>config_system.py</strong> (see the <a class="reference internal" href="config_py.html#config-py"><span class="std std-ref">config.py</span></a> documentation) to
avoid modifying the main configuration file.</p>
</section>
<section id="user-settings">
<h3>User Settings<a class="headerlink" href="#user-settings" title="Link to this heading">¶</a></h3>
<p>When running in desktop mode, pgAdmin has a single, default user account that is
used for the desktop user. When running in server mode, there may be unlimited
users who are required to login prior to using the application. pgAdmin utilised
the <strong>Flask-Security</strong> module to manage application security and users, and
provides options for self-service password reset and password changes etc.</p>
<p>Whether in desktop or server mode, each user’s settings are stored in a SQLite
OR external database which is also used to store the user accounts. This is initially
created using the <strong>setup.py</strong> script which will create the database file and
schema within it, and add the first user account (with administrative
privileges) and a default server group for them. A <strong>settings</strong> table is also
used to store user configuration settings in a key-value fashion. Although not
required, setting keys (or names) are typically formatted using forward slashes
to artificially namespace values, much like the pgAdmin 3 settings files on Linux
or Mac.</p>
<p>Note that the local configuration must be setup prior to <strong>setup.py</strong> being run.
The local configuration will determine how the script sets up the database,
particularly with regard to desktop vs. server mode.</p>
</section>
</section>
<section id="pgadmin-core">
<h2>pgAdmin Core<a class="headerlink" href="#pgadmin-core" title="Link to this heading">¶</a></h2>
<p>The heart of pgAdmin is the <strong>pgadmin</strong> package. This contains the globally
available HTML templates used by the Jinja engine, as well as any global static
files such as images, Javascript and CSS files that are used in multiple modules.</p>
<p>The work of the package is handled in it’s constructor, <strong>__init__.py</strong>. This
is responsible for setting up logging and authentication, dynamically loading
other modules, and a few other tasks.</p>
</section>
<section id="modules">
<h2>Modules<a class="headerlink" href="#modules" title="Link to this heading">¶</a></h2>
<p>Units of functionality are added to pgAdmin through the addition of modules.
Theses are Python object instance of classes, inherits the
PgAdminModule class (a Flask Blueprint implementation), found in
<strong>web/pgadmin/utils.py</strong>. It provide various hook points for other modules
to utilise (primarily the default module - the browser).</p>
<p>To be recognised as a module, a Python package must be created. This must:</p>
<ol class="arabic simple">
<li><p>Be placed within the <strong>web/pgadmin/</strong> directory, and</p></li>
<li><p>Implements pgadmin.utils.PgAdminModule class</p></li>
<li><p>An instance variable (generally - named <strong>blueprint</strong>) representing that
particular class in that package.</p></li>
</ol>
<p>Each module may define a <strong>template</strong> and <strong>static</strong> directory for the Blueprint
that it implements. To avoid name collisions, templates should be stored under
a directory within the specified template directory, named after the module itself.
For example, the <strong>browser</strong> module stores it’s templates in
<strong>web/pgadmin/browser/templates/browser/</strong>. This does not apply to static files
which may omit the second module name.</p>
<p>In addition to defining the Blueprint, the <strong>views</strong> module is typically
responsible for defining all the views that will be rendered in response to
client requests, we must provide a REST API url(s) for these views. These must
include appropriate route and security decorators. Take a look at the NodeView
class, which uses the same approach as Flask’s MethodView, it can be found in
<strong>web/pgadmin/browser/utils.py</strong>. This specific class is used by browser nodes
for creating REST API url(s) for different operation on them. i.e. list, create,
update, delete, fetch children, get
statistics/reversed SQL/dependencies/dependents list for that node, etc. We can
use the same class for other purpose too. You just need to inherit that class,
and overload the member variables operations, parent_ids, ids, node_type, and
then register it as node view with PgAdminModule instance.</p>
<p>Most pgAdmin modules will also implement the <strong>hooks</strong> provided by the
PgAdminModule class. This is responsible for providing hook points to integrate
the module into the rest of the application - for example, a hook might tell
the caller what CSS files need to be included on the rendered page, or what menu
options to include and what they should do. Hook points need not exist if they
are not required. It is the responsibility of the caller to ensure they are
present before attempting to utilise them.</p>
<p>Hooks currently implemented are:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">MyModule</span><span class="p">(</span><span class="n">PgAdminModule</span><span class="p">):</span>
<span class="w"> </span><span class="sd">"""</span>
<span class="sd"> This is class implements the pgadmin.utils.PgAdminModule, and</span>
<span class="sd"> implements the hooks</span>
<span class="sd"> """</span>
<span class="o">...</span>
<span class="k">def</span> <span class="nf">get_own_stylesheets</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="w"> </span><span class="sd">"""</span>
<span class="sd"> Returns:</span>
<span class="sd"> list: the stylesheets used by this module, not including any</span>
<span class="sd"> stylesheet needed by the submodules.</span>
<span class="sd"> """</span>
<span class="k">return</span> <span class="p">[</span><span class="n">url_for</span><span class="p">(</span><span class="s1">'static'</span><span class="p">,</span> <span class="s1">'css/mymodule.css'</span><span class="p">)]</span>
<span class="k">def</span> <span class="nf">get_own_javascripts</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="w"> </span><span class="sd">"""</span>
<span class="sd"> Returns:</span>
<span class="sd"> list of dict:</span>
<span class="sd"> - contains the name (representation for this javascript</span>
<span class="sd"> module), path (url for it without .js suffix), deps (array of</span>
<span class="sd"> dependents), exports window object by the javascript module,</span>
<span class="sd"> and when (would you like to load this javascript), etc</span>
<span class="sd"> information for this module, not including any script needed</span>
<span class="sd"> by submodules.</span>
<span class="sd"> """</span>
<span class="k">return</span> <span class="p">[</span>
<span class="p">{</span>
<span class="s1">'name'</span><span class="p">:</span> <span class="s1">'pgadmin.extension.mymodule'</span><span class="p">,</span>
<span class="s1">'path'</span><span class="p">:</span> <span class="n">url_for</span><span class="p">(</span><span class="s1">'static'</span><span class="p">,</span> <span class="n">filename</span><span class="o">=</span><span class="s1">'js/mymodule'</span><span class="p">),</span>
<span class="s1">'exports'</span><span class="p">:</span> <span class="kc">None</span><span class="p">,</span>
<span class="s1">'when'</span><span class="p">:</span> <span class="s1">'server'</span>
<span class="p">}</span>
<span class="p">]</span>
<span class="k">def</span> <span class="nf">get_own_menuitems</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="w"> </span><span class="sd">"""</span>
<span class="sd"> Returns:</span>
<span class="sd"> dict: the menuitems for this module, not including</span>
<span class="sd"> any needed from the submodules.</span>
<span class="sd"> """</span>
<span class="k">return</span> <span class="p">{</span>
<span class="s1">'help_items'</span><span class="p">:</span> <span class="p">[</span>
<span class="n">MenuItem</span><span class="p">(</span>
<span class="n">name</span><span class="o">=</span><span class="s1">'mnu_mymodule_help'</span><span class="p">,</span>
<span class="n">priority</span><span class="o">=</span><span class="mi">999</span><span class="p">,</span>
<span class="c1"># We need to create javascript, which registers itself</span>
<span class="c1"># as module</span>
<span class="n">module</span><span class="o">=</span><span class="s2">"pgAdmin.MyModule"</span><span class="p">,</span>
<span class="n">callback</span><span class="o">=</span><span class="s1">'about_show'</span><span class="p">,</span>
<span class="n">icon</span><span class="o">=</span><span class="s1">'fa fa-info-circle'</span><span class="p">,</span>
<span class="n">label</span><span class="o">=</span><span class="n">gettext</span><span class="p">(</span><span class="s1">'About MyModule'</span>
<span class="p">)</span>
<span class="p">]</span>
<span class="p">}</span>
<span class="k">def</span> <span class="nf">get_panels</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="w"> </span><span class="sd">"""</span>
<span class="sd"> Returns:</span>
<span class="sd"> list: a list of panel objects to add implemented in javascript</span>
<span class="sd"> module</span>
<span class="sd"> """</span>
<span class="k">return</span> <span class="p">[]</span>
<span class="o">...</span>
<span class="n">blueprint</span> <span class="o">=</span> <span class="n">MyModule</span><span class="p">(</span><span class="s1">'mymodule'</span><span class="p">,</span> <span class="vm">__name__</span><span class="p">,</span> <span class="n">static_url_path</span><span class="o">=</span><span class="s1">'/static'</span><span class="p">)</span>
</pre></div>
</div>
<p>pgAdmin Modules may include any additional Python modules that are required to
fulfill their purpose, as required. They may also reference other dynamically
loaded modules, but must use the defined hook points and fail gracefully in the
event that a particular module is not present.</p>
</section>
<section id="nodes">
<h2>Nodes<a class="headerlink" href="#nodes" title="Link to this heading">¶</a></h2>
<p>Nodes are very similar to modules, it represents an individual node or,
collection object on the object explorer treeview. To recognised as a node module, a
Python package (along with javascript modules) must be created. This must:</p>
<ol class="arabic simple">
<li><p>Be placed within the <strong>web/pgadmin/browser/</strong> directory, and</p></li>
<li><p>Implements the BrowserPluginModule, and registers the node view, which
exposes required the REST APIs</p></li>
<li><p>An instance of the class object</p></li>
</ol>
</section>
<section id="front-end">
<h2>Front End<a class="headerlink" href="#front-end" title="Link to this heading">¶</a></h2>
<p>pgAdmin uses javascript extensively for the front-end implementation. It uses
require.js to allow the lazy loading (or, say load only when required),
ReactJS with CSS and MaterialUI for UI look and feel. We have
divided each module in small chunks as much as possible. Not all javascript
modules are required to be loaded (i.e. loading a javascript module for
database will make sense only when a server node is loaded completely.) Please
look at the javascript files node.js, browser.js, menu.js, panel.js, etc for
better understanding of the code.</p>
</section>
</section>
<div class="clearer"></div>
</div>
</div>
</div>
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
<div class="sphinxsidebarwrapper">
<div>
<h3><a href="index.html">Table of Contents</a></h3>
<ul>
<li><a class="reference internal" href="#">Code Overview</a><ul>
<li><a class="reference internal" href="#runtime">Runtime</a></li>
<li><a class="reference internal" href="#web-application">Web Application</a><ul>
<li><a class="reference internal" href="#configuration">Configuration</a></li>
<li><a class="reference internal" href="#user-settings">User Settings</a></li>
</ul>
</li>
<li><a class="reference internal" href="#pgadmin-core">pgAdmin Core</a></li>
<li><a class="reference internal" href="#modules">Modules</a></li>
<li><a class="reference internal" href="#nodes">Nodes</a></li>
<li><a class="reference internal" href="#front-end">Front End</a></li>
</ul>
</li>
</ul>
</div>
<h3><a href="index.html">Table of Contents</a></h3>
<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="getting_started.html">Getting Started</a></li>
<li class="toctree-l1"><a class="reference internal" href="external_database.html">External database for pgAdmin user settings</a></li>
<li class="toctree-l1"><a class="reference internal" href="connecting.html">Connecting To A Server</a></li>
<li class="toctree-l1"><a class="reference internal" href="managing_cluster_objects.html">Managing Cluster Objects</a></li>
<li class="toctree-l1"><a class="reference internal" href="managing_database_objects.html">Managing Database Objects</a></li>
<li class="toctree-l1"><a class="reference internal" href="modifying_tables.html">Creating or Modifying a Table</a></li>
<li class="toctree-l1"><a class="reference internal" href="management_basics.html">Management Basics</a></li>
<li class="toctree-l1"><a class="reference internal" href="backup_and_restore.html">Backup and Restore</a></li>
<li class="toctree-l1"><a class="reference internal" href="developer_tools.html">Developer Tools</a></li>
<li class="toctree-l1"><a class="reference internal" href="processes.html">Processes</a></li>
<li class="toctree-l1"><a class="reference internal" href="pgagent.html">pgAgent</a></li>
<li class="toctree-l1 current"><a class="reference internal" href="contributions.html">pgAdmin Project Contributions</a><ul class="current">
<li class="toctree-l2"><a class="reference internal" href="submitting_pull_requests.html">Submitting Pull Requests</a></li>
<li class="toctree-l2 current"><a class="current reference internal" href="#">Code Overview</a></li>
<li class="toctree-l2"><a class="reference internal" href="coding_standards.html">Coding Standards</a></li>
<li class="toctree-l2"><a class="reference internal" href="code_snippets.html">Code Snippets</a></li>
<li class="toctree-l2"><a class="reference internal" href="code_review.html">Code Review Notes</a></li>
<li class="toctree-l2"><a class="reference internal" href="translations.html">Translations</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="release_notes.html">Release Notes</a></li>
<li class="toctree-l1"><a class="reference internal" href="licence.html">Licence</a></li>
</ul>
<search id="searchbox" style="display: none" role="search">
<h3 id="searchlabel">Quick search</h3>
<div class="searchformwrapper">
<form class="search" action="search.html" method="get">
<input type="text" name="q" aria-labelledby="searchlabel" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"/>
<input type="submit" value="Go" />
</form>
</div>
</search>
<script>document.getElementById('searchbox').style.display = "block"</script>
</div>
<div id="sidebarbutton" title="Collapse sidebar">
<span>«</span>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="genindex.html" title="General Index"
>index</a></li>
<li class="right" >
<a href="coding_standards.html" title="Coding Standards"
>next</a> |</li>
<li class="right" >
<a href="submitting_pull_requests.html" title="Submitting Pull Requests"
>previous</a> |</li>
<li class="nav-item nav-item-0"><a href="index.html">pgAdmin 4 8.6 documentation</a> »</li>
<li class="nav-item nav-item-1"><a href="contributions.html" >pgAdmin Project Contributions</a> »</li>
<li class="nav-item nav-item-this"><a href="">Code Overview</a></li>
</ul>
</div>
<div class="footer" role="contentinfo">
<div class="related" role="navigation" aria-label="related navigation">
<ul>
<li class="left" style="margin-left: 10px">© Copyright (C) 2013 - 2024, The pgAdmin Development Team.</li>
<li class="right" style="margin-right: 10px"><a href="genindex.html" title="General Index" accesskey="I">index</a></li>
<li class="right" ><a href="coding_standards.html" title="Coding Standards" accesskey="N">next</a> |</li>
<li class="right" ><a href="submitting_pull_requests.html" title="Submitting Pull Requests" accesskey="P">previous</a> |</li>
</ul>
</div>
</div>
</body>
</html> |