API & Client - remove id in user data

This commit is contained in:
Sam 2020-02-08 14:49:37 +01:00
parent 2c3bc0f9bc
commit 9f487a4d68
16 changed files with 50 additions and 50 deletions

View File

@ -6,7 +6,7 @@ Authentication
auth.register_user,
auth.login_user,
auth.logout_user,
auth.get_user_status,
auth.get_authenticated_user_profile,
auth.edit_user,
auth.edit_picture,
auth.del_picture

View File

@ -316,7 +316,6 @@
<span class="nt">&quot;created_at&quot;</span><span class="p">:</span> <span class="s2">&quot;Sun, 14 Jul 2019 14:09:58 GMT&quot;</span><span class="p">,</span>
<span class="nt">&quot;email&quot;</span><span class="p">:</span> <span class="s2">&quot;sam@example.com&quot;</span><span class="p">,</span>
<span class="nt">&quot;first_name&quot;</span><span class="p">:</span> <span class="kc">null</span><span class="p">,</span>
<span class="nt">&quot;id&quot;</span><span class="p">:</span> <span class="mi">2</span><span class="p">,</span>
<span class="nt">&quot;language&quot;</span><span class="p">:</span> <span class="s2">&quot;en&quot;</span><span class="p">,</span>
<span class="nt">&quot;last_name&quot;</span><span class="p">:</span> <span class="kc">null</span><span class="p">,</span>
<span class="nt">&quot;location&quot;</span><span class="p">:</span> <span class="kc">null</span><span class="p">,</span>
@ -379,7 +378,6 @@
<span class="nt">&quot;created_at&quot;</span><span class="p">:</span> <span class="s2">&quot;Sun, 14 Jul 2019 14:09:58 GMT&quot;</span><span class="p">,</span>
<span class="nt">&quot;email&quot;</span><span class="p">:</span> <span class="s2">&quot;sam@example.com&quot;</span><span class="p">,</span>
<span class="nt">&quot;first_name&quot;</span><span class="p">:</span> <span class="kc">null</span><span class="p">,</span>
<span class="nt">&quot;id&quot;</span><span class="p">:</span> <span class="mi">2</span><span class="p">,</span>
<span class="nt">&quot;language&quot;</span><span class="p">:</span> <span class="s2">&quot;en&quot;</span><span class="p">,</span>
<span class="nt">&quot;last_name&quot;</span><span class="p">:</span> <span class="kc">null</span><span class="p">,</span>
<span class="nt">&quot;location&quot;</span><span class="p">:</span> <span class="kc">null</span><span class="p">,</span>

View File

