The models module — Kallithea 0.3.6 documentation

# The `models` module

## kallithea.model

The application’s model objects

This file was forked by the Kallithea project in July 2014. Original author and date, and relevant copyright and licensing information is below: :created_on: Nov 25, 2010 :author: marcink :copyright: (c) 2013 RhodeCode GmbH, and others. :license: GPLv3, see LICENSE.md for more details.

example: |
    from paste.deploy import appconfig
    from pylons import config
    from sqlalchemy import engine_from_config
    from kallithea.config.environment import load_environment

    conf = appconfig('config:development.ini', relative_to = './../../')
    load_environment(conf.global_conf, conf.local_conf)

    engine = engine_from_config(config, 'sqlalchemy.')
    init_model(engine)
    # RUN YOUR CODE HERE

class `kallithea.model.` `BaseModel`(sa=None)

  Base Model for all Kallithea models, it adds sql alchemy session into instance of model

  Parameters: | sa – If passed it reuses this session instead of creating a new one

  classmethod `get_all`()

    Returns all instances of what is defined in cls class variable

`kallithea.model.` `init_model`(engine)

  Initializes db session, bind the engine with the metadata, Call this before using any of the tables or classes in the model, preferably once in application start

  Parameters: | engine – engine to bind to

## kallithea.model.comment

comments model for Kallithea

This file was forked by the Kallithea project in July 2014. Original author and date, and relevant copyright and licensing information is below: :created_on: Nov 11, 2011 :author: marcink :copyright: (c) 2013 RhodeCode GmbH, and others. :license: GPLv3, see LICENSE.md for more details.

class `kallithea.model.comment.` `ChangesetCommentsModel`(sa=None)
  `cls`

    alias of `kallithea.model.db.ChangesetComment`

  `create`(text, repo, user, revision=None, pull_request=None, f_path=None, line_no=None, status_change=None, closing_pr=False, send_email=True)

    Creates a new comment for either a changeset or a pull request. status_change and closing_pr is only for the optional email.

    Returns the created comment.

  `get_comments`(repo_id, revision=None, pull_request=None)

    Gets general comments for either revision or pull_request.

    Returns a list, ordered by creation date.

  `get_inline_comments`(repo_id, revision=None, pull_request=None)

    Gets inline comments for either revision or pull_request.

    Returns a list of tuples with file path and list of comments per line number.

## kallithea.model.notification

Model for notifications

This file was forked by the Kallithea project in July 2014. Original author and date, and relevant copyright and licensing information is below: :created_on: Nov 20, 2011 :author: marcink :copyright: (c) 2013 RhodeCode GmbH, and others. :license: GPLv3, see LICENSE.md for more details.

class `kallithea.model.notification.` `EmailNotificationModel`
  `get_email_description`(type_, **kwargs)

    return subject for email based on given type

  `get_email_tmpl`(type_, content_type, **kwargs)

    return generated template for email based on given type

class `kallithea.model.notification.` `NotificationModel`(sa=None)
  `cls`

    alias of `kallithea.model.db.Notification`

  `create`(created_by, subject, body, recipients=None, type_=u'message', with_email=True, email_kwargs={})

    Creates notification of given type

    Parameters: | created_by – int, str or User instance. User who created this notification subject – body – recipients – list of int, str or User objects, when None is given send to all admins type – type of notification with_email – send email with this notification email_kwargs – additional dict to pass as args to email template

  `get_for_user`(user, filter_=None)

    Get notifications for given user, filter them if filter dict is given

    Parameters: | user – filter –

  `make_description`(notification, show_age=True)

    Creates a human readable description based on properties of notification object

## kallithea.model.permission

permissions model for Kallithea

This file was forked by the Kallithea project in July 2014. Original author and date, and relevant copyright and licensing information is below: :created_on: Aug 20, 2010 :author: marcink :copyright: (c) 2013 RhodeCode GmbH, and others. :license: GPLv3, see LICENSE.md for more details.

