Make user add/delete/update work.

Signed-off-by: Jan Losinski <losinski@wh2.tu-dresden.de>
This commit is contained in:
Jan Losinski 2018-03-22 00:26:02 +01:00
parent 6b892de27e
commit f0448e59d3
3 changed files with 66 additions and 13 deletions

View File

@ -143,14 +143,65 @@ class IndexView(ViewBase):
return self._template({})
@aiohttp_jinja2.template("relogin.html")
def re_login(request):
pass
class ReLoginView(ViewBase):
@aiohttp_jinja2.template("relogin.html")
async def get(self):
return self._template({'action': 'update', 'description': 'Refresh'})
@aiohttp_jinja2.template("relogin.html")
async def post(self):
data = await self.request.post()
self._set_data(data)
validator = Validator(self.request.app.loop, data)
await validator.validate_credentials()
self._add_errors(validator.errors)
if validator.success:
with self._session as session:
user = session.query(models.User).filter(models.User.name == validator.username).first()
if user is None:
self._add_errors({'user': 'User not registered'})
else:
if await self._wallabag.get_token(user, validator.password):
session.commit()
self._add_message("User {user} successfully updated.".format(user=validator.username))
logger.info("User {user} successfully updated.", user=user)
else:
self._add_errors({'auth': "Authentication against wallabag server failed"})
return self._template({'action': 'update', 'description': 'Refresh'})
@aiohttp_jinja2.template("index.html")
def delete_user(request):
pass
class DeleteView(ViewBase):
@aiohttp_jinja2.template("relogin.html")
async def get(self):
return self._template({'action': 'delete', 'description': 'Delete'})
@aiohttp_jinja2.template("relogin.html")
async def post(self):
data = await self.request.post()
self._set_data(data)
validator = Validator(self.request.app.loop, data)
await validator.validate_credentials()
self._add_errors(validator.errors)
if validator.success:
with self._session as session:
user = session.query(models.User).filter(models.User.name == validator.username).first()
if user is None:
self._add_errors({'user': 'User not registered'})
else:
if await self._wallabag.get_token(user, validator.password):
session.delete(user)
session.commit()
self._add_message("User {user} successfully deleted.".format(user=validator.username))
logger.info("User {user} successfully deleted.", user=user)
else:
self._add_errors({'auth': "Authentication against wallabag server failed"})
return self._template({'action': 'delete', 'description': 'Delete'})
class App:
@ -176,6 +227,8 @@ class App:
path=os.path.join(os.path.dirname(__file__), 'static'),
name='static')
self.app.router.add_view("/", IndexView)
self.app.router.add_view("/delete", DeleteView)
self.app.router.add_view("/update", ReLoginView)
def run(self):
web.run_app(self.app, host=self.config.interface_host, port=self.config.interface_port)

View File

@ -7,7 +7,7 @@
{{ forms.messages(errors=errors, messages=messages) }}
<div class="card">
<div class="card-header">Add or edit an account</div>
<div class="card-header">Add an account</div>
<div class="card-body">
<form method="post" action="/">
<div class="row">

View File

@ -3,19 +3,19 @@
{% import 'macros.html' as forms %}
{% block body %}
{{ forms.messages(errors=errors, messages=messages) }}
<div class="card">
<div class="card-header">Fetch a new Wallabag API token</div>
<div class="card-header">{{ description }} an account</div>
<div class="card-body">
<form>
<form method="post" action="/{{ action }}">
<div class="row">
{{ forms.input('user', "Username", description='Your wallabag username at ' + wallabag_host) }}
{{ forms.input('password', "Password", type='password', description='Your wallabag password at ' + wallabag_host + ". The password will not be stored") }}
{{ forms.input('username', "Username", description='Your wallabag username at ' + wallabag_host, data=data, errors=errors) }}
{{ forms.input('password', "Password", type='password', description='Your wallabag password at ' + wallabag_host + ". The password will not be stored", data=data, errors=errors) }}
</div>
<div class="row">
<div class="col-lg">
<button type="submit" class="btn btn-primary">Register</button>
<button type="submit" class="btn btn-primary">{{ description }}</button>
</div>
</div>
</form>