@ -148,7 +148,6 @@
<span class="nt">&quot;created_at&quot;</span><span class="p">:</span> <span class="s2">&quot;Sun, 14 Jul 2019 14:09:58 GMT&quot;</span><span class="p">,</span>
<span class="nt">&quot;email&quot;</span><span class="p">:</span> <span class="s2">&quot;admin@example.com&quot;</span><span class="p">,</span>
<span class="nt">&quot;first_name&quot;</span><span class="p">:</span> <span class="kc">null</span><span class="p">,</span>
<span class="nt">&quot;id&quot;</span><span class="p">:</span> <span class="mi">1</span><span class="p">,</span>
<span class="nt">&quot;language&quot;</span><span class="p">:</span> <span class="s2">&quot;en&quot;</span><span class="p">,</span>
<span class="nt">&quot;last_name&quot;</span><span class="p">:</span> <span class="kc">null</span><span class="p">,</span>
<span class="nt">&quot;location&quot;</span><span class="p">:</span> <span class="kc">null</span><span class="p">,</span>
@ -172,7 +171,6 @@
<span class="nt">&quot;created_at&quot;</span><span class="p">:</span> <span class="s2">&quot;Sat, 20 Jul 2019 11:27:03 GMT&quot;</span><span class="p">,</span>
<span class="nt">&quot;email&quot;</span><span class="p">:</span> <span class="s2">&quot;sam@example.com&quot;</span><span class="p">,</span>
<span class="nt">&quot;first_name&quot;</span><span class="p">:</span> <span class="kc">null</span><span class="p">,</span>
<span class="nt">&quot;id&quot;</span><span class="p">:</span> <span class="mi">2</span><span class="p">,</span>
<span class="nt">&quot;language&quot;</span><span class="p">:</span> <span class="s2">&quot;fr&quot;</span><span class="p">,</span>
<span class="nt">&quot;last_name&quot;</span><span class="p">:</span> <span class="kc">null</span><span class="p">,</span>
<span class="nt">&quot;location&quot;</span><span class="p">:</span> <span class="kc">null</span><span class="p">,</span>
@ -221,7 +219,7 @@
<code class="sig-name descname">GET </code><code class="sig-name descname">/api/users/</code><span class="sig-paren">(</span><em class="sig-param">user_name</em><span class="sig-paren">)</span><a class="headerlink" href="#get--api-users-(user_name)" title="Permalink to this definition"></a></dt>
<dd><p>Get single user details</p>
<p><strong>Example request</strong>:</p>
<div class="highlight-http notranslate"><div class="highlight"><pre><span></span><span class="nf">GET</span> <span class="nn">/api/users/1</span> <span class="kr">HTTP</span><span class="o">/</span><span class="m">1.1</span>
<div class="highlight-http notranslate"><div class="highlight"><pre><span></span><span class="nf">GET</span> <span class="nn">/api/users/admin</span> <span class="kr">HTTP</span><span class="o">/</span><span class="m">1.1</span>
<span class="na">Content-Type</span><span class="o">:</span> <span class="l">application/json</span>
</pre></div>
</div>
@ -238,7 +236,6 @@
<span class="nt">&quot;created_at&quot;</span><span class="p">:</span> <span class="s2">&quot;Sun, 14 Jul 2019 14:09:58 GMT&quot;</span><span class="p">,</span>
<span class="nt">&quot;email&quot;</span><span class="p">:</span> <span class="s2">&quot;admin@example.com&quot;</span><span class="p">,</span>
<span class="nt">&quot;first_name&quot;</span><span class="p">:</span> <span class="kc">null</span><span class="p">,</span>
<span class="nt">&quot;id&quot;</span><span class="p">:</span> <span class="mi">1</span><span class="p">,</span>
<span class="nt">&quot;language&quot;</span><span class="p">:</span> <span class="s2">&quot;en&quot;</span><span class="p">,</span>
<span class="nt">&quot;last_name&quot;</span><span class="p">:</span> <span class="kc">null</span><span class="p">,</span>
<span class="nt">&quot;location&quot;</span><span class="p">:</span> <span class="kc">null</span><span class="p">,</span>
@ -291,11 +288,11 @@
</dd></dl>
<dl class="get">
<dt id="get--api-users-(user_id)-picture">
<code class="sig-name descname">GET </code><code class="sig-name descname">/api/users/</code><span class="sig-paren">(</span><em class="sig-param">user_id</em><span class="sig-paren">)</span><code class="sig-name descname">/picture</code><a class="headerlink" href="#get--api-users-(user_id)-picture" title="Permalink to this definition"></a></dt>
<dt id="get--api-users-(user_name)-picture">
<code class="sig-name descname">GET </code><code class="sig-name descname">/api/users/</code><span class="sig-paren">(</span><em class="sig-param">user_name</em><span class="sig-paren">)</span><code class="sig-name descname">/picture</code><a class="headerlink" href="#get--api-users-(user_name)-picture" title="Permalink to this definition"></a></dt>
<dd><p>get user picture</p>
<p><strong>Example request</strong>:</p>
<div class="highlight-http notranslate"><div class="highlight"><pre><span></span><span class="nf">GET</span> <span class="nn">/api/users/1/picture</span> <span class="kr">HTTP</span><span class="o">/</span><span class="m">1.1</span>
<div class="highlight-http notranslate"><div class="highlight"><pre><span></span><span class="nf">GET</span> <span class="nn">/api/users/admin/picture</span> <span class="kr">HTTP</span><span class="o">/</span><span class="m">1.1</span>
<span class="na">Content-Type</span><span class="o">:</span> <span class="l">application/json</span>
</pre></div>
</div>
@ -307,7 +304,7 @@
<dl class="field-list simple">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><ul class="simple">
<li><p><strong>user_id</strong> (<em>integer</em>) user id</p></li>
<li><p><strong>user_name</strong> (<em>integer</em>) user name</p></li>
</ul>
</dd>
<dt class="field-even">Status Codes</dt>