class `kallithea.model.permission.` `PermissionModel`(sa=None)

  Permissions model for Kallithea

  `cls`

    alias of `kallithea.model.db.Permission`

  `create_default_permissions`(user, force=False)

    Create missing default permissions for user. If force is set, the default permissions for the user are reset, otherwise only missing permissions are created.

    Parameters: | user –

  `create_permissions`()

    Create permissions for whole system

## kallithea.model.repo_permission

repository permission model for Kallithea

This file was forked by the Kallithea project in July 2014. Original author and date, and relevant copyright and licensing information is below: :created_on: Oct 1, 2011 :author: nvinot, marcink

class `kallithea.model.repo_permission.` `RepositoryPermissionModel`(sa=None)
  `cls`

    alias of `kallithea.model.db.UserRepoToPerm`

## kallithea.model.repo

Repository model for kallithea

This file was forked by the Kallithea project in July 2014. Original author and date, and relevant copyright and licensing information is below: :created_on: Jun 5, 2010 :author: marcink :copyright: (c) 2013 RhodeCode GmbH, and others. :license: GPLv3, see LICENSE.md for more details.

class `kallithea.model.repo.` `RepoModel`(sa=None)
  `cls`

    alias of `kallithea.model.db.Repository`

  `create`(form_data, cur_user)

    Create repository using celery tasks

    Parameters: | form_data – cur_user –

  `create_fork`(form_data, cur_user)

    Simple wrapper into executing celery task for fork creation

    Parameters: | form_data – cur_user –

  `delete`(repo, forks=None, fs_remove=True, cur_user=None)

    Delete given repository, forks parameter defines what do do with attached forks. Throws AttachedForksError if deleted repo has attached forks

    Parameters: | repo – forks – str ‘delete’ or ‘detach’ fs_remove – remove(archive) repo from filesystem

  `delete_stats`(repo_name)

    removes stats for given repo

    Parameters: | repo_name –

  `get_all_user_repos`(user)

    Gets all repositories that user have at least read access

    Parameters: | user –

  `grant_user_group_permission`(repo, group_name, perm)

    Grant permission for user group on given repository, or update existing one if found

    Parameters: | repo – Instance of Repository, repository_id, or repository name group_name – Instance of UserGroup, users_group_id, or user group name perm – Instance of Permission, or permission_name

  `grant_user_permission`(repo, user, perm)

    Grant permission for user on given repository, or update existing one if found

    Parameters: | repo – Instance of Repository, repository_id, or repository name user – Instance of User, user_id or username perm – Instance of Permission, or permission_name

  `repos_path`

    Gets the repositories root path from database

  `revoke_user_group_permission`(repo, group_name)

    Revoke permission for user group on given repository

    Parameters: | repo – Instance of Repository, repository_id, or repository name group_name – Instance of UserGroup, users_group_id, or user group name

  `revoke_user_permission`(repo, user)

    Revoke permission for user on given repository

    Parameters: | repo – Instance of Repository, repository_id, or repository name user – Instance of User, user_id or username

## kallithea.model.repo_group

repo group model for Kallithea

This file was forked by the Kallithea project in July 2014. Original author and date, and relevant copyright and licensing information is below: :created_on: Jan 25, 2011 :author: marcink :copyright: (c) 2013 RhodeCode GmbH, and others. :license: GPLv3, see LICENSE.md for more details.

class `kallithea.model.repo_group.` `RepoGroupModel`(sa=None)
  `cls`

    alias of `kallithea.model.db.RepoGroup`

  `delete_permission`(repo_group, obj, obj_type, recursive)

    Revokes permission for repo_group for given obj(user or users_group), obj_type can be user or user group

    Parameters: | repo_group – obj – user or user group id obj_type – user or user group type recursive – recurse to all children of group

  `grant_user_group_permission`(repo_group, group_name, perm)

    Grant permission for user group on given repository group, or update existing one if found

    Parameters: | repo_group – Instance of RepoGroup, repositories_group_id, or repositories_group name group_name – Instance of UserGroup, users_group_id, or user group name perm – Instance of Permission, or permission_name

  `grant_user_permission`(repo_group, user, perm)

    Grant permission for user on given repository group, or update existing one if found

    Parameters: | repo_group – Instance of RepoGroup, repositories_group_id, or repositories_group name user – Instance of User, user_id or username perm – Instance of Permission, or permission_name

  `repos_path`

    Gets the repositories root path from database

  `revoke_user_group_permission`(repo_group, group_name)

    Revoke permission for user group on given repository group

    Parameters: | repo_group – Instance of RepoGroup, repositories_group_id, or repositories_group name group_name – Instance of UserGroup, users_group_id, or user group name

  `revoke_user_permission`(repo_group, user)

    Revoke permission for user on given repository group

    Parameters: | repo_group – Instance of RepoGroup, repositories_group_id, or repositories_group name user – Instance of User, user_id or username

