Commit 6e6eb03c authored by Louis's avatar Louis
Browse files

Add form if generated from web server

parent 9bf0cbca
......@@ -5,9 +5,9 @@
- Core
- [ ] Display "Listening to http://localhost:PORT"
- [ ] localhost:PORT/index.html shows a form with text entries for pkg, usr, column if no valid package is given
- [ ] Add an argument to Renderer to show the default data to put in form
- [ ] If this argument is None, do not show forms
- [x] localhost:PORT/index.html shows a form with text entries for pkg, usr, column if no valid package is given
- [x] Add an argument to Renderer to show the default data to put in form
- [x] If this argument is None, do not show forms
- [ ] Gather errors
- Project management
......
#!/usr/bin/env python3
import collections
import colorsys
import datetime
import functools
......@@ -54,7 +53,7 @@ def iter_user_packages(user):
class Renderer:
def __init__(self, config):
def __init__(self, config, form=None):
# Processing config
self.config = config
if "columns" not in self.config:
......@@ -80,6 +79,12 @@ class Renderer:
sys.stderr.write("Ignoring package '{}': {}.\n".format(name, error))
continue
# Processing form
self.form = form
if self.form is not None:
for key in ['pkg', 'user', 'col']:
self.form[key] = ",".join(set(itertools.chain(*[item.split(",") for item in self.form.get(key, [])])))
# Loading plugins
self.columns = {
plugin.keyword: plugin(self)
......@@ -88,12 +93,12 @@ class Renderer:
}
@classmethod
def from_yaml(cls, filename):
def from_yaml(cls, filename, form=None):
with open(filename, 'r') as stream:
return cls(yaml.safe_load(stream.read()))
return cls(yaml.safe_load(stream.read()), form=form)
@classmethod
def from_args(cls, packages=None, columns=None, users=None):
def from_args(cls, packages=None, columns=None, users=None, form=None):
config = {}
# Processing packages
......@@ -113,7 +118,7 @@ class Renderer:
if columns:
config['columns'] = columns
return cls(config)
return cls(config, form=form)
def __iter__(self):
yield from self.packages
......@@ -145,6 +150,7 @@ class Renderer:
config=self.config,
columns=self.columns,
packages=self.packages,
form=self.form,
)
......
......@@ -9,41 +9,42 @@
<script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
</head>
<body>
<!-- TODO Show only if necessary -->
<style>
/* Tooltip container */
.tooltip {
position: relative;
display: inline-block;
border-bottom: 1px dotted black; /* If you want dots under the hoverable text */
}
{% if form != none %}
<style>
/* Tooltip container */
.tooltip {
position: relative;
display: inline-block;
border-bottom: 1px dotted black; /* If you want dots under the hoverable text */
}
/* Tooltip text */
.tooltip .tooltiptext {
visibility: hidden;
width: 120px;
background-color: black;
color: #fff;
text-align: center;
padding: 5px 0;
border-radius: 6px;
/* Tooltip text */
.tooltip .tooltiptext {
visibility: hidden;
width: 120px;
background-color: black;
color: #fff;
text-align: center;
padding: 5px 0;
border-radius: 6px;
/* Position the tooltip text - see examples below! */
position: absolute;
z-index: 1;
}
/* Position the tooltip text - see examples below! */
position: absolute;
z-index: 1;
}
/* Show the tooltip text when you mouse over the tooltip container */
.tooltip:hover .tooltiptext {
visibility: visible;
}
</style>
<form action=".." method="get"> <!-- TODO Parent -->
<span class="tooltip">Packages <div class="tooltiptext">Comma separated list of packages to monitor.</div></span>: <input type="text" name="pkg"> <!-- TODO Prefill -->
<span class="tooltip">Users <div class="tooltiptext">Comma separated list of users to monitor.</div></span>: <input type="text" name="user"> <!-- TODO Prefill -->
<span class="tooltip">Columns <div class="tooltiptext">Comma separated list of columns to display.</div></span>: <input type="text" name="col"> <!-- TODO Prefill -->
<input type="submit" value="Submit">
</form>
/* Show the tooltip text when you mouse over the tooltip container */
.tooltip:hover .tooltiptext {
visibility: visible;
}
</style>
<form action="{{ form.index }}" method="get">
<span class="tooltip">Packages <div class="tooltiptext">Comma separated list of packages to monitor.</div></span>: <input type="text" name="pkg" value="{{ form.pkg }}">
<span class="tooltip">Users <div class="tooltiptext">Comma separated list of users to monitor.</div></span>: <input type="text" name="user" value="{{ form.user }}">
<span class="tooltip">Columns <div class="tooltiptext">Comma separated list of columns to display.</div></span>: <input type="text" name="col" value="{{ form.col }}">
<input type="submit" value="Submit">
</form>
{% endif %}
{{ linecharts.plot(packages.values()) }}
......
......@@ -37,12 +37,21 @@ class HttpHandler(http.server.SimpleHTTPRequestHandler):
packages=querydict.get('pkg'),
columns=querydict.get('col', ["homepage", "pypiversion", "pypimdownloads", "pypiwdownloads", "pypiddownloads"]),
users=querydict.get('user'),
form={
'pkg': querydict.get('pkg', []),
'col': querydict.get('col', []),
'user': querydict.get('user', []),
'index': '',
}
).render(), "utf-8"))
elif self._find_yaml(path):
self.send_response(200)
self.send_header("Content-type", "text/html")
self.end_headers()
self.wfile.write(bytes(pypimonitor.Renderer.from_yaml(self._find_yaml(path)).render(), "utf-8"))
self.wfile.write(bytes(pypimonitor.Renderer.from_yaml(
self._find_yaml(path),
form={'index': "/".join(".."*len(path.split("/")))},
).render(), "utf-8"))
else:
super().do_GET()
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment