Blame action/repo.py

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