Implement bulk user management features: add user generation preview and bulk creation functionality in CID group view

This commit is contained in:
Ross
2025-12-15 12:23:26 +00:00
parent 885ab1aad4
commit 00b736d8d6
5 changed files with 173 additions and 53 deletions
+72
View File
@@ -4561,6 +4561,78 @@ def cid_group_view_detail(request, group_id):
)
@user_is_cid_user_manager
def cid_group_generate_preview(request, group_id):
"""Parse submitted rows and return a preview fragment for HTMX."""
if not request.htmx:
raise Http404()
group = get_object_or_404(CidUserGroup, pk=group_id)
rows_text = request.POST.get('rows', '')
original = rows_text
parsed = []
for line in rows_text.splitlines():
line = line.strip()
if not line:
continue
# Accept CSV or tab-separated: name,email or just email
parts = [p.strip() for p in line.replace('\t', ',').split(',') if p.strip()]
if len(parts) == 1:
email = parts[0]
name = ''
else:
name = parts[0]
email = parts[1]
parsed.append({'name': name, 'email': email})
html = render_to_string('generic/partials/cid_group_generate_preview.html', {'rows': parsed, 'original': original, 'group': group}, request=request)
return HttpResponse(html)
@user_is_cid_user_manager
def cid_group_create_bulk(request, group_id):
"""Create CidUser rows from posted rows and add them to the group, returning updated users list HTML."""
if not request.htmx:
raise Http404()
group = get_object_or_404(CidUserGroup, pk=group_id)
rows_text = request.POST.get('rows', '')
created = []
import secrets
for line in rows_text.splitlines():
line = line.strip()
if not line:
continue
parts = [p.strip() for p in line.replace('\t', ',').split(',') if p.strip()]
if len(parts) == 1:
email = parts[0]
name = ''
else:
name = parts[0]
email = parts[1]
# generate cid and passcode
try:
cid_val = get_next_cid()
except Exception:
# fallback: max+1
last = CidUser.objects.order_by('-cid').first()
cid_val = (last.cid if last and last.cid else 0) + 1
passcode = secrets.token_hex(3)
cu = CidUser.objects.create(cid=cid_val, passcode=passcode, name=name, email=email, group=group)
created.append(cu)
# re-render the users list
users = group.ciduser_set.filter(active=True)
inner = render_to_string('generic/partials/group_users_list.html', {'users': users, 'group_type': 'cid'}, request=request)
# return the wrapper so HTMX can replace the container even if it didn't exist before
html = f"<div id=\"group-users-wrap\">{inner}</div>"
return HttpResponse(html)
@user_is_cid_user_manager
def cid_group_view_all(request):
groups = CidUserGroup.objects.filter()