View File

@ -217,12 +217,12 @@
<tr>
<td></td>
<td>
<a href="api/users.html#get--api-users-(user_id)-picture"><code class="xref">GET /api/users/(user_id)/picture</code></a></td><td>
<a href="api/users.html#get--api-users-(user_name)"><code class="xref">GET /api/users/(user_name)</code></a></td><td>
<em></em></td></tr>
<tr>
<td></td>
<td>
<a href="api/users.html#get--api-users-(user_name)"><code class="xref">GET /api/users/(user_name)</code></a></td><td>
<a href="api/users.html#get--api-users-(user_name)-picture"><code class="xref">GET /api/users/(user_name)/picture</code></a></td><td>
<em></em></td></tr>
<tr>
<td></td>

Binary file not shown.

File diff suppressed because one or more lines are too long

View File

@ -6,7 +6,7 @@ Authentication
auth.register_user,
auth.login_user,
auth.logout_user,
auth.get_user_status,
auth.get_authenticated_user_profile,
auth.edit_user,
auth.edit_picture,
auth.del_picture

View File

@ -261,7 +261,7 @@ def test_decode_auth_token(app, user_1):
def test_user_no_picture(app, user_1):
client = app.test_client()
response = client.get('/api/users/1/picture')
response = client.get(f'/api/users/{user_1.username}/picture')
data = json.loads(response.data.decode())
assert response.status_code == 404
@ -271,7 +271,7 @@ def test_user_no_picture(app, user_1):
def test_user_picture_no_user(app, user_1):
client = app.test_client()
response = client.get('/api/users/2/picture')
response = client.get('/api/users/not_existing/picture')
data = json.loads(response.data.decode())
assert response.status_code == 404

View File

@ -2,7 +2,6 @@ def test_user_model(app, user_1):
assert '<User \'test\'>' == str(user_1)
serialized_user = user_1.serialize()
assert 1 == serialized_user['id']
assert 'test' == serialized_user['username']
assert 'created_at' in serialized_user
assert serialized_user['admin'] is False

View File

