Blob Blame Raw

import exception
from generator.form import Form

from page.page import Page
from page.repo import RepoPage, RepoCreatePage, RepoListPage


class UserCreatePage(Page):
  def process(self, request, path, prevpath):
    if path:
      raise exception.HttpNotFound()
    if not request.model.users.can_create():
      raise exception.HttpNotFound()
    answer = request.answer
    
    answer.add_uipath_entry( answer.t('Create user'), prevpath )
    
    form = Form(request)
    form.begin('', 'user.create')
    form.add_input('login:', 'login', 'text')
    form.add_input('password:', 'password', 'password', '')
    form.add_input('retry password:', 'passwordretry', 'password', '')
    form.add_input('name:', 'name', 'text')
    form.add_submit()
    form.end()
    answer.content += form.content

    return answer.complete_content()


class UserPage(Page):
  def __init__(self):
    super().__init__()
    self.profile = UserProfilePage()
    
  def process(self, request, path, prevpath):
    if not path:
      raise exception.HttpNotFound()

    user = None
    user = request.model.users.get_by_login(str(path[0]))
    if not user:
      raise exception.HttpNotFound()

    answer = request.answer

    answer.objects['user'] = user
    answer.add_uipath_entry( user.name, prevpath + [user.login] )
    return self.profile.sub_process(request, path, prevpath)


class UserProfilePage(Page):
  def __init__(self):
    super().__init__()
    self.edit = UserUpdatePage()
    self.delete = UserDeletePage()
    self.repo = RepoPage()
    self.repo_create = RepoCreatePage()
    self.repos = RepoListPage()

  def process(self, request, path, prevpath):
    user = request.answer.objects['user']
    answer = request.answer

    if path:
      if path[0] == 'edit':
        return self.edit.sub_process(request, path, prevpath)
      if path[0] == 'delete':
        return self.delete.sub_process(request, path, prevpath)
      if path[0] == 'repo':
        return self.repo.sub_process(request, path, prevpath)
      if path[0] == 'repo_create':
        return self.repo_create.sub_process(request, path, prevpath)
      if path[0] == 'repos':
        return self.repos.sub_process(request, path, prevpath)
      raise exception.HttpNotFound()

    answer.content += '<p>' + answer.e(user.login) + '</p>\n'
    answer.content += '<p>' + answer.e(user.name) + '</p>\n'
    if user.superuser:
      answer.content += '<p>' + answer.te('Site admin') + '</p>\n'
    if user.can_update():
      answer.content += '<p>' + self.make_link(answer, prevpath + ['edit'], 'Edit user') + '</p>\n'

    answer.content += '<p>' + self.make_link(answer, prevpath + ['repos'], 'Repositories') + '</p>\n'

    return answer.complete_content()


class UserUpdatePage(Page):
  def process(self, request, path, prevpath):
    if path:
      raise exception.HttpNotFound()

    answer = request.answer
    user = answer.objects['user']
    if not user.can_update():
      raise exception.HttpNotFound()
    
    answer.add_uipath_entry( answer.t('Edit user'), prevpath )

    form = Form(request)
    form.begin('Profile', 'user.update')
    form.add_hidden('user_id', user.id)
    form.add_input('name:', 'name', 'name', user.name)
    form.add_submit()
    form.end()
    answer.content += form.content
    
    form = Form(request)
    form.begin('Change password', 'user.setpassword')
    form.add_hidden('user_id', user.id)
    if user.id == request.model.myrights.user_id:
      form.add_input('old password:', 'oldpassword', 'password', '')
    form.add_input('new password:', 'newpassword', 'password', '')
    form.add_input('new password retry:', 'newpasswordretry', 'password', '')
    form.add_submit()
    form.end()
    answer.content += form.content
    
    if user.id != request.model.myrights.user_id and not user.superuser is None:
      form = Form(request)
      form.begin('Global rights', 'user.setsuperuser')
      form.add_hidden('user_id', user.id)
      form.add_checkbox('site admin:', 'superuser', user.superuser)
      form.add_submit()
      form.end()
      answer.content += form.content

    if user.can_delete() and prevpath:
      url = request.get_urlpath_escaped(prevpath[:-1] + ['delete'])
      form = Form(request)
      form.content += '<form method="GET" action="' + url + '">' + form.GROUP_BEGIN
      form.content += form.title('Deletion')
      form.add_submit('Delete user')
      form.end()
      answer.content += form.content

    return answer.complete_content()


class UserDeletePage(Page):
  def process(self, request, path, prevpath):
    if path:
      raise exception.HttpNotFound()

    answer = request.answer
    user = answer.objects['user']
    if not user.can_delete():
      raise exception.HttpNotFound()

    answer.add_uipath_entry( answer.t('Delete user'), prevpath )

    form = Form(request)
    form.begin('', 'user.delete')
    form.add_hidden('user_id', user.id)
    form.content += '<p>' + answer.e(user.login) + '</p>\n'
    form.content += '<p>' + answer.e(user.name) + '</p>\n'
    form.content += '<p>' + answer.te('Do you really want do delete user?') + '</p>\n'
    if user.id == request.model.myrights.user_id:
      form.add_input('password to delete your account:', 'password', 'password', '')
    form.add_submit('Confirm delete')
    form.end()
    answer.content += form.content

    return answer.complete_content()


class UserListPage(Page):
  def process(self, request, path, prevpath):
    if path:
      raise exception.HttpNotFound()
    if not prevpath:
      raise exception.HttpNotFound()
    if not request.model.users.can_list():
      raise exception.HttpNotFound()
    
    users = request.model.users.get_list()
    
    answer = request.answer
    answer.add_uipath_entry( answer.t('Users list'), prevpath )

    for user in users:
      url = request.get_urlpath_escaped(prevpath[:-1] + ['user', str(user.login)])
      answer.content += '<p><a href="' + url + '">' \
                     + answer.e(user.login) + ' ' \
                     + answer.e(user.name) + '</a></p>\n'

    if request.model.users.can_create():
      answer.content += '<p>' + self.make_link(answer, prevpath[:-1] + ['user_create'], 'Create user') + '</p>\n'

    return answer.complete_content()