Blame action/repo.py

e5b0ac
e5b0ac
e5b0ac
import exception
e5b0ac
from action.action import Action
e5b0ac
from action.user import UserBase
e5b0ac
e5b0ac
e5b0ac
class RepoBase(UserBase):
e5b0ac
  def parse_repository_id(self, request):
e5b0ac
    repository_id = 0
e5b0ac
    try:
e5b0ac
      repository_id = int(request.postvars.get('repository_id', 0))
e5b0ac
    except Exception:
e5b0ac
      raise exception.ActionError( request.t('Repository Id incorrect') )
e5b0ac
    if not repository_id:
e5b0ac
      raise exception.ActionError( request.t('Repository Id incorrect') )
e5b0ac
    return repository_id
e5b0ac
  
e146e6
  def redirect_to_repo(self, request, repo, path = None):
e146e6
    if path is None: path = list()
e146e6
    return self.redirect_to_user(request, repo.get_user(), ['repo', str(repo.name)] + path)
e146e6
e5b0ac
e5b0ac
class RepoCreate(RepoBase):
e5b0ac
  def process(self, request):
e5b0ac
    user_id = self.parse_user_id(request)
e5b0ac
    name = str(request.postvars.get('name', ''))
e5b0ac
    repotype = str(request.postvars.get('type', ''))
e5b0ac
    title = str(request.postvars.get('title', ''))
e5b0ac
    description = str(request.postvars.get('description', ''))
e5b0ac
    
e146e6
    user = request.model.users.get_by_id(user_id)
e146e6
    if not user:
e146e6
      raise exception.ActionError( request.t('User not found') )
e146e6
e5b0ac
    repo = None
e5b0ac
    try:
e146e6
      repo = request.model.repositories.create(user, name, repotype, title, description)
e5b0ac
    except Exception as e:
e5b0ac
      self.propagate_exception(e)
e5b0ac
e5b0ac
    request.connection.commit()
e146e6
    return self.redirect_to_repo(request, repo)
e5b0ac
e5b0ac
e5b0ac
class RepoUpdate(RepoBase):
e5b0ac
  def process(self, request):
e5b0ac
    repository_id = self.parse_repository_id(request)
e5b0ac
    title = str(request.postvars.get('title', ''))
e5b0ac
    description = str(request.postvars.get('description', ''))
e5b0ac
    
e5b0ac
    repo = request.model.repositories.get_by_id(repository_id)
e5b0ac
    if not repo:
e5b0ac
      raise exception.ActionError( request.t('Repository not found') )
e5b0ac
    
e5b0ac
    try:
e5b0ac
      repo.update(title, description)
e5b0ac
    except Exception as e:
e5b0ac
      self.propagate_exception(e)
e5b0ac
e5b0ac
    request.connection.commit()
e146e6
    return self.redirect_to_repo(request, repo)
e5b0ac
    
e5b0ac
e5b0ac
class RepoDelete(RepoBase):
e5b0ac
  def process(self, request):
e5b0ac
    repository_id = self.parse_repository_id(request)
e5b0ac
    
e5b0ac
    repo = request.model.repositories.get_by_id(repository_id)
e5b0ac
    if not repo:
e5b0ac
      raise exception.ActionError( request.t('Repository not found') )
e146e6
    user = repo.get_user()
e5b0ac
    
e5b0ac
    try:
e5b0ac
      repo.delete()
e5b0ac
    except Exception as e:
e5b0ac
      self.propagate_exception(e)
e5b0ac
e5b0ac
    request.connection.commit()
e146e6
    return self.redirect_to_user(request, user, ['repos'])
e5b0ac
    
e5b0ac
e146e6
class RepoSetUserRight(RepoBase):
e146e6
  def process(self, request):
e146e6
    repository_id = self.parse_repository_id(request)
e146e6
    repo = request.model.repositories.get_by_id(repository_id)
e146e6
    if not repo:
e146e6
      raise exception.ActionError( request.t('Repository not found') )
e146e6
e146e6
    allowed = bool(request.postvars.get('allowed', False))
e146e6
    mode = str(request.postvars.get('mode', ''))
e146e6
    
e146e6
    try:
e146e6
      user_id = int(request.postvars.get('user_id', 0))
e146e6
    except Exception:
e146e6
      raise exception.ActionError( request.t('User Id incorrect') )
e146e6
    user_login = str(request.postvars.get('user_login', ''))
e146e6
    all_users = bool(request.postvars.get('all_users', False))
e146e6
    
e146e6
    if user_id and user_login:
e146e6
      raise exception.ActionError( request.t('Both user id and user login was set') )
e146e6
    if all_users and (user_id or user_login):
e146e6
      raise exception.ActionError( request.t('All users flag was set with selection of concrete user') )
e146e6
    
e146e6
    user = None
e146e6
    if user_id:
e146e6
      user = request.model.users.get_by_id(user_id)
e146e6
    elif user_login:
e146e6
      user = request.model.users.get_by_login(user_login)
e146e6
      user_id = user.id
e146e6
    
e146e6
    if not all_users and not user:
e146e6
      raise exception.ActionError( request.t('User not found') )
e146e6
      
e146e6
    try:
e146e6
      repo.set_user_right(user_id, mode, allowed, all_users = all_users)
e146e6
    except Exception as e:
e146e6
      self.propagate_exception(e)
e146e6
e146e6
    request.connection.commit()
e146e6
    return self.redirect_to_repo(request, repo, ['edit'])
e146e6
e146e6
e5b0ac
actions = {
e146e6
  'create'       : RepoCreate(),
e146e6
  'update'       : RepoUpdate(),
e146e6
  'delete'       : RepoDelete(),
e146e6
  'setuserright' : RepoSetUserRight(),
e5b0ac
}
e5b0ac
e5b0ac