@ -237,7 +237,7 @@ def login_user():
@auth_blueprint.route('/auth/logout', methods=['GET'])
@authenticate
def logout_user(user_id):
def logout_user(auth_user_id):
"""
user logout
@ -285,7 +285,7 @@ def logout_user(user_id):
if auth_header:
auth_token = auth_header.split(" ")[1]
resp = User.decode_auth_token(auth_token)
if not isinstance(user_id, str):
if not isinstance(auth_user_id, str):
response_object = {
'status': 'success',
'message': 'Successfully logged out.',
@ -304,7 +304,7 @@ def logout_user(user_id):
@auth_blueprint.route('/auth/profile', methods=['GET'])
@authenticate
def get_user_status(user_id):
def get_authenticated_user_profile(auth_user_id):
"""
get authenticated user info
@ -330,7 +330,6 @@ def get_user_status(user_id):
"created_at": "Sun, 14 Jul 2019 14:09:58 GMT",
"email": "sam@example.com",
"first_name": null,
"id": 2,
"language": "en",
"last_name": null,
"location": null,
@ -360,14 +359,14 @@ def get_user_status(user_id):
- Invalid token. Please log in again.
"""
user = User.query.filter_by(id=user_id).first()
user = User.query.filter_by(id=auth_user_id).first()
response_object = {'status': 'success', 'data': user.serialize()}
return jsonify(response_object), 200
@auth_blueprint.route('/auth/profile/edit', methods=['POST'])
@authenticate
def edit_user(user_id):
def edit_user(auth_user_id):
"""
edit authenticated user
@ -393,7 +392,6 @@ def edit_user(user_id):
"created_at": "Sun, 14 Jul 2019 14:09:58 GMT",
"email": "sam@example.com",
"first_name": null,
"id": 2,
"language": "en",
"last_name": null,
"location": null,
@ -476,7 +474,7 @@ def edit_user(user_id):
).decode()
try:
user = User.query.filter_by(id=user_id).first()
user = User.query.filter_by(id=auth_user_id).first()
user.first_name = first_name
user.last_name = last_name
user.bio = bio
@ -513,7 +511,7 @@ def edit_user(user_id):
@auth_blueprint.route('/auth/picture', methods=['POST'])
@authenticate
def edit_picture(user_id):
def edit_picture(auth_user_id):
"""
update authenticated user picture
@ -573,15 +571,17 @@ def edit_picture(user_id):
file = request.files['file']
filename = secure_filename(file.filename)
dirpath = os.path.join(
current_app.config['UPLOAD_FOLDER'], 'pictures', str(user_id)
current_app.config['UPLOAD_FOLDER'], 'pictures', str(auth_user_id)
)
if not os.path.exists(dirpath):
os.makedirs(dirpath)
absolute_picture_path = os.path.join(dirpath, filename)
relative_picture_path = os.path.join('pictures', str(user_id), filename)
relative_picture_path = os.path.join(
'pictures', str(auth_user_id), filename
)
try:
user = User.query.filter_by(id=user_id).first()
user = User.query.filter_by(id=auth_user_id).first()
if user.picture is not None:
old_picture_path = get_absolute_file_path(user.picture)
if os.path.isfile(get_absolute_file_path(old_picture_path)):
@ -608,7 +608,7 @@ def edit_picture(user_id):
@auth_blueprint.route('/auth/picture', methods=['DELETE'])
@authenticate
def del_picture(user_id):
def del_picture(auth_user_id):
"""
delete authenticated user picture
@ -637,7 +637,7 @@ def del_picture(user_id):
"""
try:
user = User.query.filter_by(id=user_id).first()
user = User.query.filter_by(id=auth_user_id).first()
picture_path = get_absolute_file_path(user.picture)
if os.path.isfile(picture_path):
os.remove(picture_path)

View File

@ -110,7 +110,6 @@ class User(db.Model):
.first()
)
return {
'id': self.id,
'username': self.username,
'email': self.email,
'created_at': self.created_at,

View File

@ -37,7 +37,6 @@ def get_users(auth_user_id):
"created_at": "Sun, 14 Jul 2019 14:09:58 GMT",
"email": "admin@example.com",
"first_name": null,
"id": 1,
"language": "en",
"last_name": null,
"location": null,
@ -61,7 +60,6 @@ def get_users(auth_user_id):
"created_at": "Sat, 20 Jul 2019 11:27:03 GMT",
"email": "sam@example.com",
"first_name": null,
"id": 2,
"language": "fr",
"last_name": null,
"location": null,
@ -108,7 +106,7 @@ def get_single_user(auth_user_id, user_name):
.. sourcecode:: http
GET /api/users/1 HTTP/1.1
GET /api/users/admin HTTP/1.1
Content-Type: application/json
**Example response**:
@ -127,7 +125,6 @@ def get_single_user(auth_user_id, user_name):
"created_at": "Sun, 14 Jul 2019 14:09:58 GMT",
"email": "admin@example.com",
"first_name": null,
"id": 1,
"language": "en",
"last_name": null,
"location": null,
@ -177,15 +174,15 @@ def get_single_user(auth_user_id, user_name):
return jsonify(response_object), 404
@users_blueprint.route('/users/<user_id>/picture', methods=['GET'])
def get_picture(user_id):
@users_blueprint.route('/users/<user_name>/picture', methods=['GET'])
def get_picture(user_name):
""" get user picture
**Example request**:
.. sourcecode:: http
GET /api/users/1/picture HTTP/1.1
GET /api/users/admin/picture HTTP/1.1
Content-Type: application/json
**Example response**:
@ -195,7 +192,7 @@ def get_picture(user_id):
HTTP/1.1 200 OK
Content-Type: image/jpeg
:param integer user_id: user id
:param integer user_name: user name
:statuscode 200: success
:statuscode 404:
@ -205,7 +202,7 @@ def get_picture(user_id):
"""
response_object = {'status': 'not found', 'message': 'No picture.'}
try:
user = User.query.filter_by(id=int(user_id)).first()
user = User.query.filter_by(username=user_name).first()
if not user:
response_object = {
'status': 'fail',

View File

@ -7,6 +7,7 @@ import { Link } from 'react-router-dom'
import Message from '../../Common/Message'
import { history } from '../../../index'
import { getOrUpdateData } from '../../../actions'
import { apiUrl } from '../../../utils'
class AdminUsers extends React.Component {
componentDidMount() {
@ -28,7 +29,7 @@ class AdminUsers extends React.Component {
<table className="table">
<thead>
<tr>
<th>{t('administration:id')}</th>
<th>#</th>
<th>{t('user:Username')}</th>
<th>{t('user:Email')}</th>
<th>{t('user:Registration Date')}</th>
@ -38,8 +39,18 @@ class AdminUsers extends React.Component {
</thead>
<tbody>
{users.map(user => (
<tr key={user.id}>
<th scope="row">{user.id}</th>
<tr key={user.username}>
<td>
{user.picture === true && (
<img
alt="Avatar"
src={`${apiUrl}users/${
user.username
}/picture?${Date.now()}`}
className="img-fluid App-nav-profile-img"
/>
)}
</td>
<td>
<Link to={`/users/${user.username}`}>
{user.username}

View File

@ -8,7 +8,7 @@ import { apiUrl } from '../../utils'
class NavBar extends React.PureComponent {
render() {
const { admin, id, isAuthenticated, picture, t, username } = this.props
const { admin, isAuthenticated, picture, t, username } = this.props
return (
<header>
<nav className="navbar navbar-expand-lg navbar-light bg-light">
@ -120,7 +120,7 @@ class NavBar extends React.PureComponent {
{picture === true && (
<img
alt="Avatar"
src={`${apiUrl}users/${id}/picture?${Date.now()}`}
src={`${apiUrl}users/${username}/picture?${Date.now()}`}
className="img-fluid App-nav-profile-img"
/>
)}
@ -161,7 +161,6 @@ class NavBar extends React.PureComponent {
export default withTranslation()(
connect(({ user }) => ({
admin: user.admin,
id: user.id,
isAuthenticated: user.isAuthenticated,
picture: user.picture,
username: user.username,

View File

@ -89,7 +89,7 @@ function ProfileDetail({
<img
alt="Profile"
src={
`${apiUrl}users/${user.id}/picture` +
`${apiUrl}users/${user.username}/picture` +
`?${Date.now()}`
}
className="img-fluid App-profile-img-small"

View File

@ -23,7 +23,7 @@ class UserProfile extends React.Component {
<div>
{user && (
<ProfileDetail
editable={currentUser.id === user.id}
editable={currentUser.username === user.username}
t={t}
user={user}
/>