## kallithea.model.scm

Scm model for Kallithea

This file was forked by the Kallithea project in July 2014. Original author and date, and relevant copyright and licensing information is below: :created_on: Apr 9, 2010 :author: marcink :copyright: (c) 2013 RhodeCode GmbH, and others. :license: GPLv3, see LICENSE.md for more details.

`kallithea.model.scm.` `AvailableRepoGroupChoices`(top_perms, repo_group_perms, extras=())

  Return group_id,string tuples with choices for all the repo groups where the user has the necessary permissions.

  Top level is -1.

class `kallithea.model.scm.` `CachedRepoList`(db_repo_list, repos_path, order_by=None, perm_set=None)

  Cached repo list. Uses super-fast in-memory cache after initialization.

class `kallithea.model.scm.` `ScmModel`(sa=None)

  Generic Scm Model

  `commit_change`(repo, repo_name, cs, user, author, message, content, f_path)

    Commit a change to a single file

    Parameters: | repo – a db_repo.scm_instance

  `create_nodes`(user, repo, message, nodes, parent_cs=None, author=None, trigger_push_hook=True)

    Commits specified nodes to repo.

    Parameters: | user – Kallithea User object or user_id, the committer repo – Kallithea Repository object message – commit message nodes – mapping {filename:{‘content’:content},…} parent_cs – parent changeset, can be empty than it’s initial commit author – author of commit, cna be different that committer only for git trigger_push_hook – trigger push hooks
    Returns: | new committed changeset

  `delete_nodes`(user, repo, message, nodes, parent_cs=None, author=None, trigger_push_hook=True)

    Deletes specified nodes from repo.

    Parameters: | user – Kallithea User object or user_id, the committer repo – Kallithea Repository object message – commit message nodes – mapping {filename:{‘content’:content},…} parent_cs – parent changeset, can be empty than it’s initial commit author – author of commit, cna be different that committer only for git trigger_push_hook – trigger push hooks
    Returns: | new committed changeset after deletion

  `get_nodes`(repo_name, revision, root_path='/', flat=True)

    Recursively walk root dir and return a set of all paths found.

    Parameters: | repo_name – name of repository revision – revision for which to list nodes root_path – root path to list flat – return as a list, if False returns a dict with description

  `get_repo_landing_revs`(repo=None)

    Generates select option with tags branches and bookmarks (for hg only) grouped by type

    Parameters: | repo –

  `get_repos`(all_repos=None, sort_key=None, simple=False)

    Get all repos from db and for each repo create its backend instance and fill that backed with information from database

    Parameters: | all_repos – list of repository names as strings give specific repositories list, good for filtering sort_key – initial sorting of repos simple – use SimpleCachedList - one without the SCM info

  `install_git_hooks`(repo, force_create=False)

    Creates a kallithea hook inside a git repository

    Parameters: | repo – Instance of VCS repo force_create – Create even if same name hook exists

  `mark_for_invalidation`(repo_name, delete=False)

    Mark caches of this repo invalid in the database.

    Parameters: | repo_name – the repo for which caches should be marked invalid

  `pull_changes`(repo, username)

    Pull from “clone URL”.

  `repo_scan`(repos_path=None)

    Listing of repositories in given path. This path should not be a repository itself. Return a dictionary of repository objects

    Parameters: | repos_path – path to directory containing repositories

  `repos_path`

    Gets the repositories root path from database

  `update_nodes`(user, repo, message, nodes, parent_cs=None, author=None, trigger_push_hook=True)

    Commits specified nodes to repo. Again.

