Skip to content
Snippets Groups Projects
Commit f85abe62 authored by Jan Mach's avatar Jan Mach
Browse files

Implemented user membership and user managers feature into groups.

This feature is a reverse direction to the previous commit. User accounts can now be added into groups and can be designated as group managers. (Redmine issue: #3734)
parent de571bc4
No related branches found
No related tags found
No related merge requests found
...@@ -17,17 +17,21 @@ __author__ = "Jan Mach <jan.mach@cesnet.cz>" ...@@ -17,17 +17,21 @@ __author__ = "Jan Mach <jan.mach@cesnet.cz>"
__credits__ = "Pavel Kácha <pavel.kacha@cesnet.cz>, Andrea Kropáčová <andrea.kropacova@cesnet.cz>" __credits__ = "Pavel Kácha <pavel.kacha@cesnet.cz>, Andrea Kropáčová <andrea.kropacova@cesnet.cz>"
import re
import sqlalchemy import sqlalchemy
import ipranges
import flask_wtf
import wtforms import wtforms
from wtforms.ext.sqlalchemy.fields import QuerySelectMultipleField
#
# Flask related modules.
#
import flask_wtf
from flask_babel import gettext, lazy_gettext from flask_babel import gettext, lazy_gettext
#
# Custom modules.
#
import hawat.db import hawat.db
from mentat.datatype.sqldb import GroupModel from mentat.datatype.sqldb import GroupModel, UserModel
def check_name_existence(form, field): def check_name_existence(form, field):
...@@ -44,6 +48,7 @@ def check_name_existence(form, field): ...@@ -44,6 +48,7 @@ def check_name_existence(form, field):
pass pass
raise wtforms.validators.ValidationError(gettext('Group with this name already exists.')) raise wtforms.validators.ValidationError(gettext('Group with this name already exists.'))
def check_name_uniqueness(form, field): def check_name_uniqueness(form, field):
""" """
Callback for validating user logins during account update action. Callback for validating user logins during account update action.
...@@ -56,6 +61,20 @@ def check_name_uniqueness(form, field): ...@@ -56,6 +61,20 @@ def check_name_uniqueness(form, field):
return return
raise wtforms.validators.ValidationError(gettext('Group with this name already exists.')) raise wtforms.validators.ValidationError(gettext('Group with this name already exists.'))
def get_available_users():
"""
Query the database for list of all available user accounts.
"""
return hawat.db.db_query(UserModel).order_by(UserModel.fullname).all()
def format_select_option_label(item):
"""
"""
return "{} ({})".format(item.fullname, item.login)
class BaseGroupForm(flask_wtf.FlaskForm): class BaseGroupForm(flask_wtf.FlaskForm):
""" """
Class representing base group form. Class representing base group form.
...@@ -79,6 +98,7 @@ class BaseGroupForm(flask_wtf.FlaskForm): ...@@ -79,6 +98,7 @@ class BaseGroupForm(flask_wtf.FlaskForm):
lazy_gettext('Cancel') lazy_gettext('Cancel')
) )
class CreateGroupForm(BaseGroupForm): class CreateGroupForm(BaseGroupForm):
""" """
Class representing group create form. Class representing group create form.
...@@ -90,6 +110,21 @@ class CreateGroupForm(BaseGroupForm): ...@@ -90,6 +110,21 @@ class CreateGroupForm(BaseGroupForm):
check_name_existence check_name_existence
] ]
) )
members = QuerySelectMultipleField(
lazy_gettext('Members:'),
query_factory = get_available_users,
get_label = format_select_option_label,
allow_blank = True,
blank_text = lazy_gettext('<< no selection >>')
)
managers = QuerySelectMultipleField(
lazy_gettext('Managers:'),
query_factory = get_available_users,
get_label = format_select_option_label,
allow_blank = True,
blank_text = lazy_gettext('<< no selection >>')
)
class UpdateGroupForm(BaseGroupForm): class UpdateGroupForm(BaseGroupForm):
""" """
...@@ -97,6 +132,7 @@ class UpdateGroupForm(BaseGroupForm): ...@@ -97,6 +132,7 @@ class UpdateGroupForm(BaseGroupForm):
""" """
pass pass
class AdminUpdateGroupForm(BaseGroupForm): class AdminUpdateGroupForm(BaseGroupForm):
""" """
Class representing group update form for administrators. Class representing group update form for administrators.
...@@ -108,6 +144,20 @@ class AdminUpdateGroupForm(BaseGroupForm): ...@@ -108,6 +144,20 @@ class AdminUpdateGroupForm(BaseGroupForm):
check_name_uniqueness check_name_uniqueness
] ]
) )
members = QuerySelectMultipleField(
lazy_gettext('Members:'),
query_factory = get_available_users,
get_label = format_select_option_label,
allow_blank = True,
blank_text = lazy_gettext('<< no selection >>')
)
managers = QuerySelectMultipleField(
lazy_gettext('Managers:'),
query_factory = get_available_users,
get_label = format_select_option_label,
allow_blank = True,
blank_text = lazy_gettext('<< no selection >>')
)
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
......
...@@ -27,6 +27,15 @@ ...@@ -27,6 +27,15 @@
{{ macros_site.render_form_item_default(form.description) }} {{ macros_site.render_form_item_default(form.description) }}
{%- if item_action == 'create' or current_user.has_role('admin') %}
<hr>
{{ macros_site.render_form_item_select(form.members) }}
{{ macros_site.render_form_item_select(form.managers) }}
{%- endif %}
{{ macros_site.form_errors(form.source.errors) }} {{ macros_site.form_errors(form.source.errors) }}
{{ form.source }} {{ form.source }}
{{ macros_site.form_errors(form.csrf_token.errors) }} {{ macros_site.form_errors(form.csrf_token.errors) }}
......
...@@ -40,6 +40,34 @@ ...@@ -40,6 +40,34 @@
<th class="info">{{ gettext('Description') }}</th> <th class="info">{{ gettext('Description') }}</th>
<td>{{ item.description | default(gettext('<< unknown >>')) }}</td> <td>{{ item.description | default(gettext('<< unknown >>')) }}</td>
</tr> </tr>
<tr>
<th class="info">{{ gettext('Members') }}</th>
<td>
{%- if item.members %}
{%- for subitem in item.members %}
<a data-toggle="tooltip" href="{{ url_for('users.show', item_id = subitem.id ) }}" title="{{ gettext('View details of user account &quot;%(item)s&quot;', item = subitem.login) }}">
{{ subitem.fullname }}
</a>{%- if not loop.last %} | {%- endif %}
{%- endfor %}
{%- else %}
{{ gettext('<< none >>') }}
{%- endif %}
</td>
</tr>
<tr>
<th class="info">{{ gettext('Managers') }}</th>
<td>
{%- if item.managers %}
{%- for subitem in item.managers %}
<a data-toggle="tooltip" href="{{ url_for('users.show', item_id = subitem.id ) }}" title="{{ gettext('View details of user account &quot;%(item)s&quot;', item = subitem.login) }}">
{{ subitem.fullname }}
</a>{%- if not loop.last %} | {%- endif %}
{%- endfor %}
{%- else %}
{{ gettext('<< none >>') }}
{%- endif %}
</td>
</tr>
</tbody> </tbody>
</table> </table>
</div><!-- /.col-lg-12 --> </div><!-- /.col-lg-12 -->
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment