Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion admin/templates/resource/create.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,13 @@
<div class="w-1/2 mb-4">
<div class="flex flex-col mr-3">
<label for={{ attribute.name }} class="uppercase text-gray-500 text-xs mb-1">{{ attribute.name | format_label }}</label>
{% if 'VARCHAR' in attribute.type %}
{% if attribute["name"] == "roles" %}
<select name="{{ attribute.name }}" id="{{ attribute.name }}" class="py-2.5 px-3 border border-gray-300 rounded">
{% for role in roles_list %}
<option value="{{ role }}">{{ role }}</option>
{% endfor %}
</select>
{% elif 'VARCHAR' in attribute.type %}
<input type="text" name="{{ attribute.name }}" id="{{ attribute.name }}" class="py-2 px-3 border border-gray-300 rounded" />
{% elif attribute.type == 'INTEGER' %}
<input type="number" name="{{ attribute.name }}" id="{{ attribute.name }}" class="py-2 px-3 border border-gray-300 rounded" />
Expand Down
8 changes: 7 additions & 1 deletion admin/templates/resource/edit.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,13 @@
<div class="w-1/2 mb-4">
<div class="flex flex-col mr-3">
<label for={{ attribute.name }} class="uppercase text-gray-500 text-xs mb-1">{{ attribute.name | format_label }}</label>
{% if attribute["name"] == admin_configs['user']['secret'] %}
{% if attribute["name"] == "roles" %}
<select name="{{ attribute.name }}" id="{{ attribute.name }}" class="py-2.5 px-3 border border-gray-300 rounded">
{% for role in roles_list %}
<option value="{{ role }}" {% if resource[attribute.name] == role %} selected {% endif %}>{{ role }}</option>
{% endfor %}
</select>
{% elif attribute["name"] == admin_configs['user']['secret'] %}
<input type="password" name="{{ attribute.name }}" id="{{ attribute.name }}" class="py-2 px-3 border border-gray-300 rounded" />
{% elif 'VARCHAR' in attribute.type %}
<input type="text" name="{{ attribute.name }}" id="{{ attribute.name }}" class="py-2 px-3 border border-gray-300 rounded" {% if resource[attribute.name] %} value="{{ resource[attribute.name] }}" {% endif %} />
Expand Down
37 changes: 26 additions & 11 deletions admin/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
from flask_bcrypt import Bcrypt
from flask_login import current_user, login_required, login_user, logout_user
from flask_wtf import FlaskForm
from models.admin_portal_user import AdminPortalUser

# TODO: remove project dependency
from models.crop import CropModel
Expand Down Expand Up @@ -173,12 +174,12 @@ def process_user_id(user_id):
if user_id is None:
return False

selected_user = UserModel.query.filter(
UserModel.roles == "cs_user", UserModel.id == user_id
selected_user = AdminPortalUser.query.filter(
AdminPortalUser.roles == "Data Collector", AdminPortalUser.id == user_id
).first()

if selected_user is not None:
if selected_user.roles == "cs_user":
if selected_user.roles == "Data Collector":
return "Team Member"
else:
return "Regular User"
Expand Down Expand Up @@ -584,10 +585,6 @@ def filter_resources(
and_(or_(*search_query_conditions), and_(*date_conditions))
)

if model.__name__ == 'UserModel':
role_condition = model.roles.in_(['cs_user', 'admin', 'user', 'superadmin'])
filter_query = filter_query.filter(or_(role_condition, model.roles.isnot(None)))

if sort and len(sort):
sort_conditions = []
for criterion in sort:
Expand Down Expand Up @@ -733,12 +730,14 @@ def resource_create(resource_type):
resource_class = get_resource_class(resource_type)
model = resource_class.model
editable_attributes = get_editable_attributes(resource_type)
roles_list = ["Super Admin", "Data Validator", "Data Collector", "Admin"]

if request.method == "GET":
return render_template(
"resource/create.html",
resource_type=resource_type,
editable_attributes=editable_attributes,
roles_list=roles_list,
)

attributes_to_save = {}
Expand Down Expand Up @@ -845,6 +844,7 @@ def resource_edit(resource_type, resource_id):
)

editable_attributes = get_editable_attributes(resource_type)
roles_list = ["Super Admin", "Data Validator", "Data Collector", "Admin"]
old_resource = copy.copy(
resource
) # make a clone before there are any updates
Expand All @@ -856,6 +856,7 @@ def resource_edit(resource_type, resource_id):
resource=resource,
editable_attributes=editable_attributes,
admin_configs=admin_configs,
roles_list=roles_list,
)

if (
Expand Down Expand Up @@ -900,6 +901,10 @@ def resource_edit(resource_type, resource_id):
)
setattr(resource, attribute["name"], validated_attribute_value)

update_roles_in_admin_portal_users(
resource.id, validated_attribute_value
)

if resource_type == "mandi-receipt":
updated_mandi = MandiModel.query.get(resource.mandi_id)
updated_crop = CropModel.query.get(resource.crop_id)
Expand Down Expand Up @@ -978,6 +983,16 @@ def resource_delete(resource_type, resource_id):
)


def update_roles_in_admin_portal_users(user_id, roles):
admin_user = AdminPortalUser.query.filter_by(id=user_id).first()

if admin_user:
admin_user.roles = roles

db.session.add(admin_user)
db.session.commit()


@admin.route("/resource/<string:resource_type>/download", methods=["GET"])
@login_required
def resource_download(resource_type):
Expand Down Expand Up @@ -1326,8 +1341,8 @@ def resource_filter(resource_type, status):

# cs_user_details
cs_users = (
UserModel.query.filter(UserModel.roles == "cs_user")
.order_by(asc(UserModel.name))
AdminPortalUser.query.filter(AdminPortalUser.roles == "Data Collector")
.order_by(asc(AdminPortalUser.name))
.all()
)
list_display = resource_class.list_display
Expand All @@ -1348,8 +1363,8 @@ def resource_filter(resource_type, status):
selected_crop = crop_id
if user_id:
filter_conditions.append(model.user_id == user_id)
selected_user = UserModel.query.filter(
UserModel.roles == "cs_user", UserModel.id == user_id
selected_user = AdminPortalUser.query.filter(
AdminPortalUser.roles == "Data Collector", AdminPortalUser.id == user_id
).first()
selected_user_mobile_number = selected_user.mobile_number
selected_user_id = user_id
Expand Down