Commit a23660ca by Dudás Ádám

school: refactoring and some TODOs for views

TODOs are refactorings which have to be synchronized with the HTML/JS side
parent 61edad50
...@@ -31,6 +31,8 @@ import re ...@@ -31,6 +31,8 @@ import re
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
neptun_re = re.compile('^[a-zA-Z][a-zA-Z0-9]{5}$')
def logout(request): def logout(request):
auth.logout(request) auth.logout(request)
return redirect('/Shibboleth.sso/Logout?return=https%3a%2f%2fcloud.ik.bme.hu%2f') return redirect('/Shibboleth.sso/Logout?return=https%3a%2f%2fcloud.ik.bme.hu%2f')
...@@ -185,7 +187,7 @@ def group_new(request): ...@@ -185,7 +187,7 @@ def group_new(request):
members_list = [m for m in members_list if m != ''] members_list = [m for m in members_list if m != '']
members = [] members = []
for member in members_list: for member in members_list:
if re.match('^[a-zA-Z][a-zA-Z0-9]{5}$', member.strip()) is None: if neptun_re.match(member.strip()) is None:
messages.error(request, _('Invalid NEPTUN code found.')) messages.error(request, _('Invalid NEPTUN code found.'))
return redirect('/') return redirect('/')
person, created = Person.objects.get_or_create(code=member) person, created = Person.objects.get_or_create(code=member)
...@@ -207,7 +209,7 @@ def group_new(request): ...@@ -207,7 +209,7 @@ def group_new(request):
def group_ajax_add_new_member(request, gid): def group_ajax_add_new_member(request, gid):
group = get_object_or_404(Group, id=gid) group = get_object_or_404(Group, id=gid)
member = request.POST['neptun'] member = request.POST['neptun']
if re.match('^[a-zA-Z][a-zA-Z0-9]{5}$', member.strip()) is None: if neptun_re.match(member.strip()) is None:
status = json.dumps({'status': 'Error'}) status = json.dumps({'status': 'Error'})
messages.error(request, _('Invalid NEPTUN code')) messages.error(request, _('Invalid NEPTUN code'))
return HttpResponse(status) return HttpResponse(status)
...@@ -223,7 +225,7 @@ def group_ajax_add_new_member(request, gid): ...@@ -223,7 +225,7 @@ def group_ajax_add_new_member(request, gid):
def group_ajax_remove_member(request, gid): def group_ajax_remove_member(request, gid):
group = get_object_or_404(Group, id=gid) group = get_object_or_404(Group, id=gid)
member = request.POST['neptun'] member = request.POST['neptun']
if re.match('^[a-zA-Z][a-zA-Z0-9]{5}$', member) is None: if neptun_re.match(member.strip()) is None:
status = json.dumps({'status': 'Error'}) status = json.dumps({'status': 'Error'})
messages.error(request, _('Invalid NEPTUN code')) messages.error(request, _('Invalid NEPTUN code'))
return HttpResponse(status) return HttpResponse(status)
...@@ -237,6 +239,7 @@ def group_ajax_remove_member(request, gid): ...@@ -237,6 +239,7 @@ def group_ajax_remove_member(request, gid):
@login_required @login_required
def group_ajax_delete(request): def group_ajax_delete(request):
# TODO should take parameter in URL using DELETE command
gid = request.POST['gid'] gid = request.POST['gid']
group = get_object_or_404(Group, id=gid) group = get_object_or_404(Group, id=gid)
group.delete() group.delete()
...@@ -247,30 +250,27 @@ def group_ajax_delete(request): ...@@ -247,30 +250,27 @@ def group_ajax_delete(request):
@login_required @login_required
def group_ajax_owner_autocomplete(request): def group_ajax_owner_autocomplete(request):
# TODO should be renamed to something like 'user_ajax_autocomplete'
users = ( query = request.POST['q']
User.objects.filter(last_name__istartswith=request.POST['q'])[:5] + users = chain(User.objects.filter(last_name__istartswith=query)[:5],
User.objects.filter(first_name__istartswith=request.POST['q'])[:5] + User.objects.filter(first_name__istartswith=query)[:5],
User.objects.filter(username__istartswith=request.POST['q'])[:5]) User.objects.filter(username__istartswith=query)[:5])
results = map(lambda u: { results = [{'name': user.get_full_name(),
'name': u.get_full_name(), 'neptun': user.username} for user in users]
'neptun': u.username}, users)
return HttpResponse(json.dumps(results, ensure_ascii=False)) return HttpResponse(json.dumps(results, ensure_ascii=False))
@login_required @login_required
def group_ajax_add_new_owner(request, gid): def group_ajax_add_new_owner(request, gid):
if request.user.cloud_details.share_quota == 0: if request.user.cloud_details.share_quota <= 0:
return HttpResponse({'status': 'denied'}) return HttpResponse(json.dumps({'status': 'denied'}))
group = get_object_or_404(Group, id=gid) group = get_object_or_404(Group, id=gid)
member = request.POST['neptun'] member = request.POST['neptun']
if re.match('^[a-zA-Z][a-zA-Z0-9]{5}$', member.strip()) is None: if neptun_re.match(member.strip()) is None:
status = json.dumps({'status': 'Error'}) status = json.dumps({'status': 'Error'})
messages.error(request, _('Invalid NEPTUN code')) messages.error(request, _('Invalid NEPTUN code'))
return HttpResponse(status) return HttpResponse(status)
person, created = Person.objects.get_or_create(code=member) person, created = Person.objects.get_or_create(code=member)
group.owners.add(person) group.owners.add(person)
group.save() group.save()
return HttpResponse(json.dumps({ return HttpResponse(json.dumps({'status': 'OK'}))
'status': 'OK'
}))
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment