fix(webapi): resources no longer encoded twice

This commit is contained in:
Julian Lobbes 2022-11-22 02:57:57 +01:00
parent 3b40baf64b
commit 413bc29ec4
6 changed files with 89 additions and 9 deletions

View File

@ -49,7 +49,8 @@ def create_app(test_config=None):
app.add_url_rule('/', endpoint='index')
from . import webapi
api.add_resource(webapi.User, '/api/user/<string:username>')
api.add_resource(webapi.UserResource, '/api/user/<string:username>')
api.add_resource(webapi.GroupResource, '/api/group/<string:groupname>')
api.init_app(app)
# TODO create OUs

View File

@ -20,7 +20,7 @@
{% for user in non_members %}
<tr class="userEntry text-center" id="{{ user.username }}">
<th scope="row">
<img src="{{ url_for('static', filename='images/users/' + user.username + '/thumbnail.jpg') }}"
<img src="{{ user.get_thumbnail_url() }}"
alt="Profile picture for user {{ user.username }}"
class="img-fluid rounded"
style="max-width: 50px"
@ -50,7 +50,7 @@
{% for user in members %}
<tr class="userEntry text-center" id="{{ user.username }}">
<th scope="row">
<img src="{{ url_for('static', filename='images/users/' + user.username + '/thumbnail.jpg') }}"
<img src="{{ user.get_thumbnail_url() }}"
alt="Profile picture for user {{ user.username }}"
class="img-fluid rounded"
style="max-width: 50px"

View File

@ -27,7 +27,7 @@
{% for user in users | sort %}
<tr>
<th scope="row">
<img src="{{ url_for('static', filename='images/users/' + user.username + '/thumbnail.jpg') }}"
<img src="{{ user.get_thumbnail_url() }}"
alt="profile picture for user {{ user.username }}"
class="img-fluid rounded"
style="max-width: 100px"

View File

@ -3,7 +3,7 @@
{% block content %}
<div class="row justify-content-sm-center">
<div class="col-sm-2 text-center align-self-center m-2">
<img src="{{ url_for('static', filename='images/users/' + user.username + '/full.jpg') }}"
<img src="{{ user.get_picture_url() }}"
alt="profile picture for user {{ user.username }}"
class="img-thumbnail"
style="max-width: 150px"

View File

@ -388,6 +388,8 @@ class User:
path_to_full_image = path_to_image_folder / "full.jpg"
path_to_thumbnail = path_to_image_folder / "thumbnail.jpg"
if not path_to_image_folder.is_dir():
path_to_image_folder.mkdir(parents=True)
@ -416,6 +418,18 @@ class User:
return "uid=" + self.username + ',' + current_app.config['LDAP_USERS_OU']
def get_picture_url(self):
"""Returns the URL to this user's static profile picture image file."""
return f'/static/images/users/{self.username}/full.jpg'
def get_thumbnail_url(self):
"""Returns the URL to this user's static profile thumbnail image file."""
return f'/static/images/users/{self.username}/thumbnail.jpg'
def __eq__(self, other):
return self.username == other.username

View File

@ -1,11 +1,76 @@
from json import JSONEncoder, JSONDecoder, loads, dumps
from flask import Blueprint
from flask_restful import Resource
import lumi2.ldap as ldap
from lumi2.usermodel import User
from lumi2.usermodel import User, Group
class User(Resource):
class UserEncoder(JSONEncoder):
def default(self, obj):
if isinstance(obj, User):
return {
"username": obj.username,
"password_hash": obj.password_hash,
"email": obj.email,
"first_name": obj.first_name,
"last_name": obj.last_name,
"display_name": obj.display_name,
"picture": obj.get_picture_url(),
}
return JSONEncoder.default(self, obj)
class GroupEncoder(JSONEncoder):
def default(self, obj):
if isinstance(obj, Group):
return {
"groupname": obj.groupname,
"members": [user.username for user in obj.members],
}
return JSONEncoder.default(self, obj)
class UserResource(Resource):
def get(self, username):
pass
try:
conn = ldap.get_connection()
except:
return 500
try:
user = ldap.get_user(conn, username)
except ldap.EntryNotFoundException:
return {"message": f"User '{username}' does not exist."}, 400
return {
"user": {
"username": user.username,
"password_hash": user.password_hash,
"email": user.email,
"first_name": user.first_name,
"last_name": user.last_name,
"display_name": user.display_name,
"picture": user.get_picture_url(),
}
}
class GroupResource(Resource):
def get(self, groupname):
try:
conn = ldap.get_connection()
except:
return 500
try:
group = ldap.get_group(conn, groupname)
except ldap.EntryNotFoundException:
return {"message": f"Group '{groupname}' does not exist."}, 400
return {
"group": {
"groupname": group.groupname,
"members": [user.username for user in group.members],
}
}