class `kallithea.model.scm.` `SimpleCachedRepoList`(db_repo_list, repos_path, order_by=None, perm_set=None)

  Lighter version of CachedRepoList without the scm initialisation

## kallithea.model.user

users model for Kallithea

This file was forked by the Kallithea project in July 2014. Original author and date, and relevant copyright and licensing information is below: :created_on: Apr 9, 2010 :author: marcink :copyright: (c) 2013 RhodeCode GmbH, and others. :license: GPLv3, see LICENSE.md for more details.

class `kallithea.model.user.` `UserModel`(sa=None)
  `add_extra_email`(user, email)

    Adds email address to UserEmailMap

    Parameters: | user – email –

  `add_extra_ip`(user, ip)

    Adds IP address to UserIpMap

    Parameters: | user – ip –

  `cls`

    alias of `kallithea.model.db.User`

  `create_or_update`(username, password, email, firstname='', lastname='', active=True, admin=False, extern_type=None, extern_name=None, cur_user=None)

    Creates a new instance if not found, or updates current one

    Parameters: | username – password – email – active – firstname – lastname – active – admin – extern_name – extern_type – cur_user –

  `delete_extra_email`(user, email_id)

    Removes email address from UserEmailMap

    Parameters: | user – email_id –

  `delete_extra_ip`(user, ip_id)

    Removes IP address from UserIpMap

    Parameters: | user – ip_id –

  `get_reset_password_token`(user, timestamp, session_id)

    The token is a 40-digit hexstring, calculated as a HMAC-SHA1.

    In a traditional HMAC scenario, an attacker is unable to know or influence the secret key, but can know or influence the message and token. This scenario is slightly different (in particular since the message sender is also the message recipient), but sufficiently similar to use an HMAC. Benefits compared to a plain SHA1 hash includes resistance against a length extension attack.

    The HMAC key consists of the following values (known only to the server and authorized users):

    - per-application secret (the app_instance_uuid setting), without which an attacker cannot counterfeit tokens
    - hashed user password, invalidating the token upon password change

    The HMAC message consists of the following values (potentially known to an attacker):

    - session ID (the anti-CSRF token), requiring an attacker to have access to the browser session in which the token was created
    - numeric user ID, limiting the token to a specific user (yet allowing users to be renamed)
    - user email address
    - time of token issue (a Unix timestamp, to enable token expiration)

    The key and message values are separated by NUL characters, which are guaranteed not to occur in any of the values.

  `grant_perm`(user, perm)

    Grant user global permissions

    Parameters: | user – perm –

  `revoke_perm`(user, perm)

    Revoke users global permissions

    Parameters: | user – perm –

  `send_reset_password_email`(data)

    Sends email with a password reset token and link to the password reset confirmation page with all information (including the token) pre-filled. Also returns URL of that page, only without the token, allowing users to copy-paste or manually enter the token from the email.

## kallithea.model.user_group

user group model for Kallithea

This file was forked by the Kallithea project in July 2014. Original author and date, and relevant copyright and licensing information is below: :created_on: Oct 1, 2011 :author: nvinot, marcink

class `kallithea.model.user_group.` `UserGroupModel`(sa=None)
  `cls`

    alias of `kallithea.model.db.UserGroup`

  `delete`(user_group, force=False)

    Deletes user group, unless force flag is used raises exception if there are members in that group, else deletes group and users

    Parameters: | user_group – force –

  `grant_user_group_permission`(target_user_group, user_group, perm)

    Grant user group permission for given target_user_group

    Parameters: | target_user_group – user_group – perm –

  `grant_user_permission`(user_group, user, perm)

    Grant permission for user on given user group, or update existing one if found

    Parameters: | user_group – Instance of UserGroup, users_group_id, or users_group_name user – Instance of User, user_id or username perm – Instance of Permission, or permission_name

  `revoke_user_group_permission`(target_user_group, user_group)

    Revoke user group permission for given target_user_group

    Parameters: | target_user_group – user_group –

  `revoke_user_permission`(user_group, user)

    Revoke permission for user on given repository group

    Parameters: | user_group – Instance of RepoGroup, repositories_group_id, or repositories_group name user – Instance of User, user_id or username