fix(webapi): resources no longer encoded twice
This commit is contained in:
parent
3b40baf64b
commit
413bc29ec4
@ -49,7 +49,8 @@ def create_app(test_config=None):
|
|||||||
app.add_url_rule('/', endpoint='index')
|
app.add_url_rule('/', endpoint='index')
|
||||||
|
|
||||||
from . import webapi
|
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)
|
api.init_app(app)
|
||||||
|
|
||||||
# TODO create OUs
|
# TODO create OUs
|
||||||
|
@ -20,7 +20,7 @@
|
|||||||
{% for user in non_members %}
|
{% for user in non_members %}
|
||||||
<tr class="userEntry text-center" id="{{ user.username }}">
|
<tr class="userEntry text-center" id="{{ user.username }}">
|
||||||
<th scope="row">
|
<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 }}"
|
alt="Profile picture for user {{ user.username }}"
|
||||||
class="img-fluid rounded"
|
class="img-fluid rounded"
|
||||||
style="max-width: 50px"
|
style="max-width: 50px"
|
||||||
@ -50,7 +50,7 @@
|
|||||||
{% for user in members %}
|
{% for user in members %}
|
||||||
<tr class="userEntry text-center" id="{{ user.username }}">
|
<tr class="userEntry text-center" id="{{ user.username }}">
|
||||||
<th scope="row">
|
<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 }}"
|
alt="Profile picture for user {{ user.username }}"
|
||||||
class="img-fluid rounded"
|
class="img-fluid rounded"
|
||||||
style="max-width: 50px"
|
style="max-width: 50px"
|
||||||
|
@ -27,7 +27,7 @@
|
|||||||
{% for user in users | sort %}
|
{% for user in users | sort %}
|
||||||
<tr>
|
<tr>
|
||||||
<th scope="row">
|
<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 }}"
|
alt="profile picture for user {{ user.username }}"
|
||||||
class="img-fluid rounded"
|
class="img-fluid rounded"
|
||||||
style="max-width: 100px"
|
style="max-width: 100px"
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
{% block content %}
|
{% block content %}
|
||||||
<div class="row justify-content-sm-center">
|
<div class="row justify-content-sm-center">
|
||||||
<div class="col-sm-2 text-center align-self-center m-2">
|
<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 }}"
|
alt="profile picture for user {{ user.username }}"
|
||||||
class="img-thumbnail"
|
class="img-thumbnail"
|
||||||
style="max-width: 150px"
|
style="max-width: 150px"
|
||||||
|
@ -388,6 +388,8 @@ class User:
|
|||||||
path_to_full_image = path_to_image_folder / "full.jpg"
|
path_to_full_image = path_to_image_folder / "full.jpg"
|
||||||
path_to_thumbnail = path_to_image_folder / "thumbnail.jpg"
|
path_to_thumbnail = path_to_image_folder / "thumbnail.jpg"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if not path_to_image_folder.is_dir():
|
if not path_to_image_folder.is_dir():
|
||||||
path_to_image_folder.mkdir(parents=True)
|
path_to_image_folder.mkdir(parents=True)
|
||||||
|
|
||||||
@ -416,6 +418,18 @@ class User:
|
|||||||
return "uid=" + self.username + ',' + current_app.config['LDAP_USERS_OU']
|
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):
|
def __eq__(self, other):
|
||||||
return self.username == other.username
|
return self.username == other.username
|
||||||
|
|
||||||
|
@ -1,11 +1,76 @@
|
|||||||
|
from json import JSONEncoder, JSONDecoder, loads, dumps
|
||||||
|
|
||||||
from flask import Blueprint
|
from flask import Blueprint
|
||||||
from flask_restful import Resource
|
from flask_restful import Resource
|
||||||
|
|
||||||
|
|
||||||
import lumi2.ldap as ldap
|
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):
|
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],
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user