##// END OF EJS Templates
db: add migration for repo_group_id on integrations, version 56
dan -
r673:8d3bae78 default
parent child Browse files
Show More
This diff has been collapsed as it changes many lines, (3506 lines changed) Show them Hide them
@@ -0,0 +1,3506 b''
1 # -*- coding: utf-8 -*-
2
3 # Copyright (C) 2010-2016 RhodeCode GmbH
4 #
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU Affero General Public License, version 3
7 # (only), as published by the Free Software Foundation.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU Affero General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16 #
17 # This program is dual-licensed. If you wish to learn more about the
18 # RhodeCode Enterprise Edition, including its added features, Support services,
19 # and proprietary license terms, please see https://rhodecode.com/licenses/
20
21 """
22 Database Models for RhodeCode Enterprise
23 """
24
25 import os
26 import sys
27 import time
28 import hashlib
29 import logging
30 import datetime
31 import warnings
32 import ipaddress
33 import functools
34 import traceback
35 import collections
36
37
38 from sqlalchemy import *
39 from sqlalchemy.exc import IntegrityError
40 from sqlalchemy.ext.declarative import declared_attr
41 from sqlalchemy.ext.hybrid import hybrid_property
42 from sqlalchemy.orm import (
43 relationship, joinedload, class_mapper, validates, aliased)
44 from sqlalchemy.sql.expression import true
45 from beaker.cache import cache_region, region_invalidate
46 from webob.exc import HTTPNotFound
47 from zope.cachedescriptors.property import Lazy as LazyProperty
48
49 from pylons import url
50 from pylons.i18n.translation import lazy_ugettext as _
51
52 from rhodecode.lib.vcs import get_backend, get_vcs_instance
53 from rhodecode.lib.vcs.utils.helpers import get_scm
54 from rhodecode.lib.vcs.exceptions import VCSError
55 from rhodecode.lib.vcs.backends.base import (
56 EmptyCommit, Reference, MergeFailureReason)
57 from rhodecode.lib.utils2 import (
58 str2bool, safe_str, get_commit_safe, safe_unicode, remove_prefix, md5_safe,
59 time_to_datetime, aslist, Optional, safe_int, get_clone_url, AttributeDict)
60 from rhodecode.lib.jsonalchemy import MutationObj, JsonType, JSONDict
61 from rhodecode.lib.ext_json import json
62 from rhodecode.lib.caching_query import FromCache
63 from rhodecode.lib.encrypt import AESCipher
64
65 from rhodecode.model.meta import Base, Session
66
67 URL_SEP = '/'
68 log = logging.getLogger(__name__)
69
70 # =============================================================================
71 # BASE CLASSES
72 # =============================================================================
73
74 # this is propagated from .ini file rhodecode.encrypted_values.secret or
75 # beaker.session.secret if first is not set.
76 # and initialized at environment.py
77 ENCRYPTION_KEY = None
78
79 # used to sort permissions by types, '#' used here is not allowed to be in
80 # usernames, and it's very early in sorted string.printable table.
81 PERMISSION_TYPE_SORT = {
82 'admin': '####',
83 'write': '###',
84 'read': '##',
85 'none': '#',
86 }
87
88
89 def display_sort(obj):
90 """
91 Sort function used to sort permissions in .permissions() function of
92 Repository, RepoGroup, UserGroup. Also it put the default user in front
93 of all other resources
94 """
95
96 if obj.username == User.DEFAULT_USER:
97 return '#####'
98 prefix = PERMISSION_TYPE_SORT.get(obj.permission.split('.')[-1], '')
99 return prefix + obj.username
100
101
102 def _hash_key(k):
103 return md5_safe(k)
104
105
106 class EncryptedTextValue(TypeDecorator):
107 """
108 Special column for encrypted long text data, use like::
109
110 value = Column("encrypted_value", EncryptedValue(), nullable=False)
111
112 This column is intelligent so if value is in unencrypted form it return
113 unencrypted form, but on save it always encrypts
114 """
115 impl = Text
116
117 def process_bind_param(self, value, dialect):
118 if not value:
119 return value
120 if value.startswith('enc$aes$') or value.startswith('enc$aes_hmac$'):
121 # protect against double encrypting if someone manually starts
122 # doing
123 raise ValueError('value needs to be in unencrypted format, ie. '
124 'not starting with enc$aes')
125 return 'enc$aes_hmac$%s' % AESCipher(
126 ENCRYPTION_KEY, hmac=True).encrypt(value)
127
128 def process_result_value(self, value, dialect):
129 import rhodecode
130
131 if not value:
132 return value
133
134 parts = value.split('$', 3)
135 if not len(parts) == 3:
136 # probably not encrypted values
137 return value
138 else:
139 if parts[0] != 'enc':
140 # parts ok but without our header ?
141 return value
142 enc_strict_mode = str2bool(rhodecode.CONFIG.get(
143 'rhodecode.encrypted_values.strict') or True)
144 # at that stage we know it's our encryption
145 if parts[1] == 'aes':
146 decrypted_data = AESCipher(ENCRYPTION_KEY).decrypt(parts[2])
147 elif parts[1] == 'aes_hmac':
148 decrypted_data = AESCipher(
149 ENCRYPTION_KEY, hmac=True,
150 strict_verification=enc_strict_mode).decrypt(parts[2])
151 else:
152 raise ValueError(
153 'Encryption type part is wrong, must be `aes` '
154 'or `aes_hmac`, got `%s` instead' % (parts[1]))
155 return decrypted_data
156
157
158 class BaseModel(object):
159 """
160 Base Model for all classes
161 """
162
163 @classmethod
164 def _get_keys(cls):
165 """return column names for this model """
166 return class_mapper(cls).c.keys()
167
168 def get_dict(self):
169 """
170 return dict with keys and values corresponding
171 to this model data """
172
173 d = {}
174 for k in self._get_keys():
175 d[k] = getattr(self, k)
176
177 # also use __json__() if present to get additional fields
178 _json_attr = getattr(self, '__json__', None)
179 if _json_attr:
180 # update with attributes from __json__
181 if callable(_json_attr):
182 _json_attr = _json_attr()
183 for k, val in _json_attr.iteritems():
184 d[k] = val
185 return d
186
187 def get_appstruct(self):
188 """return list with keys and values tuples corresponding
189 to this model data """
190
191 l = []
192 for k in self._get_keys():
193 l.append((k, getattr(self, k),))
194 return l
195
196 def populate_obj(self, populate_dict):
197 """populate model with data from given populate_dict"""
198
199 for k in self._get_keys():
200 if k in populate_dict:
201 setattr(self, k, populate_dict[k])
202
203 @classmethod
204 def query(cls):
205 return Session().query(cls)
206
207 @classmethod
208 def get(cls, id_):
209 if id_:
210 return cls.query().get(id_)
211
212 @classmethod
213 def get_or_404(cls, id_):
214 try:
215 id_ = int(id_)
216 except (TypeError, ValueError):
217 raise HTTPNotFound
218
219 res = cls.query().get(id_)
220 if not res:
221 raise HTTPNotFound
222 return res
223
224 @classmethod
225 def getAll(cls):
226 # deprecated and left for backward compatibility
227 return cls.get_all()
228
229 @classmethod
230 def get_all(cls):
231 return cls.query().all()
232
233 @classmethod
234 def delete(cls, id_):
235 obj = cls.query().get(id_)
236 Session().delete(obj)
237
238 @classmethod
239 def identity_cache(cls, session, attr_name, value):
240 exist_in_session = []
241 for (item_cls, pkey), instance in session.identity_map.items():
242 if cls == item_cls and getattr(instance, attr_name) == value:
243 exist_in_session.append(instance)
244 if exist_in_session:
245 if len(exist_in_session) == 1:
246 return exist_in_session[0]
247 log.exception(
248 'multiple objects with attr %s and '
249 'value %s found with same name: %r',
250 attr_name, value, exist_in_session)
251
252 def __repr__(self):
253 if hasattr(self, '__unicode__'):
254 # python repr needs to return str
255 try:
256 return safe_str(self.__unicode__())
257 except UnicodeDecodeError:
258 pass
259 return '<DB:%s>' % (self.__class__.__name__)
260
261
262 class RhodeCodeSetting(Base, BaseModel):
263 __tablename__ = 'rhodecode_settings'
264 __table_args__ = (
265 UniqueConstraint('app_settings_name'),
266 {'extend_existing': True, 'mysql_engine': 'InnoDB',
267 'mysql_charset': 'utf8', 'sqlite_autoincrement': True}
268 )
269
270 SETTINGS_TYPES = {
271 'str': safe_str,
272 'int': safe_int,
273 'unicode': safe_unicode,
274 'bool': str2bool,
275 'list': functools.partial(aslist, sep=',')
276 }
277 DEFAULT_UPDATE_URL = 'https://rhodecode.com/api/v1/info/versions'
278 GLOBAL_CONF_KEY = 'app_settings'
279
280 app_settings_id = Column("app_settings_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
281 app_settings_name = Column("app_settings_name", String(255), nullable=True, unique=None, default=None)
282 _app_settings_value = Column("app_settings_value", String(4096), nullable=True, unique=None, default=None)
283 _app_settings_type = Column("app_settings_type", String(255), nullable=True, unique=None, default=None)
284
285 def __init__(self, key='', val='', type='unicode'):
286 self.app_settings_name = key
287 self.app_settings_type = type
288 self.app_settings_value = val
289
290 @validates('_app_settings_value')
291 def validate_settings_value(self, key, val):
292 assert type(val) == unicode
293 return val
294
295 @hybrid_property
296 def app_settings_value(self):
297 v = self._app_settings_value
298 _type = self.app_settings_type
299 if _type:
300 _type = self.app_settings_type.split('.')[0]
301 # decode the encrypted value
302 if 'encrypted' in self.app_settings_type:
303 cipher = EncryptedTextValue()
304 v = safe_unicode(cipher.process_result_value(v, None))
305
306 converter = self.SETTINGS_TYPES.get(_type) or \
307 self.SETTINGS_TYPES['unicode']
308 return converter(v)
309
310 @app_settings_value.setter
311 def app_settings_value(self, val):
312 """
313 Setter that will always make sure we use unicode in app_settings_value
314
315 :param val:
316 """
317 val = safe_unicode(val)
318 # encode the encrypted value
319 if 'encrypted' in self.app_settings_type:
320 cipher = EncryptedTextValue()
321 val = safe_unicode(cipher.process_bind_param(val, None))
322 self._app_settings_value = val
323
324 @hybrid_property
325 def app_settings_type(self):
326 return self._app_settings_type
327
328 @app_settings_type.setter
329 def app_settings_type(self, val):
330 if val.split('.')[0] not in self.SETTINGS_TYPES:
331 raise Exception('type must be one of %s got %s'
332 % (self.SETTINGS_TYPES.keys(), val))
333 self._app_settings_type = val
334
335 def __unicode__(self):
336 return u"<%s('%s:%s[%s]')>" % (
337 self.__class__.__name__,
338 self.app_settings_name, self.app_settings_value,
339 self.app_settings_type
340 )
341
342
343 class RhodeCodeUi(Base, BaseModel):
344 __tablename__ = 'rhodecode_ui'
345 __table_args__ = (
346 UniqueConstraint('ui_key'),
347 {'extend_existing': True, 'mysql_engine': 'InnoDB',
348 'mysql_charset': 'utf8', 'sqlite_autoincrement': True}
349 )
350
351 HOOK_REPO_SIZE = 'changegroup.repo_size'
352 # HG
353 HOOK_PRE_PULL = 'preoutgoing.pre_pull'
354 HOOK_PULL = 'outgoing.pull_logger'
355 HOOK_PRE_PUSH = 'prechangegroup.pre_push'
356 HOOK_PUSH = 'changegroup.push_logger'
357
358 # TODO: johbo: Unify way how hooks are configured for git and hg,
359 # git part is currently hardcoded.
360
361 # SVN PATTERNS
362 SVN_BRANCH_ID = 'vcs_svn_branch'
363 SVN_TAG_ID = 'vcs_svn_tag'
364
365 ui_id = Column(
366 "ui_id", Integer(), nullable=False, unique=True, default=None,
367 primary_key=True)
368 ui_section = Column(
369 "ui_section", String(255), nullable=True, unique=None, default=None)
370 ui_key = Column(
371 "ui_key", String(255), nullable=True, unique=None, default=None)
372 ui_value = Column(
373 "ui_value", String(255), nullable=True, unique=None, default=None)
374 ui_active = Column(
375 "ui_active", Boolean(), nullable=True, unique=None, default=True)
376
377 def __repr__(self):
378 return '<%s[%s]%s=>%s]>' % (self.__class__.__name__, self.ui_section,
379 self.ui_key, self.ui_value)
380
381
382 class RepoRhodeCodeSetting(Base, BaseModel):
383 __tablename__ = 'repo_rhodecode_settings'
384 __table_args__ = (
385 UniqueConstraint(
386 'app_settings_name', 'repository_id',
387 name='uq_repo_rhodecode_setting_name_repo_id'),
388 {'extend_existing': True, 'mysql_engine': 'InnoDB',
389 'mysql_charset': 'utf8', 'sqlite_autoincrement': True}
390 )
391
392 repository_id = Column(
393 "repository_id", Integer(), ForeignKey('repositories.repo_id'),
394 nullable=False)
395 app_settings_id = Column(
396 "app_settings_id", Integer(), nullable=False, unique=True,
397 default=None, primary_key=True)
398 app_settings_name = Column(
399 "app_settings_name", String(255), nullable=True, unique=None,
400 default=None)
401 _app_settings_value = Column(
402 "app_settings_value", String(4096), nullable=True, unique=None,
403 default=None)
404 _app_settings_type = Column(
405 "app_settings_type", String(255), nullable=True, unique=None,
406 default=None)
407
408 repository = relationship('Repository')
409
410 def __init__(self, repository_id, key='', val='', type='unicode'):
411 self.repository_id = repository_id
412 self.app_settings_name = key
413 self.app_settings_type = type
414 self.app_settings_value = val
415
416 @validates('_app_settings_value')
417 def validate_settings_value(self, key, val):
418 assert type(val) == unicode
419 return val
420
421 @hybrid_property
422 def app_settings_value(self):
423 v = self._app_settings_value
424 type_ = self.app_settings_type
425 SETTINGS_TYPES = RhodeCodeSetting.SETTINGS_TYPES
426 converter = SETTINGS_TYPES.get(type_) or SETTINGS_TYPES['unicode']
427 return converter(v)
428
429 @app_settings_value.setter
430 def app_settings_value(self, val):
431 """
432 Setter that will always make sure we use unicode in app_settings_value
433
434 :param val:
435 """
436 self._app_settings_value = safe_unicode(val)
437
438 @hybrid_property
439 def app_settings_type(self):
440 return self._app_settings_type
441
442 @app_settings_type.setter
443 def app_settings_type(self, val):
444 SETTINGS_TYPES = RhodeCodeSetting.SETTINGS_TYPES
445 if val not in SETTINGS_TYPES:
446 raise Exception('type must be one of %s got %s'
447 % (SETTINGS_TYPES.keys(), val))
448 self._app_settings_type = val
449
450 def __unicode__(self):
451 return u"<%s('%s:%s:%s[%s]')>" % (
452 self.__class__.__name__, self.repository.repo_name,
453 self.app_settings_name, self.app_settings_value,
454 self.app_settings_type
455 )
456
457
458 class RepoRhodeCodeUi(Base, BaseModel):
459 __tablename__ = 'repo_rhodecode_ui'
460 __table_args__ = (
461 UniqueConstraint(
462 'repository_id', 'ui_section', 'ui_key',
463 name='uq_repo_rhodecode_ui_repository_id_section_key'),
464 {'extend_existing': True, 'mysql_engine': 'InnoDB',
465 'mysql_charset': 'utf8', 'sqlite_autoincrement': True}
466 )
467
468 repository_id = Column(
469 "repository_id", Integer(), ForeignKey('repositories.repo_id'),
470 nullable=False)
471 ui_id = Column(
472 "ui_id", Integer(), nullable=False, unique=True, default=None,
473 primary_key=True)
474 ui_section = Column(
475 "ui_section", String(255), nullable=True, unique=None, default=None)
476 ui_key = Column(
477 "ui_key", String(255), nullable=True, unique=None, default=None)
478 ui_value = Column(
479 "ui_value", String(255), nullable=True, unique=None, default=None)
480 ui_active = Column(
481 "ui_active", Boolean(), nullable=True, unique=None, default=True)
482
483 repository = relationship('Repository')
484
485 def __repr__(self):
486 return '<%s[%s:%s]%s=>%s]>' % (
487 self.__class__.__name__, self.repository.repo_name,
488 self.ui_section, self.ui_key, self.ui_value)
489
490
491 class User(Base, BaseModel):
492 __tablename__ = 'users'
493 __table_args__ = (
494 UniqueConstraint('username'), UniqueConstraint('email'),
495 Index('u_username_idx', 'username'),
496 Index('u_email_idx', 'email'),
497 {'extend_existing': True, 'mysql_engine': 'InnoDB',
498 'mysql_charset': 'utf8', 'sqlite_autoincrement': True}
499 )
500 DEFAULT_USER = 'default'
501 DEFAULT_USER_EMAIL = 'anonymous@rhodecode.org'
502 DEFAULT_GRAVATAR_URL = 'https://secure.gravatar.com/avatar/{md5email}?d=identicon&s={size}'
503
504 user_id = Column("user_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
505 username = Column("username", String(255), nullable=True, unique=None, default=None)
506 password = Column("password", String(255), nullable=True, unique=None, default=None)
507 active = Column("active", Boolean(), nullable=True, unique=None, default=True)
508 admin = Column("admin", Boolean(), nullable=True, unique=None, default=False)
509 name = Column("firstname", String(255), nullable=True, unique=None, default=None)
510 lastname = Column("lastname", String(255), nullable=True, unique=None, default=None)
511 _email = Column("email", String(255), nullable=True, unique=None, default=None)
512 last_login = Column("last_login", DateTime(timezone=False), nullable=True, unique=None, default=None)
513 extern_type = Column("extern_type", String(255), nullable=True, unique=None, default=None)
514 extern_name = Column("extern_name", String(255), nullable=True, unique=None, default=None)
515 api_key = Column("api_key", String(255), nullable=True, unique=None, default=None)
516 inherit_default_permissions = Column("inherit_default_permissions", Boolean(), nullable=False, unique=None, default=True)
517 created_on = Column('created_on', DateTime(timezone=False), nullable=False, default=datetime.datetime.now)
518 _user_data = Column("user_data", LargeBinary(), nullable=True) # JSON data
519
520 user_log = relationship('UserLog')
521 user_perms = relationship('UserToPerm', primaryjoin="User.user_id==UserToPerm.user_id", cascade='all')
522
523 repositories = relationship('Repository')
524 repository_groups = relationship('RepoGroup')
525 user_groups = relationship('UserGroup')
526
527 user_followers = relationship('UserFollowing', primaryjoin='UserFollowing.follows_user_id==User.user_id', cascade='all')
528 followings = relationship('UserFollowing', primaryjoin='UserFollowing.user_id==User.user_id', cascade='all')
529
530 repo_to_perm = relationship('UserRepoToPerm', primaryjoin='UserRepoToPerm.user_id==User.user_id', cascade='all')
531 repo_group_to_perm = relationship('UserRepoGroupToPerm', primaryjoin='UserRepoGroupToPerm.user_id==User.user_id', cascade='all')
532 user_group_to_perm = relationship('UserUserGroupToPerm', primaryjoin='UserUserGroupToPerm.user_id==User.user_id', cascade='all')
533
534 group_member = relationship('UserGroupMember', cascade='all')
535
536 notifications = relationship('UserNotification', cascade='all')
537 # notifications assigned to this user
538 user_created_notifications = relationship('Notification', cascade='all')
539 # comments created by this user
540 user_comments = relationship('ChangesetComment', cascade='all')
541 # user profile extra info
542 user_emails = relationship('UserEmailMap', cascade='all')
543 user_ip_map = relationship('UserIpMap', cascade='all')
544 user_auth_tokens = relationship('UserApiKeys', cascade='all')
545 # gists
546 user_gists = relationship('Gist', cascade='all')
547 # user pull requests
548 user_pull_requests = relationship('PullRequest', cascade='all')
549 # external identities
550 extenal_identities = relationship(
551 'ExternalIdentity',
552 primaryjoin="User.user_id==ExternalIdentity.local_user_id",
553 cascade='all')
554
555 def __unicode__(self):
556 return u"<%s('id:%s:%s')>" % (self.__class__.__name__,
557 self.user_id, self.username)
558
559 @hybrid_property
560 def email(self):
561 return self._email
562
563 @email.setter
564 def email(self, val):
565 self._email = val.lower() if val else None
566
567 @property
568 def firstname(self):
569 # alias for future
570 return self.name
571
572 @property
573 def emails(self):
574 other = UserEmailMap.query().filter(UserEmailMap.user==self).all()
575 return [self.email] + [x.email for x in other]
576
577 @property
578 def auth_tokens(self):
579 return [self.api_key] + [x.api_key for x in self.extra_auth_tokens]
580
581 @property
582 def extra_auth_tokens(self):
583 return UserApiKeys.query().filter(UserApiKeys.user == self).all()
584
585 @property
586 def feed_token(self):
587 feed_tokens = UserApiKeys.query()\
588 .filter(UserApiKeys.user == self)\
589 .filter(UserApiKeys.role == UserApiKeys.ROLE_FEED)\
590 .all()
591 if feed_tokens:
592 return feed_tokens[0].api_key
593 else:
594 # use the main token so we don't end up with nothing...
595 return self.api_key
596
597 @classmethod
598 def extra_valid_auth_tokens(cls, user, role=None):
599 tokens = UserApiKeys.query().filter(UserApiKeys.user == user)\
600 .filter(or_(UserApiKeys.expires == -1,
601 UserApiKeys.expires >= time.time()))
602 if role:
603 tokens = tokens.filter(or_(UserApiKeys.role == role,
604 UserApiKeys.role == UserApiKeys.ROLE_ALL))
605 return tokens.all()
606
607 @property
608 def ip_addresses(self):
609 ret = UserIpMap.query().filter(UserIpMap.user == self).all()
610 return [x.ip_addr for x in ret]
611
612 @property
613 def username_and_name(self):
614 return '%s (%s %s)' % (self.username, self.firstname, self.lastname)
615
616 @property
617 def username_or_name_or_email(self):
618 full_name = self.full_name if self.full_name is not ' ' else None
619 return self.username or full_name or self.email
620
621 @property
622 def full_name(self):
623 return '%s %s' % (self.firstname, self.lastname)
624
625 @property
626 def full_name_or_username(self):
627 return ('%s %s' % (self.firstname, self.lastname)
628 if (self.firstname and self.lastname) else self.username)
629
630 @property
631 def full_contact(self):
632 return '%s %s <%s>' % (self.firstname, self.lastname, self.email)
633
634 @property
635 def short_contact(self):
636 return '%s %s' % (self.firstname, self.lastname)
637
638 @property
639 def is_admin(self):
640 return self.admin
641
642 @property
643 def AuthUser(self):
644 """
645 Returns instance of AuthUser for this user
646 """
647 from rhodecode.lib.auth import AuthUser
648 return AuthUser(user_id=self.user_id, api_key=self.api_key,
649 username=self.username)
650
651 @hybrid_property
652 def user_data(self):
653 if not self._user_data:
654 return {}
655
656 try:
657 return json.loads(self._user_data)
658 except TypeError:
659 return {}
660
661 @user_data.setter
662 def user_data(self, val):
663 if not isinstance(val, dict):
664 raise Exception('user_data must be dict, got %s' % type(val))
665 try:
666 self._user_data = json.dumps(val)
667 except Exception:
668 log.error(traceback.format_exc())
669
670 @classmethod
671 def get_by_username(cls, username, case_insensitive=False,
672 cache=False, identity_cache=False):
673 session = Session()
674
675 if case_insensitive:
676 q = cls.query().filter(
677 func.lower(cls.username) == func.lower(username))
678 else:
679 q = cls.query().filter(cls.username == username)
680
681 if cache:
682 if identity_cache:
683 val = cls.identity_cache(session, 'username', username)
684 if val:
685 return val
686 else:
687 q = q.options(
688 FromCache("sql_cache_short",
689 "get_user_by_name_%s" % _hash_key(username)))
690
691 return q.scalar()
692
693 @classmethod
694 def get_by_auth_token(cls, auth_token, cache=False, fallback=True):
695 q = cls.query().filter(cls.api_key == auth_token)
696
697 if cache:
698 q = q.options(FromCache("sql_cache_short",
699 "get_auth_token_%s" % auth_token))
700 res = q.scalar()
701
702 if fallback and not res:
703 #fallback to additional keys
704 _res = UserApiKeys.query()\
705 .filter(UserApiKeys.api_key == auth_token)\
706 .filter(or_(UserApiKeys.expires == -1,
707 UserApiKeys.expires >= time.time()))\
708 .first()
709 if _res:
710 res = _res.user
711 return res
712
713 @classmethod
714 def get_by_email(cls, email, case_insensitive=False, cache=False):
715
716 if case_insensitive:
717 q = cls.query().filter(func.lower(cls.email) == func.lower(email))
718
719 else:
720 q = cls.query().filter(cls.email == email)
721
722 if cache:
723 q = q.options(FromCache("sql_cache_short",
724 "get_email_key_%s" % _hash_key(email)))
725
726 ret = q.scalar()
727 if ret is None:
728 q = UserEmailMap.query()
729 # try fetching in alternate email map
730 if case_insensitive:
731 q = q.filter(func.lower(UserEmailMap.email) == func.lower(email))
732 else:
733 q = q.filter(UserEmailMap.email == email)
734 q = q.options(joinedload(UserEmailMap.user))
735 if cache:
736 q = q.options(FromCache("sql_cache_short",
737 "get_email_map_key_%s" % email))
738 ret = getattr(q.scalar(), 'user', None)
739
740 return ret
741
742 @classmethod
743 def get_from_cs_author(cls, author):
744 """
745 Tries to get User objects out of commit author string
746
747 :param author:
748 """
749 from rhodecode.lib.helpers import email, author_name
750 # Valid email in the attribute passed, see if they're in the system
751 _email = email(author)
752 if _email:
753 user = cls.get_by_email(_email, case_insensitive=True)
754 if user:
755 return user
756 # Maybe we can match by username?
757 _author = author_name(author)
758 user = cls.get_by_username(_author, case_insensitive=True)
759 if user:
760 return user
761
762 def update_userdata(self, **kwargs):
763 usr = self
764 old = usr.user_data
765 old.update(**kwargs)
766 usr.user_data = old
767 Session().add(usr)
768 log.debug('updated userdata with ', kwargs)
769
770 def update_lastlogin(self):
771 """Update user lastlogin"""
772 self.last_login = datetime.datetime.now()
773 Session().add(self)
774 log.debug('updated user %s lastlogin', self.username)
775
776 def update_lastactivity(self):
777 """Update user lastactivity"""
778 usr = self
779 old = usr.user_data
780 old.update({'last_activity': time.time()})
781 usr.user_data = old
782 Session().add(usr)
783 log.debug('updated user %s lastactivity', usr.username)
784
785 def update_password(self, new_password, change_api_key=False):
786 from rhodecode.lib.auth import get_crypt_password,generate_auth_token
787
788 self.password = get_crypt_password(new_password)
789 if change_api_key:
790 self.api_key = generate_auth_token(self.username)
791 Session().add(self)
792
793 @classmethod
794 def get_first_super_admin(cls):
795 user = User.query().filter(User.admin == true()).first()
796 if user is None:
797 raise Exception('FATAL: Missing administrative account!')
798 return user
799
800 @classmethod
801 def get_all_super_admins(cls):
802 """
803 Returns all admin accounts sorted by username
804 """
805 return User.query().filter(User.admin == true())\
806 .order_by(User.username.asc()).all()
807
808 @classmethod
809 def get_default_user(cls, cache=False):
810 user = User.get_by_username(User.DEFAULT_USER, cache=cache)
811 if user is None:
812 raise Exception('FATAL: Missing default account!')
813 return user
814
815 def _get_default_perms(self, user, suffix=''):
816 from rhodecode.model.permission import PermissionModel
817 return PermissionModel().get_default_perms(user.user_perms, suffix)
818
819 def get_default_perms(self, suffix=''):
820 return self._get_default_perms(self, suffix)
821
822 def get_api_data(self, include_secrets=False, details='full'):
823 """
824 Common function for generating user related data for API
825
826 :param include_secrets: By default secrets in the API data will be replaced
827 by a placeholder value to prevent exposing this data by accident. In case
828 this data shall be exposed, set this flag to ``True``.
829
830 :param details: details can be 'basic|full' basic gives only a subset of
831 the available user information that includes user_id, name and emails.
832 """
833 user = self
834 user_data = self.user_data
835 data = {
836 'user_id': user.user_id,
837 'username': user.username,
838 'firstname': user.name,
839 'lastname': user.lastname,
840 'email': user.email,
841 'emails': user.emails,
842 }
843 if details == 'basic':
844 return data
845
846 api_key_length = 40
847 api_key_replacement = '*' * api_key_length
848
849 extras = {
850 'api_key': api_key_replacement,
851 'api_keys': [api_key_replacement],
852 'active': user.active,
853 'admin': user.admin,
854 'extern_type': user.extern_type,
855 'extern_name': user.extern_name,
856 'last_login': user.last_login,
857 'ip_addresses': user.ip_addresses,
858 'language': user_data.get('language')
859 }
860 data.update(extras)
861
862 if include_secrets:
863 data['api_key'] = user.api_key
864 data['api_keys'] = user.auth_tokens
865 return data
866
867 def __json__(self):
868 data = {
869 'full_name': self.full_name,
870 'full_name_or_username': self.full_name_or_username,
871 'short_contact': self.short_contact,
872 'full_contact': self.full_contact,
873 }
874 data.update(self.get_api_data())
875 return data
876
877
878 class UserApiKeys(Base, BaseModel):
879 __tablename__ = 'user_api_keys'
880 __table_args__ = (
881 Index('uak_api_key_idx', 'api_key'),
882 Index('uak_api_key_expires_idx', 'api_key', 'expires'),
883 UniqueConstraint('api_key'),
884 {'extend_existing': True, 'mysql_engine': 'InnoDB',
885 'mysql_charset': 'utf8', 'sqlite_autoincrement': True}
886 )
887 __mapper_args__ = {}
888
889 # ApiKey role
890 ROLE_ALL = 'token_role_all'
891 ROLE_HTTP = 'token_role_http'
892 ROLE_VCS = 'token_role_vcs'
893 ROLE_API = 'token_role_api'
894 ROLE_FEED = 'token_role_feed'
895 ROLES = [ROLE_ALL, ROLE_HTTP, ROLE_VCS, ROLE_API, ROLE_FEED]
896
897 user_api_key_id = Column("user_api_key_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
898 user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=True, unique=None, default=None)
899 api_key = Column("api_key", String(255), nullable=False, unique=True)
900 description = Column('description', UnicodeText().with_variant(UnicodeText(1024), 'mysql'))
901 expires = Column('expires', Float(53), nullable=False)
902 role = Column('role', String(255), nullable=True)
903 created_on = Column('created_on', DateTime(timezone=False), nullable=False, default=datetime.datetime.now)
904
905 user = relationship('User', lazy='joined')
906
907 @classmethod
908 def _get_role_name(cls, role):
909 return {
910 cls.ROLE_ALL: _('all'),
911 cls.ROLE_HTTP: _('http/web interface'),
912 cls.ROLE_VCS: _('vcs (git/hg/svn protocol)'),
913 cls.ROLE_API: _('api calls'),
914 cls.ROLE_FEED: _('feed access'),
915 }.get(role, role)
916
917 @property
918 def expired(self):
919 if self.expires == -1:
920 return False
921 return time.time() > self.expires
922
923 @property
924 def role_humanized(self):
925 return self._get_role_name(self.role)
926
927
928 class UserEmailMap(Base, BaseModel):
929 __tablename__ = 'user_email_map'
930 __table_args__ = (
931 Index('uem_email_idx', 'email'),
932 UniqueConstraint('email'),
933 {'extend_existing': True, 'mysql_engine': 'InnoDB',
934 'mysql_charset': 'utf8', 'sqlite_autoincrement': True}
935 )
936 __mapper_args__ = {}
937
938 email_id = Column("email_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
939 user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=True, unique=None, default=None)
940 _email = Column("email", String(255), nullable=True, unique=False, default=None)
941 user = relationship('User', lazy='joined')
942
943 @validates('_email')
944 def validate_email(self, key, email):
945 # check if this email is not main one
946 main_email = Session().query(User).filter(User.email == email).scalar()
947 if main_email is not None:
948 raise AttributeError('email %s is present is user table' % email)
949 return email
950
951 @hybrid_property
952 def email(self):
953 return self._email
954
955 @email.setter
956 def email(self, val):
957 self._email = val.lower() if val else None
958
959
960 class UserIpMap(Base, BaseModel):
961 __tablename__ = 'user_ip_map'
962 __table_args__ = (
963 UniqueConstraint('user_id', 'ip_addr'),
964 {'extend_existing': True, 'mysql_engine': 'InnoDB',
965 'mysql_charset': 'utf8', 'sqlite_autoincrement': True}
966 )
967 __mapper_args__ = {}
968
969 ip_id = Column("ip_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
970 user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=True, unique=None, default=None)
971 ip_addr = Column("ip_addr", String(255), nullable=True, unique=False, default=None)
972 active = Column("active", Boolean(), nullable=True, unique=None, default=True)
973 description = Column("description", String(10000), nullable=True, unique=None, default=None)
974 user = relationship('User', lazy='joined')
975
976 @classmethod
977 def _get_ip_range(cls, ip_addr):
978 net = ipaddress.ip_network(ip_addr, strict=False)
979 return [str(net.network_address), str(net.broadcast_address)]
980
981 def __json__(self):
982 return {
983 'ip_addr': self.ip_addr,
984 'ip_range': self._get_ip_range(self.ip_addr),
985 }
986
987 def __unicode__(self):
988 return u"<%s('user_id:%s=>%s')>" % (self.__class__.__name__,
989 self.user_id, self.ip_addr)
990
991 class UserLog(Base, BaseModel):
992 __tablename__ = 'user_logs'
993 __table_args__ = (
994 {'extend_existing': True, 'mysql_engine': 'InnoDB',
995 'mysql_charset': 'utf8', 'sqlite_autoincrement': True},
996 )
997 user_log_id = Column("user_log_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
998 user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=True, unique=None, default=None)
999 username = Column("username", String(255), nullable=True, unique=None, default=None)
1000 repository_id = Column("repository_id", Integer(), ForeignKey('repositories.repo_id'), nullable=True)
1001 repository_name = Column("repository_name", String(255), nullable=True, unique=None, default=None)
1002 user_ip = Column("user_ip", String(255), nullable=True, unique=None, default=None)
1003 action = Column("action", Text().with_variant(Text(1200000), 'mysql'), nullable=True, unique=None, default=None)
1004 action_date = Column("action_date", DateTime(timezone=False), nullable=True, unique=None, default=None)
1005
1006 def __unicode__(self):
1007 return u"<%s('id:%s:%s')>" % (self.__class__.__name__,
1008 self.repository_name,
1009 self.action)
1010
1011 @property
1012 def action_as_day(self):
1013 return datetime.date(*self.action_date.timetuple()[:3])
1014
1015 user = relationship('User')
1016 repository = relationship('Repository', cascade='')
1017
1018
1019 class UserGroup(Base, BaseModel):
1020 __tablename__ = 'users_groups'
1021 __table_args__ = (
1022 {'extend_existing': True, 'mysql_engine': 'InnoDB',
1023 'mysql_charset': 'utf8', 'sqlite_autoincrement': True},
1024 )
1025
1026 users_group_id = Column("users_group_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
1027 users_group_name = Column("users_group_name", String(255), nullable=False, unique=True, default=None)
1028 user_group_description = Column("user_group_description", String(10000), nullable=True, unique=None, default=None)
1029 users_group_active = Column("users_group_active", Boolean(), nullable=True, unique=None, default=None)
1030 inherit_default_permissions = Column("users_group_inherit_default_permissions", Boolean(), nullable=False, unique=None, default=True)
1031 user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=False, default=None)
1032 created_on = Column('created_on', DateTime(timezone=False), nullable=False, default=datetime.datetime.now)
1033 _group_data = Column("group_data", LargeBinary(), nullable=True) # JSON data
1034
1035 members = relationship('UserGroupMember', cascade="all, delete, delete-orphan", lazy="joined")
1036 users_group_to_perm = relationship('UserGroupToPerm', cascade='all')
1037 users_group_repo_to_perm = relationship('UserGroupRepoToPerm', cascade='all')
1038 users_group_repo_group_to_perm = relationship('UserGroupRepoGroupToPerm', cascade='all')
1039 user_user_group_to_perm = relationship('UserUserGroupToPerm', cascade='all')
1040 user_group_user_group_to_perm = relationship('UserGroupUserGroupToPerm ', primaryjoin="UserGroupUserGroupToPerm.target_user_group_id==UserGroup.users_group_id", cascade='all')
1041
1042 user = relationship('User')
1043
1044 @hybrid_property
1045 def group_data(self):
1046 if not self._group_data:
1047 return {}
1048
1049 try:
1050 return json.loads(self._group_data)
1051 except TypeError:
1052 return {}
1053
1054 @group_data.setter
1055 def group_data(self, val):
1056 try:
1057 self._group_data = json.dumps(val)
1058 except Exception:
1059 log.error(traceback.format_exc())
1060
1061 def __unicode__(self):
1062 return u"<%s('id:%s:%s')>" % (self.__class__.__name__,
1063 self.users_group_id,
1064 self.users_group_name)
1065
1066 @classmethod
1067 def get_by_group_name(cls, group_name, cache=False,
1068 case_insensitive=False):
1069 if case_insensitive:
1070 q = cls.query().filter(func.lower(cls.users_group_name) ==
1071 func.lower(group_name))
1072
1073 else:
1074 q = cls.query().filter(cls.users_group_name == group_name)
1075 if cache:
1076 q = q.options(FromCache(
1077 "sql_cache_short",
1078 "get_group_%s" % _hash_key(group_name)))
1079 return q.scalar()
1080
1081 @classmethod
1082 def get(cls, user_group_id, cache=False):
1083 user_group = cls.query()
1084 if cache:
1085 user_group = user_group.options(FromCache("sql_cache_short",
1086 "get_users_group_%s" % user_group_id))
1087 return user_group.get(user_group_id)
1088
1089 def permissions(self, with_admins=True, with_owner=True):
1090 q = UserUserGroupToPerm.query().filter(UserUserGroupToPerm.user_group == self)
1091 q = q.options(joinedload(UserUserGroupToPerm.user_group),
1092 joinedload(UserUserGroupToPerm.user),
1093 joinedload(UserUserGroupToPerm.permission),)
1094
1095 # get owners and admins and permissions. We do a trick of re-writing
1096 # objects from sqlalchemy to named-tuples due to sqlalchemy session
1097 # has a global reference and changing one object propagates to all
1098 # others. This means if admin is also an owner admin_row that change
1099 # would propagate to both objects
1100 perm_rows = []
1101 for _usr in q.all():
1102 usr = AttributeDict(_usr.user.get_dict())
1103 usr.permission = _usr.permission.permission_name
1104 perm_rows.append(usr)
1105
1106 # filter the perm rows by 'default' first and then sort them by
1107 # admin,write,read,none permissions sorted again alphabetically in
1108 # each group
1109 perm_rows = sorted(perm_rows, key=display_sort)
1110
1111 _admin_perm = 'usergroup.admin'
1112 owner_row = []
1113 if with_owner:
1114 usr = AttributeDict(self.user.get_dict())
1115 usr.owner_row = True
1116 usr.permission = _admin_perm
1117 owner_row.append(usr)
1118
1119 super_admin_rows = []
1120 if with_admins:
1121 for usr in User.get_all_super_admins():
1122 # if this admin is also owner, don't double the record
1123 if usr.user_id == owner_row[0].user_id:
1124 owner_row[0].admin_row = True
1125 else:
1126 usr = AttributeDict(usr.get_dict())
1127 usr.admin_row = True
1128 usr.permission = _admin_perm
1129 super_admin_rows.append(usr)
1130
1131 return super_admin_rows + owner_row + perm_rows
1132
1133 def permission_user_groups(self):
1134 q = UserGroupUserGroupToPerm.query().filter(UserGroupUserGroupToPerm.target_user_group == self)
1135 q = q.options(joinedload(UserGroupUserGroupToPerm.user_group),
1136 joinedload(UserGroupUserGroupToPerm.target_user_group),
1137 joinedload(UserGroupUserGroupToPerm.permission),)
1138
1139 perm_rows = []
1140 for _user_group in q.all():
1141 usr = AttributeDict(_user_group.user_group.get_dict())
1142 usr.permission = _user_group.permission.permission_name
1143 perm_rows.append(usr)
1144
1145 return perm_rows
1146
1147 def _get_default_perms(self, user_group, suffix=''):
1148 from rhodecode.model.permission import PermissionModel
1149 return PermissionModel().get_default_perms(user_group.users_group_to_perm, suffix)
1150
1151 def get_default_perms(self, suffix=''):
1152 return self._get_default_perms(self, suffix)
1153
1154 def get_api_data(self, with_group_members=True, include_secrets=False):
1155 """
1156 :param include_secrets: See :meth:`User.get_api_data`, this parameter is
1157 basically forwarded.
1158
1159 """
1160 user_group = self
1161
1162 data = {
1163 'users_group_id': user_group.users_group_id,
1164 'group_name': user_group.users_group_name,
1165 'group_description': user_group.user_group_description,
1166 'active': user_group.users_group_active,
1167 'owner': user_group.user.username,
1168 }
1169 if with_group_members:
1170 users = []
1171 for user in user_group.members:
1172 user = user.user
1173 users.append(user.get_api_data(include_secrets=include_secrets))
1174 data['users'] = users
1175
1176 return data
1177
1178
1179 class UserGroupMember(Base, BaseModel):
1180 __tablename__ = 'users_groups_members'
1181 __table_args__ = (
1182 {'extend_existing': True, 'mysql_engine': 'InnoDB',
1183 'mysql_charset': 'utf8', 'sqlite_autoincrement': True},
1184 )
1185
1186 users_group_member_id = Column("users_group_member_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
1187 users_group_id = Column("users_group_id", Integer(), ForeignKey('users_groups.users_group_id'), nullable=False, unique=None, default=None)
1188 user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=None, default=None)
1189
1190 user = relationship('User', lazy='joined')
1191 users_group = relationship('UserGroup')
1192
1193 def __init__(self, gr_id='', u_id=''):
1194 self.users_group_id = gr_id
1195 self.user_id = u_id
1196
1197
1198 class RepositoryField(Base, BaseModel):
1199 __tablename__ = 'repositories_fields'
1200 __table_args__ = (
1201 UniqueConstraint('repository_id', 'field_key'), # no-multi field
1202 {'extend_existing': True, 'mysql_engine': 'InnoDB',
1203 'mysql_charset': 'utf8', 'sqlite_autoincrement': True},
1204 )
1205 PREFIX = 'ex_' # prefix used in form to not conflict with already existing fields
1206
1207 repo_field_id = Column("repo_field_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
1208 repository_id = Column("repository_id", Integer(), ForeignKey('repositories.repo_id'), nullable=False, unique=None, default=None)
1209 field_key = Column("field_key", String(250))
1210 field_label = Column("field_label", String(1024), nullable=False)
1211 field_value = Column("field_value", String(10000), nullable=False)
1212 field_desc = Column("field_desc", String(1024), nullable=False)
1213 field_type = Column("field_type", String(255), nullable=False, unique=None)
1214 created_on = Column('created_on', DateTime(timezone=False), nullable=False, default=datetime.datetime.now)
1215
1216 repository = relationship('Repository')
1217
1218 @property
1219 def field_key_prefixed(self):
1220 return 'ex_%s' % self.field_key
1221
1222 @classmethod
1223 def un_prefix_key(cls, key):
1224 if key.startswith(cls.PREFIX):
1225 return key[len(cls.PREFIX):]
1226 return key
1227
1228 @classmethod
1229 def get_by_key_name(cls, key, repo):
1230 row = cls.query()\
1231 .filter(cls.repository == repo)\
1232 .filter(cls.field_key == key).scalar()
1233 return row
1234
1235
1236 class Repository(Base, BaseModel):
1237 __tablename__ = 'repositories'
1238 __table_args__ = (
1239 Index('r_repo_name_idx', 'repo_name', mysql_length=255),
1240 {'extend_existing': True, 'mysql_engine': 'InnoDB',
1241 'mysql_charset': 'utf8', 'sqlite_autoincrement': True},
1242 )
1243 DEFAULT_CLONE_URI = '{scheme}://{user}@{netloc}/{repo}'
1244 DEFAULT_CLONE_URI_ID = '{scheme}://{user}@{netloc}/_{repoid}'
1245
1246 STATE_CREATED = 'repo_state_created'
1247 STATE_PENDING = 'repo_state_pending'
1248 STATE_ERROR = 'repo_state_error'
1249
1250 LOCK_AUTOMATIC = 'lock_auto'
1251 LOCK_API = 'lock_api'
1252 LOCK_WEB = 'lock_web'
1253 LOCK_PULL = 'lock_pull'
1254
1255 NAME_SEP = URL_SEP
1256
1257 repo_id = Column(
1258 "repo_id", Integer(), nullable=False, unique=True, default=None,
1259 primary_key=True)
1260 _repo_name = Column(
1261 "repo_name", Text(), nullable=False, default=None)
1262 _repo_name_hash = Column(
1263 "repo_name_hash", String(255), nullable=False, unique=True)
1264 repo_state = Column("repo_state", String(255), nullable=True)
1265
1266 clone_uri = Column(
1267 "clone_uri", EncryptedTextValue(), nullable=True, unique=False,
1268 default=None)
1269 repo_type = Column(
1270 "repo_type", String(255), nullable=False, unique=False, default=None)
1271 user_id = Column(
1272 "user_id", Integer(), ForeignKey('users.user_id'), nullable=False,
1273 unique=False, default=None)
1274 private = Column(
1275 "private", Boolean(), nullable=True, unique=None, default=None)
1276 enable_statistics = Column(
1277 "statistics", Boolean(), nullable=True, unique=None, default=True)
1278 enable_downloads = Column(
1279 "downloads", Boolean(), nullable=True, unique=None, default=True)
1280 description = Column(
1281 "description", String(10000), nullable=True, unique=None, default=None)
1282 created_on = Column(
1283 'created_on', DateTime(timezone=False), nullable=True, unique=None,
1284 default=datetime.datetime.now)
1285 updated_on = Column(
1286 'updated_on', DateTime(timezone=False), nullable=True, unique=None,
1287 default=datetime.datetime.now)
1288 _landing_revision = Column(
1289 "landing_revision", String(255), nullable=False, unique=False,
1290 default=None)
1291 enable_locking = Column(
1292 "enable_locking", Boolean(), nullable=False, unique=None,
1293 default=False)
1294 _locked = Column(
1295 "locked", String(255), nullable=True, unique=False, default=None)
1296 _changeset_cache = Column(
1297 "changeset_cache", LargeBinary(), nullable=True) # JSON data
1298
1299 fork_id = Column(
1300 "fork_id", Integer(), ForeignKey('repositories.repo_id'),
1301 nullable=True, unique=False, default=None)
1302 group_id = Column(
1303 "group_id", Integer(), ForeignKey('groups.group_id'), nullable=True,
1304 unique=False, default=None)
1305
1306 user = relationship('User', lazy='joined')
1307 fork = relationship('Repository', remote_side=repo_id, lazy='joined')
1308 group = relationship('RepoGroup', lazy='joined')
1309 repo_to_perm = relationship(
1310 'UserRepoToPerm', cascade='all',
1311 order_by='UserRepoToPerm.repo_to_perm_id')
1312 users_group_to_perm = relationship('UserGroupRepoToPerm', cascade='all')
1313 stats = relationship('Statistics', cascade='all', uselist=False)
1314
1315 followers = relationship(
1316 'UserFollowing',
1317 primaryjoin='UserFollowing.follows_repo_id==Repository.repo_id',
1318 cascade='all')
1319 extra_fields = relationship(
1320 'RepositoryField', cascade="all, delete, delete-orphan")
1321 logs = relationship('UserLog')
1322 comments = relationship(
1323 'ChangesetComment', cascade="all, delete, delete-orphan")
1324 pull_requests_source = relationship(
1325 'PullRequest',
1326 primaryjoin='PullRequest.source_repo_id==Repository.repo_id',
1327 cascade="all, delete, delete-orphan")
1328 pull_requests_target = relationship(
1329 'PullRequest',
1330 primaryjoin='PullRequest.target_repo_id==Repository.repo_id',
1331 cascade="all, delete, delete-orphan")
1332 ui = relationship('RepoRhodeCodeUi', cascade="all")
1333 settings = relationship('RepoRhodeCodeSetting', cascade="all")
1334 integrations = relationship('Integration',
1335 cascade="all, delete, delete-orphan")
1336
1337 def __unicode__(self):
1338 return u"<%s('%s:%s')>" % (self.__class__.__name__, self.repo_id,
1339 safe_unicode(self.repo_name))
1340
1341 @hybrid_property
1342 def landing_rev(self):
1343 # always should return [rev_type, rev]
1344 if self._landing_revision:
1345 _rev_info = self._landing_revision.split(':')
1346 if len(_rev_info) < 2:
1347 _rev_info.insert(0, 'rev')
1348 return [_rev_info[0], _rev_info[1]]
1349 return [None, None]
1350
1351 @landing_rev.setter
1352 def landing_rev(self, val):
1353 if ':' not in val:
1354 raise ValueError('value must be delimited with `:` and consist '
1355 'of <rev_type>:<rev>, got %s instead' % val)
1356 self._landing_revision = val
1357
1358 @hybrid_property
1359 def locked(self):
1360 if self._locked:
1361 user_id, timelocked, reason = self._locked.split(':')
1362 lock_values = int(user_id), timelocked, reason
1363 else:
1364 lock_values = [None, None, None]
1365 return lock_values
1366
1367 @locked.setter
1368 def locked(self, val):
1369 if val and isinstance(val, (list, tuple)):
1370 self._locked = ':'.join(map(str, val))
1371 else:
1372 self._locked = None
1373
1374 @hybrid_property
1375 def changeset_cache(self):
1376 from rhodecode.lib.vcs.backends.base import EmptyCommit
1377 dummy = EmptyCommit().__json__()
1378 if not self._changeset_cache:
1379 return dummy
1380 try:
1381 return json.loads(self._changeset_cache)
1382 except TypeError:
1383 return dummy
1384 except Exception:
1385 log.error(traceback.format_exc())
1386 return dummy
1387
1388 @changeset_cache.setter
1389 def changeset_cache(self, val):
1390 try:
1391 self._changeset_cache = json.dumps(val)
1392 except Exception:
1393 log.error(traceback.format_exc())
1394
1395 @hybrid_property
1396 def repo_name(self):
1397 return self._repo_name
1398
1399 @repo_name.setter
1400 def repo_name(self, value):
1401 self._repo_name = value
1402 self._repo_name_hash = hashlib.sha1(safe_str(value)).hexdigest()
1403
1404 @classmethod
1405 def normalize_repo_name(cls, repo_name):
1406 """
1407 Normalizes os specific repo_name to the format internally stored inside
1408 database using URL_SEP
1409
1410 :param cls:
1411 :param repo_name:
1412 """
1413 return cls.NAME_SEP.join(repo_name.split(os.sep))
1414
1415 @classmethod
1416 def get_by_repo_name(cls, repo_name, cache=False, identity_cache=False):
1417 session = Session()
1418 q = session.query(cls).filter(cls.repo_name == repo_name)
1419
1420 if cache:
1421 if identity_cache:
1422 val = cls.identity_cache(session, 'repo_name', repo_name)
1423 if val:
1424 return val
1425 else:
1426 q = q.options(
1427 FromCache("sql_cache_short",
1428 "get_repo_by_name_%s" % _hash_key(repo_name)))
1429
1430 return q.scalar()
1431
1432 @classmethod
1433 def get_by_full_path(cls, repo_full_path):
1434 repo_name = repo_full_path.split(cls.base_path(), 1)[-1]
1435 repo_name = cls.normalize_repo_name(repo_name)
1436 return cls.get_by_repo_name(repo_name.strip(URL_SEP))
1437
1438 @classmethod
1439 def get_repo_forks(cls, repo_id):
1440 return cls.query().filter(Repository.fork_id == repo_id)
1441
1442 @classmethod
1443 def base_path(cls):
1444 """
1445 Returns base path when all repos are stored
1446
1447 :param cls:
1448 """
1449 q = Session().query(RhodeCodeUi)\
1450 .filter(RhodeCodeUi.ui_key == cls.NAME_SEP)
1451 q = q.options(FromCache("sql_cache_short", "repository_repo_path"))
1452 return q.one().ui_value
1453
1454 @classmethod
1455 def is_valid(cls, repo_name):
1456 """
1457 returns True if given repo name is a valid filesystem repository
1458
1459 :param cls:
1460 :param repo_name:
1461 """
1462 from rhodecode.lib.utils import is_valid_repo
1463
1464 return is_valid_repo(repo_name, cls.base_path())
1465
1466 @classmethod
1467 def get_all_repos(cls, user_id=Optional(None), group_id=Optional(None),
1468 case_insensitive=True):
1469 q = Repository.query()
1470
1471 if not isinstance(user_id, Optional):
1472 q = q.filter(Repository.user_id == user_id)
1473
1474 if not isinstance(group_id, Optional):
1475 q = q.filter(Repository.group_id == group_id)
1476
1477 if case_insensitive:
1478 q = q.order_by(func.lower(Repository.repo_name))
1479 else:
1480 q = q.order_by(Repository.repo_name)
1481 return q.all()
1482
1483 @property
1484 def forks(self):
1485 """
1486 Return forks of this repo
1487 """
1488 return Repository.get_repo_forks(self.repo_id)
1489
1490 @property
1491 def parent(self):
1492 """
1493 Returns fork parent
1494 """
1495 return self.fork
1496
1497 @property
1498 def just_name(self):
1499 return self.repo_name.split(self.NAME_SEP)[-1]
1500
1501 @property
1502 def groups_with_parents(self):
1503 groups = []
1504 if self.group is None:
1505 return groups
1506
1507 cur_gr = self.group
1508 groups.insert(0, cur_gr)
1509 while 1:
1510 gr = getattr(cur_gr, 'parent_group', None)
1511 cur_gr = cur_gr.parent_group
1512 if gr is None:
1513 break
1514 groups.insert(0, gr)
1515
1516 return groups
1517
1518 @property
1519 def groups_and_repo(self):
1520 return self.groups_with_parents, self
1521
1522 @LazyProperty
1523 def repo_path(self):
1524 """
1525 Returns base full path for that repository means where it actually
1526 exists on a filesystem
1527 """
1528 q = Session().query(RhodeCodeUi).filter(
1529 RhodeCodeUi.ui_key == self.NAME_SEP)
1530 q = q.options(FromCache("sql_cache_short", "repository_repo_path"))
1531 return q.one().ui_value
1532
1533 @property
1534 def repo_full_path(self):
1535 p = [self.repo_path]
1536 # we need to split the name by / since this is how we store the
1537 # names in the database, but that eventually needs to be converted
1538 # into a valid system path
1539 p += self.repo_name.split(self.NAME_SEP)
1540 return os.path.join(*map(safe_unicode, p))
1541
1542 @property
1543 def cache_keys(self):
1544 """
1545 Returns associated cache keys for that repo
1546 """
1547 return CacheKey.query()\
1548 .filter(CacheKey.cache_args == self.repo_name)\
1549 .order_by(CacheKey.cache_key)\
1550 .all()
1551
1552 def get_new_name(self, repo_name):
1553 """
1554 returns new full repository name based on assigned group and new new
1555
1556 :param group_name:
1557 """
1558 path_prefix = self.group.full_path_splitted if self.group else []
1559 return self.NAME_SEP.join(path_prefix + [repo_name])
1560
1561 @property
1562 def _config(self):
1563 """
1564 Returns db based config object.
1565 """
1566 from rhodecode.lib.utils import make_db_config
1567 return make_db_config(clear_session=False, repo=self)
1568
1569 def permissions(self, with_admins=True, with_owner=True):
1570 q = UserRepoToPerm.query().filter(UserRepoToPerm.repository == self)
1571 q = q.options(joinedload(UserRepoToPerm.repository),
1572 joinedload(UserRepoToPerm.user),
1573 joinedload(UserRepoToPerm.permission),)
1574
1575 # get owners and admins and permissions. We do a trick of re-writing
1576 # objects from sqlalchemy to named-tuples due to sqlalchemy session
1577 # has a global reference and changing one object propagates to all
1578 # others. This means if admin is also an owner admin_row that change
1579 # would propagate to both objects
1580 perm_rows = []
1581 for _usr in q.all():
1582 usr = AttributeDict(_usr.user.get_dict())
1583 usr.permission = _usr.permission.permission_name
1584 perm_rows.append(usr)
1585
1586 # filter the perm rows by 'default' first and then sort them by
1587 # admin,write,read,none permissions sorted again alphabetically in
1588 # each group
1589 perm_rows = sorted(perm_rows, key=display_sort)
1590
1591 _admin_perm = 'repository.admin'
1592 owner_row = []
1593 if with_owner:
1594 usr = AttributeDict(self.user.get_dict())
1595 usr.owner_row = True
1596 usr.permission = _admin_perm
1597 owner_row.append(usr)
1598
1599 super_admin_rows = []
1600 if with_admins:
1601 for usr in User.get_all_super_admins():
1602 # if this admin is also owner, don't double the record
1603 if usr.user_id == owner_row[0].user_id:
1604 owner_row[0].admin_row = True
1605 else:
1606 usr = AttributeDict(usr.get_dict())
1607 usr.admin_row = True
1608 usr.permission = _admin_perm
1609 super_admin_rows.append(usr)
1610
1611 return super_admin_rows + owner_row + perm_rows
1612
1613 def permission_user_groups(self):
1614 q = UserGroupRepoToPerm.query().filter(
1615 UserGroupRepoToPerm.repository == self)
1616 q = q.options(joinedload(UserGroupRepoToPerm.repository),
1617 joinedload(UserGroupRepoToPerm.users_group),
1618 joinedload(UserGroupRepoToPerm.permission),)
1619
1620 perm_rows = []
1621 for _user_group in q.all():
1622 usr = AttributeDict(_user_group.users_group.get_dict())
1623 usr.permission = _user_group.permission.permission_name
1624 perm_rows.append(usr)
1625
1626 return perm_rows
1627
1628 def get_api_data(self, include_secrets=False):
1629 """
1630 Common function for generating repo api data
1631
1632 :param include_secrets: See :meth:`User.get_api_data`.
1633
1634 """
1635 # TODO: mikhail: Here there is an anti-pattern, we probably need to
1636 # move this methods on models level.
1637 from rhodecode.model.settings import SettingsModel
1638
1639 repo = self
1640 _user_id, _time, _reason = self.locked
1641
1642 data = {
1643 'repo_id': repo.repo_id,
1644 'repo_name': repo.repo_name,
1645 'repo_type': repo.repo_type,
1646 'clone_uri': repo.clone_uri or '',
1647 'url': url('summary_home', repo_name=self.repo_name, qualified=True),
1648 'private': repo.private,
1649 'created_on': repo.created_on,
1650 'description': repo.description,
1651 'landing_rev': repo.landing_rev,
1652 'owner': repo.user.username,
1653 'fork_of': repo.fork.repo_name if repo.fork else None,
1654 'enable_statistics': repo.enable_statistics,
1655 'enable_locking': repo.enable_locking,
1656 'enable_downloads': repo.enable_downloads,
1657 'last_changeset': repo.changeset_cache,
1658 'locked_by': User.get(_user_id).get_api_data(
1659 include_secrets=include_secrets) if _user_id else None,
1660 'locked_date': time_to_datetime(_time) if _time else None,
1661 'lock_reason': _reason if _reason else None,
1662 }
1663
1664 # TODO: mikhail: should be per-repo settings here
1665 rc_config = SettingsModel().get_all_settings()
1666 repository_fields = str2bool(
1667 rc_config.get('rhodecode_repository_fields'))
1668 if repository_fields:
1669 for f in self.extra_fields:
1670 data[f.field_key_prefixed] = f.field_value
1671
1672 return data
1673
1674 @classmethod
1675 def lock(cls, repo, user_id, lock_time=None, lock_reason=None):
1676 if not lock_time:
1677 lock_time = time.time()
1678 if not lock_reason:
1679 lock_reason = cls.LOCK_AUTOMATIC
1680 repo.locked = [user_id, lock_time, lock_reason]
1681 Session().add(repo)
1682 Session().commit()
1683
1684 @classmethod
1685 def unlock(cls, repo):
1686 repo.locked = None
1687 Session().add(repo)
1688 Session().commit()
1689
1690 @classmethod
1691 def getlock(cls, repo):
1692 return repo.locked
1693
1694 def is_user_lock(self, user_id):
1695 if self.lock[0]:
1696 lock_user_id = safe_int(self.lock[0])
1697 user_id = safe_int(user_id)
1698 # both are ints, and they are equal
1699 return all([lock_user_id, user_id]) and lock_user_id == user_id
1700
1701 return False
1702
1703 def get_locking_state(self, action, user_id, only_when_enabled=True):
1704 """
1705 Checks locking on this repository, if locking is enabled and lock is
1706 present returns a tuple of make_lock, locked, locked_by.
1707 make_lock can have 3 states None (do nothing) True, make lock
1708 False release lock, This value is later propagated to hooks, which
1709 do the locking. Think about this as signals passed to hooks what to do.
1710
1711 """
1712 # TODO: johbo: This is part of the business logic and should be moved
1713 # into the RepositoryModel.
1714
1715 if action not in ('push', 'pull'):
1716 raise ValueError("Invalid action value: %s" % repr(action))
1717
1718 # defines if locked error should be thrown to user
1719 currently_locked = False
1720 # defines if new lock should be made, tri-state
1721 make_lock = None
1722 repo = self
1723 user = User.get(user_id)
1724
1725 lock_info = repo.locked
1726
1727 if repo and (repo.enable_locking or not only_when_enabled):
1728 if action == 'push':
1729 # check if it's already locked !, if it is compare users
1730 locked_by_user_id = lock_info[0]
1731 if user.user_id == locked_by_user_id:
1732 log.debug(
1733 'Got `push` action from user %s, now unlocking', user)
1734 # unlock if we have push from user who locked
1735 make_lock = False
1736 else:
1737 # we're not the same user who locked, ban with
1738 # code defined in settings (default is 423 HTTP Locked) !
1739 log.debug('Repo %s is currently locked by %s', repo, user)
1740 currently_locked = True
1741 elif action == 'pull':
1742 # [0] user [1] date
1743 if lock_info[0] and lock_info[1]:
1744 log.debug('Repo %s is currently locked by %s', repo, user)
1745 currently_locked = True
1746 else:
1747 log.debug('Setting lock on repo %s by %s', repo, user)
1748 make_lock = True
1749
1750 else:
1751 log.debug('Repository %s do not have locking enabled', repo)
1752
1753 log.debug('FINAL locking values make_lock:%s,locked:%s,locked_by:%s',
1754 make_lock, currently_locked, lock_info)
1755
1756 from rhodecode.lib.auth import HasRepoPermissionAny
1757 perm_check = HasRepoPermissionAny('repository.write', 'repository.admin')
1758 if make_lock and not perm_check(repo_name=repo.repo_name, user=user):
1759 # if we don't have at least write permission we cannot make a lock
1760 log.debug('lock state reset back to FALSE due to lack '
1761 'of at least read permission')
1762 make_lock = False
1763
1764 return make_lock, currently_locked, lock_info
1765
1766 @property
1767 def last_db_change(self):
1768 return self.updated_on
1769
1770 @property
1771 def clone_uri_hidden(self):
1772 clone_uri = self.clone_uri
1773 if clone_uri:
1774 import urlobject
1775 url_obj = urlobject.URLObject(clone_uri)
1776 if url_obj.password:
1777 clone_uri = url_obj.with_password('*****')
1778 return clone_uri
1779
1780 def clone_url(self, **override):
1781 qualified_home_url = url('home', qualified=True)
1782
1783 uri_tmpl = None
1784 if 'with_id' in override:
1785 uri_tmpl = self.DEFAULT_CLONE_URI_ID
1786 del override['with_id']
1787
1788 if 'uri_tmpl' in override:
1789 uri_tmpl = override['uri_tmpl']
1790 del override['uri_tmpl']
1791
1792 # we didn't override our tmpl from **overrides
1793 if not uri_tmpl:
1794 uri_tmpl = self.DEFAULT_CLONE_URI
1795 try:
1796 from pylons import tmpl_context as c
1797 uri_tmpl = c.clone_uri_tmpl
1798 except Exception:
1799 # in any case if we call this outside of request context,
1800 # ie, not having tmpl_context set up
1801 pass
1802
1803 return get_clone_url(uri_tmpl=uri_tmpl,
1804 qualifed_home_url=qualified_home_url,
1805 repo_name=self.repo_name,
1806 repo_id=self.repo_id, **override)
1807
1808 def set_state(self, state):
1809 self.repo_state = state
1810 Session().add(self)
1811 #==========================================================================
1812 # SCM PROPERTIES
1813 #==========================================================================
1814
1815 def get_commit(self, commit_id=None, commit_idx=None, pre_load=None):
1816 return get_commit_safe(
1817 self.scm_instance(), commit_id, commit_idx, pre_load=pre_load)
1818
1819 def get_changeset(self, rev=None, pre_load=None):
1820 warnings.warn("Use get_commit", DeprecationWarning)
1821 commit_id = None
1822 commit_idx = None
1823 if isinstance(rev, basestring):
1824 commit_id = rev
1825 else:
1826 commit_idx = rev
1827 return self.get_commit(commit_id=commit_id, commit_idx=commit_idx,
1828 pre_load=pre_load)
1829
1830 def get_landing_commit(self):
1831 """
1832 Returns landing commit, or if that doesn't exist returns the tip
1833 """
1834 _rev_type, _rev = self.landing_rev
1835 commit = self.get_commit(_rev)
1836 if isinstance(commit, EmptyCommit):
1837 return self.get_commit()
1838 return commit
1839
1840 def update_commit_cache(self, cs_cache=None, config=None):
1841 """
1842 Update cache of last changeset for repository, keys should be::
1843
1844 short_id
1845 raw_id
1846 revision
1847 parents
1848 message
1849 date
1850 author
1851
1852 :param cs_cache:
1853 """
1854 from rhodecode.lib.vcs.backends.base import BaseChangeset
1855 if cs_cache is None:
1856 # use no-cache version here
1857 scm_repo = self.scm_instance(cache=False, config=config)
1858 if scm_repo:
1859 cs_cache = scm_repo.get_commit(
1860 pre_load=["author", "date", "message", "parents"])
1861 else:
1862 cs_cache = EmptyCommit()
1863
1864 if isinstance(cs_cache, BaseChangeset):
1865 cs_cache = cs_cache.__json__()
1866
1867 def is_outdated(new_cs_cache):
1868 if (new_cs_cache['raw_id'] != self.changeset_cache['raw_id'] or
1869 new_cs_cache['revision'] != self.changeset_cache['revision']):
1870 return True
1871 return False
1872
1873 # check if we have maybe already latest cached revision
1874 if is_outdated(cs_cache) or not self.changeset_cache:
1875 _default = datetime.datetime.fromtimestamp(0)
1876 last_change = cs_cache.get('date') or _default
1877 log.debug('updated repo %s with new cs cache %s',
1878 self.repo_name, cs_cache)
1879 self.updated_on = last_change
1880 self.changeset_cache = cs_cache
1881 Session().add(self)
1882 Session().commit()
1883 else:
1884 log.debug('Skipping update_commit_cache for repo:`%s` '
1885 'commit already with latest changes', self.repo_name)
1886
1887 @property
1888 def tip(self):
1889 return self.get_commit('tip')
1890
1891 @property
1892 def author(self):
1893 return self.tip.author
1894
1895 @property
1896 def last_change(self):
1897 return self.scm_instance().last_change
1898
1899 def get_comments(self, revisions=None):
1900 """
1901 Returns comments for this repository grouped by revisions
1902
1903 :param revisions: filter query by revisions only
1904 """
1905 cmts = ChangesetComment.query()\
1906 .filter(ChangesetComment.repo == self)
1907 if revisions:
1908 cmts = cmts.filter(ChangesetComment.revision.in_(revisions))
1909 grouped = collections.defaultdict(list)
1910 for cmt in cmts.all():
1911 grouped[cmt.revision].append(cmt)
1912 return grouped
1913
1914 def statuses(self, revisions=None):
1915 """
1916 Returns statuses for this repository
1917
1918 :param revisions: list of revisions to get statuses for
1919 """
1920 statuses = ChangesetStatus.query()\
1921 .filter(ChangesetStatus.repo == self)\
1922 .filter(ChangesetStatus.version == 0)
1923
1924 if revisions:
1925 # Try doing the filtering in chunks to avoid hitting limits
1926 size = 500
1927 status_results = []
1928 for chunk in xrange(0, len(revisions), size):
1929 status_results += statuses.filter(
1930 ChangesetStatus.revision.in_(
1931 revisions[chunk: chunk+size])
1932 ).all()
1933 else:
1934 status_results = statuses.all()
1935
1936 grouped = {}
1937
1938 # maybe we have open new pullrequest without a status?
1939 stat = ChangesetStatus.STATUS_UNDER_REVIEW
1940 status_lbl = ChangesetStatus.get_status_lbl(stat)
1941 for pr in PullRequest.query().filter(PullRequest.source_repo == self).all():
1942 for rev in pr.revisions:
1943 pr_id = pr.pull_request_id
1944 pr_repo = pr.target_repo.repo_name
1945 grouped[rev] = [stat, status_lbl, pr_id, pr_repo]
1946
1947 for stat in status_results:
1948 pr_id = pr_repo = None
1949 if stat.pull_request:
1950 pr_id = stat.pull_request.pull_request_id
1951 pr_repo = stat.pull_request.target_repo.repo_name
1952 grouped[stat.revision] = [str(stat.status), stat.status_lbl,
1953 pr_id, pr_repo]
1954 return grouped
1955
1956 # ==========================================================================
1957 # SCM CACHE INSTANCE
1958 # ==========================================================================
1959
1960 def scm_instance(self, **kwargs):
1961 import rhodecode
1962
1963 # Passing a config will not hit the cache currently only used
1964 # for repo2dbmapper
1965 config = kwargs.pop('config', None)
1966 cache = kwargs.pop('cache', None)
1967 full_cache = str2bool(rhodecode.CONFIG.get('vcs_full_cache'))
1968 # if cache is NOT defined use default global, else we have a full
1969 # control over cache behaviour
1970 if cache is None and full_cache and not config:
1971 return self._get_instance_cached()
1972 return self._get_instance(cache=bool(cache), config=config)
1973
1974 def _get_instance_cached(self):
1975 @cache_region('long_term')
1976 def _get_repo(cache_key):
1977 return self._get_instance()
1978
1979 invalidator_context = CacheKey.repo_context_cache(
1980 _get_repo, self.repo_name, None, thread_scoped=True)
1981
1982 with invalidator_context as context:
1983 context.invalidate()
1984 repo = context.compute()
1985
1986 return repo
1987
1988 def _get_instance(self, cache=True, config=None):
1989 config = config or self._config
1990 custom_wire = {
1991 'cache': cache # controls the vcs.remote cache
1992 }
1993
1994 repo = get_vcs_instance(
1995 repo_path=safe_str(self.repo_full_path),
1996 config=config,
1997 with_wire=custom_wire,
1998 create=False)
1999
2000 return repo
2001
2002 def __json__(self):
2003 return {'landing_rev': self.landing_rev}
2004
2005 def get_dict(self):
2006
2007 # Since we transformed `repo_name` to a hybrid property, we need to
2008 # keep compatibility with the code which uses `repo_name` field.
2009
2010 result = super(Repository, self).get_dict()
2011 result['repo_name'] = result.pop('_repo_name', None)
2012 return result
2013
2014
2015 class RepoGroup(Base, BaseModel):
2016 __tablename__ = 'groups'
2017 __table_args__ = (
2018 UniqueConstraint('group_name', 'group_parent_id'),
2019 CheckConstraint('group_id != group_parent_id'),
2020 {'extend_existing': True, 'mysql_engine': 'InnoDB',
2021 'mysql_charset': 'utf8', 'sqlite_autoincrement': True},
2022 )
2023 __mapper_args__ = {'order_by': 'group_name'}
2024
2025 CHOICES_SEPARATOR = '/' # used to generate select2 choices for nested groups
2026
2027 group_id = Column("group_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
2028 group_name = Column("group_name", String(255), nullable=False, unique=True, default=None)
2029 group_parent_id = Column("group_parent_id", Integer(), ForeignKey('groups.group_id'), nullable=True, unique=None, default=None)
2030 group_description = Column("group_description", String(10000), nullable=True, unique=None, default=None)
2031 enable_locking = Column("enable_locking", Boolean(), nullable=False, unique=None, default=False)
2032 user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=False, default=None)
2033 created_on = Column('created_on', DateTime(timezone=False), nullable=False, default=datetime.datetime.now)
2034
2035 repo_group_to_perm = relationship('UserRepoGroupToPerm', cascade='all', order_by='UserRepoGroupToPerm.group_to_perm_id')
2036 users_group_to_perm = relationship('UserGroupRepoGroupToPerm', cascade='all')
2037 parent_group = relationship('RepoGroup', remote_side=group_id)
2038 user = relationship('User')
2039
2040 def __init__(self, group_name='', parent_group=None):
2041 self.group_name = group_name
2042 self.parent_group = parent_group
2043
2044 def __unicode__(self):
2045 return u"<%s('id:%s:%s')>" % (self.__class__.__name__, self.group_id,
2046 self.group_name)
2047
2048 @classmethod
2049 def _generate_choice(cls, repo_group):
2050 from webhelpers.html import literal as _literal
2051 _name = lambda k: _literal(cls.CHOICES_SEPARATOR.join(k))
2052 return repo_group.group_id, _name(repo_group.full_path_splitted)
2053
2054 @classmethod
2055 def groups_choices(cls, groups=None, show_empty_group=True):
2056 if not groups:
2057 groups = cls.query().all()
2058
2059 repo_groups = []
2060 if show_empty_group:
2061 repo_groups = [('-1', u'-- %s --' % _('No parent'))]
2062
2063 repo_groups.extend([cls._generate_choice(x) for x in groups])
2064
2065 repo_groups = sorted(
2066 repo_groups, key=lambda t: t[1].split(cls.CHOICES_SEPARATOR)[0])
2067 return repo_groups
2068
2069 @classmethod
2070 def url_sep(cls):
2071 return URL_SEP
2072
2073 @classmethod
2074 def get_by_group_name(cls, group_name, cache=False, case_insensitive=False):
2075 if case_insensitive:
2076 gr = cls.query().filter(func.lower(cls.group_name)
2077 == func.lower(group_name))
2078 else:
2079 gr = cls.query().filter(cls.group_name == group_name)
2080 if cache:
2081 gr = gr.options(FromCache(
2082 "sql_cache_short",
2083 "get_group_%s" % _hash_key(group_name)))
2084 return gr.scalar()
2085
2086 @classmethod
2087 def get_all_repo_groups(cls, user_id=Optional(None), group_id=Optional(None),
2088 case_insensitive=True):
2089 q = RepoGroup.query()
2090
2091 if not isinstance(user_id, Optional):
2092 q = q.filter(RepoGroup.user_id == user_id)
2093
2094 if not isinstance(group_id, Optional):
2095 q = q.filter(RepoGroup.group_parent_id == group_id)
2096
2097 if case_insensitive:
2098 q = q.order_by(func.lower(RepoGroup.group_name))
2099 else:
2100 q = q.order_by(RepoGroup.group_name)
2101 return q.all()
2102
2103 @property
2104 def parents(self):
2105 parents_recursion_limit = 10
2106 groups = []
2107 if self.parent_group is None:
2108 return groups
2109 cur_gr = self.parent_group
2110 groups.insert(0, cur_gr)
2111 cnt = 0
2112 while 1:
2113 cnt += 1
2114 gr = getattr(cur_gr, 'parent_group', None)
2115 cur_gr = cur_gr.parent_group
2116 if gr is None:
2117 break
2118 if cnt == parents_recursion_limit:
2119 # this will prevent accidental infinit loops
2120 log.error(('more than %s parents found for group %s, stopping '
2121 'recursive parent fetching' % (parents_recursion_limit, self)))
2122 break
2123
2124 groups.insert(0, gr)
2125 return groups
2126
2127 @property
2128 def children(self):
2129 return RepoGroup.query().filter(RepoGroup.parent_group == self)
2130
2131 @property
2132 def name(self):
2133 return self.group_name.split(RepoGroup.url_sep())[-1]
2134
2135 @property
2136 def full_path(self):
2137 return self.group_name
2138
2139 @property
2140 def full_path_splitted(self):
2141 return self.group_name.split(RepoGroup.url_sep())
2142
2143 @property
2144 def repositories(self):
2145 return Repository.query()\
2146 .filter(Repository.group == self)\
2147 .order_by(Repository.repo_name)
2148
2149 @property
2150 def repositories_recursive_count(self):
2151 cnt = self.repositories.count()
2152
2153 def children_count(group):
2154 cnt = 0
2155 for child in group.children:
2156 cnt += child.repositories.count()
2157 cnt += children_count(child)
2158 return cnt
2159
2160 return cnt + children_count(self)
2161
2162 def _recursive_objects(self, include_repos=True):
2163 all_ = []
2164
2165 def _get_members(root_gr):
2166 if include_repos:
2167 for r in root_gr.repositories:
2168 all_.append(r)
2169 childs = root_gr.children.all()
2170 if childs:
2171 for gr in childs:
2172 all_.append(gr)
2173 _get_members(gr)
2174
2175 _get_members(self)
2176 return [self] + all_
2177
2178 def recursive_groups_and_repos(self):
2179 """
2180 Recursive return all groups, with repositories in those groups
2181 """
2182 return self._recursive_objects()
2183
2184 def recursive_groups(self):
2185 """
2186 Returns all children groups for this group including children of children
2187 """
2188 return self._recursive_objects(include_repos=False)
2189
2190 def get_new_name(self, group_name):
2191 """
2192 returns new full group name based on parent and new name
2193
2194 :param group_name:
2195 """
2196 path_prefix = (self.parent_group.full_path_splitted if
2197 self.parent_group else [])
2198 return RepoGroup.url_sep().join(path_prefix + [group_name])
2199
2200 def permissions(self, with_admins=True, with_owner=True):
2201 q = UserRepoGroupToPerm.query().filter(UserRepoGroupToPerm.group == self)
2202 q = q.options(joinedload(UserRepoGroupToPerm.group),
2203 joinedload(UserRepoGroupToPerm.user),
2204 joinedload(UserRepoGroupToPerm.permission),)
2205
2206 # get owners and admins and permissions. We do a trick of re-writing
2207 # objects from sqlalchemy to named-tuples due to sqlalchemy session
2208 # has a global reference and changing one object propagates to all
2209 # others. This means if admin is also an owner admin_row that change
2210 # would propagate to both objects
2211 perm_rows = []
2212 for _usr in q.all():
2213 usr = AttributeDict(_usr.user.get_dict())
2214 usr.permission = _usr.permission.permission_name
2215 perm_rows.append(usr)
2216
2217 # filter the perm rows by 'default' first and then sort them by
2218 # admin,write,read,none permissions sorted again alphabetically in
2219 # each group
2220 perm_rows = sorted(perm_rows, key=display_sort)
2221
2222 _admin_perm = 'group.admin'
2223 owner_row = []
2224 if with_owner:
2225 usr = AttributeDict(self.user.get_dict())
2226 usr.owner_row = True
2227 usr.permission = _admin_perm
2228 owner_row.append(usr)
2229
2230 super_admin_rows = []
2231 if with_admins:
2232 for usr in User.get_all_super_admins():
2233 # if this admin is also owner, don't double the record
2234 if usr.user_id == owner_row[0].user_id:
2235 owner_row[0].admin_row = True
2236 else:
2237 usr = AttributeDict(usr.get_dict())
2238 usr.admin_row = True
2239 usr.permission = _admin_perm
2240 super_admin_rows.append(usr)
2241
2242 return super_admin_rows + owner_row + perm_rows
2243
2244 def permission_user_groups(self):
2245 q = UserGroupRepoGroupToPerm.query().filter(UserGroupRepoGroupToPerm.group == self)
2246 q = q.options(joinedload(UserGroupRepoGroupToPerm.group),
2247 joinedload(UserGroupRepoGroupToPerm.users_group),
2248 joinedload(UserGroupRepoGroupToPerm.permission),)
2249
2250 perm_rows = []
2251 for _user_group in q.all():
2252 usr = AttributeDict(_user_group.users_group.get_dict())
2253 usr.permission = _user_group.permission.permission_name
2254 perm_rows.append(usr)
2255
2256 return perm_rows
2257
2258 def get_api_data(self):
2259 """
2260 Common function for generating api data
2261
2262 """
2263 group = self
2264 data = {
2265 'group_id': group.group_id,
2266 'group_name': group.group_name,
2267 'group_description': group.group_description,
2268 'parent_group': group.parent_group.group_name if group.parent_group else None,
2269 'repositories': [x.repo_name for x in group.repositories],
2270 'owner': group.user.username,
2271 }
2272 return data
2273
2274
2275 class Permission(Base, BaseModel):
2276 __tablename__ = 'permissions'
2277 __table_args__ = (
2278 Index('p_perm_name_idx', 'permission_name'),
2279 {'extend_existing': True, 'mysql_engine': 'InnoDB',
2280 'mysql_charset': 'utf8', 'sqlite_autoincrement': True},
2281 )
2282 PERMS = [
2283 ('hg.admin', _('RhodeCode Super Administrator')),
2284
2285 ('repository.none', _('Repository no access')),
2286 ('repository.read', _('Repository read access')),
2287 ('repository.write', _('Repository write access')),
2288 ('repository.admin', _('Repository admin access')),
2289
2290 ('group.none', _('Repository group no access')),
2291 ('group.read', _('Repository group read access')),
2292 ('group.write', _('Repository group write access')),
2293 ('group.admin', _('Repository group admin access')),
2294
2295 ('usergroup.none', _('User group no access')),
2296 ('usergroup.read', _('User group read access')),
2297 ('usergroup.write', _('User group write access')),
2298 ('usergroup.admin', _('User group admin access')),
2299
2300 ('hg.repogroup.create.false', _('Repository Group creation disabled')),
2301 ('hg.repogroup.create.true', _('Repository Group creation enabled')),
2302
2303 ('hg.usergroup.create.false', _('User Group creation disabled')),
2304 ('hg.usergroup.create.true', _('User Group creation enabled')),
2305
2306 ('hg.create.none', _('Repository creation disabled')),
2307 ('hg.create.repository', _('Repository creation enabled')),
2308 ('hg.create.write_on_repogroup.true', _('Repository creation enabled with write permission to a repository group')),
2309 ('hg.create.write_on_repogroup.false', _('Repository creation disabled with write permission to a repository group')),
2310
2311 ('hg.fork.none', _('Repository forking disabled')),
2312 ('hg.fork.repository', _('Repository forking enabled')),
2313
2314 ('hg.register.none', _('Registration disabled')),
2315 ('hg.register.manual_activate', _('User Registration with manual account activation')),
2316 ('hg.register.auto_activate', _('User Registration with automatic account activation')),
2317
2318 ('hg.extern_activate.manual', _('Manual activation of external account')),
2319 ('hg.extern_activate.auto', _('Automatic activation of external account')),
2320
2321 ('hg.inherit_default_perms.false', _('Inherit object permissions from default user disabled')),
2322 ('hg.inherit_default_perms.true', _('Inherit object permissions from default user enabled')),
2323 ]
2324
2325 # definition of system default permissions for DEFAULT user
2326 DEFAULT_USER_PERMISSIONS = [
2327 'repository.read',
2328 'group.read',
2329 'usergroup.read',
2330 'hg.create.repository',
2331 'hg.repogroup.create.false',
2332 'hg.usergroup.create.false',
2333 'hg.create.write_on_repogroup.true',
2334 'hg.fork.repository',
2335 'hg.register.manual_activate',
2336 'hg.extern_activate.auto',
2337 'hg.inherit_default_perms.true',
2338 ]
2339
2340 # defines which permissions are more important higher the more important
2341 # Weight defines which permissions are more important.
2342 # The higher number the more important.
2343 PERM_WEIGHTS = {
2344 'repository.none': 0,
2345 'repository.read': 1,
2346 'repository.write': 3,
2347 'repository.admin': 4,
2348
2349 'group.none': 0,
2350 'group.read': 1,
2351 'group.write': 3,
2352 'group.admin': 4,
2353
2354 'usergroup.none': 0,
2355 'usergroup.read': 1,
2356 'usergroup.write': 3,
2357 'usergroup.admin': 4,
2358
2359 'hg.repogroup.create.false': 0,
2360 'hg.repogroup.create.true': 1,
2361
2362 'hg.usergroup.create.false': 0,
2363 'hg.usergroup.create.true': 1,
2364
2365 'hg.fork.none': 0,
2366 'hg.fork.repository': 1,
2367 'hg.create.none': 0,
2368 'hg.create.repository': 1
2369 }
2370
2371 permission_id = Column("permission_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
2372 permission_name = Column("permission_name", String(255), nullable=True, unique=None, default=None)
2373 permission_longname = Column("permission_longname", String(255), nullable=True, unique=None, default=None)
2374
2375 def __unicode__(self):
2376 return u"<%s('%s:%s')>" % (
2377 self.__class__.__name__, self.permission_id, self.permission_name
2378 )
2379
2380 @classmethod
2381 def get_by_key(cls, key):
2382 return cls.query().filter(cls.permission_name == key).scalar()
2383
2384 @classmethod
2385 def get_default_repo_perms(cls, user_id, repo_id=None):
2386 q = Session().query(UserRepoToPerm, Repository, Permission)\
2387 .join((Permission, UserRepoToPerm.permission_id == Permission.permission_id))\
2388 .join((Repository, UserRepoToPerm.repository_id == Repository.repo_id))\
2389 .filter(UserRepoToPerm.user_id == user_id)
2390 if repo_id:
2391 q = q.filter(UserRepoToPerm.repository_id == repo_id)
2392 return q.all()
2393
2394 @classmethod
2395 def get_default_repo_perms_from_user_group(cls, user_id, repo_id=None):
2396 q = Session().query(UserGroupRepoToPerm, Repository, Permission)\
2397 .join(
2398 Permission,
2399 UserGroupRepoToPerm.permission_id == Permission.permission_id)\
2400 .join(
2401 Repository,
2402 UserGroupRepoToPerm.repository_id == Repository.repo_id)\
2403 .join(
2404 UserGroup,
2405 UserGroupRepoToPerm.users_group_id ==
2406 UserGroup.users_group_id)\
2407 .join(
2408 UserGroupMember,
2409 UserGroupRepoToPerm.users_group_id ==
2410 UserGroupMember.users_group_id)\
2411 .filter(
2412 UserGroupMember.user_id == user_id,
2413 UserGroup.users_group_active == true())
2414 if repo_id:
2415 q = q.filter(UserGroupRepoToPerm.repository_id == repo_id)
2416 return q.all()
2417
2418 @classmethod
2419 def get_default_group_perms(cls, user_id, repo_group_id=None):
2420 q = Session().query(UserRepoGroupToPerm, RepoGroup, Permission)\
2421 .join((Permission, UserRepoGroupToPerm.permission_id == Permission.permission_id))\
2422 .join((RepoGroup, UserRepoGroupToPerm.group_id == RepoGroup.group_id))\
2423 .filter(UserRepoGroupToPerm.user_id == user_id)
2424 if repo_group_id:
2425 q = q.filter(UserRepoGroupToPerm.group_id == repo_group_id)
2426 return q.all()
2427
2428 @classmethod
2429 def get_default_group_perms_from_user_group(
2430 cls, user_id, repo_group_id=None):
2431 q = Session().query(UserGroupRepoGroupToPerm, RepoGroup, Permission)\
2432 .join(
2433 Permission,
2434 UserGroupRepoGroupToPerm.permission_id ==
2435 Permission.permission_id)\
2436 .join(
2437 RepoGroup,
2438 UserGroupRepoGroupToPerm.group_id == RepoGroup.group_id)\
2439 .join(
2440 UserGroup,
2441 UserGroupRepoGroupToPerm.users_group_id ==
2442 UserGroup.users_group_id)\
2443 .join(
2444 UserGroupMember,
2445 UserGroupRepoGroupToPerm.users_group_id ==
2446 UserGroupMember.users_group_id)\
2447 .filter(
2448 UserGroupMember.user_id == user_id,
2449 UserGroup.users_group_active == true())
2450 if repo_group_id:
2451 q = q.filter(UserGroupRepoGroupToPerm.group_id == repo_group_id)
2452 return q.all()
2453
2454 @classmethod
2455 def get_default_user_group_perms(cls, user_id, user_group_id=None):
2456 q = Session().query(UserUserGroupToPerm, UserGroup, Permission)\
2457 .join((Permission, UserUserGroupToPerm.permission_id == Permission.permission_id))\
2458 .join((UserGroup, UserUserGroupToPerm.user_group_id == UserGroup.users_group_id))\
2459 .filter(UserUserGroupToPerm.user_id == user_id)
2460 if user_group_id:
2461 q = q.filter(UserUserGroupToPerm.user_group_id == user_group_id)
2462 return q.all()
2463
2464 @classmethod
2465 def get_default_user_group_perms_from_user_group(
2466 cls, user_id, user_group_id=None):
2467 TargetUserGroup = aliased(UserGroup, name='target_user_group')
2468 q = Session().query(UserGroupUserGroupToPerm, UserGroup, Permission)\
2469 .join(
2470 Permission,
2471 UserGroupUserGroupToPerm.permission_id ==
2472 Permission.permission_id)\
2473 .join(
2474 TargetUserGroup,
2475 UserGroupUserGroupToPerm.target_user_group_id ==
2476 TargetUserGroup.users_group_id)\
2477 .join(
2478 UserGroup,
2479 UserGroupUserGroupToPerm.user_group_id ==
2480 UserGroup.users_group_id)\
2481 .join(
2482 UserGroupMember,
2483 UserGroupUserGroupToPerm.user_group_id ==
2484 UserGroupMember.users_group_id)\
2485 .filter(
2486 UserGroupMember.user_id == user_id,
2487 UserGroup.users_group_active == true())
2488 if user_group_id:
2489 q = q.filter(
2490 UserGroupUserGroupToPerm.user_group_id == user_group_id)
2491
2492 return q.all()
2493
2494
2495 class UserRepoToPerm(Base, BaseModel):
2496 __tablename__ = 'repo_to_perm'
2497 __table_args__ = (
2498 UniqueConstraint('user_id', 'repository_id', 'permission_id'),
2499 {'extend_existing': True, 'mysql_engine': 'InnoDB',
2500 'mysql_charset': 'utf8', 'sqlite_autoincrement': True}
2501 )
2502 repo_to_perm_id = Column("repo_to_perm_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
2503 user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=None, default=None)
2504 permission_id = Column("permission_id", Integer(), ForeignKey('permissions.permission_id'), nullable=False, unique=None, default=None)
2505 repository_id = Column("repository_id", Integer(), ForeignKey('repositories.repo_id'), nullable=False, unique=None, default=None)
2506
2507 user = relationship('User')
2508 repository = relationship('Repository')
2509 permission = relationship('Permission')
2510
2511 @classmethod
2512 def create(cls, user, repository, permission):
2513 n = cls()
2514 n.user = user
2515 n.repository = repository
2516 n.permission = permission
2517 Session().add(n)
2518 return n
2519
2520 def __unicode__(self):
2521 return u'<%s => %s >' % (self.user, self.repository)
2522
2523
2524 class UserUserGroupToPerm(Base, BaseModel):
2525 __tablename__ = 'user_user_group_to_perm'
2526 __table_args__ = (
2527 UniqueConstraint('user_id', 'user_group_id', 'permission_id'),
2528 {'extend_existing': True, 'mysql_engine': 'InnoDB',
2529 'mysql_charset': 'utf8', 'sqlite_autoincrement': True}
2530 )
2531 user_user_group_to_perm_id = Column("user_user_group_to_perm_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
2532 user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=None, default=None)
2533 permission_id = Column("permission_id", Integer(), ForeignKey('permissions.permission_id'), nullable=False, unique=None, default=None)
2534 user_group_id = Column("user_group_id", Integer(), ForeignKey('users_groups.users_group_id'), nullable=False, unique=None, default=None)
2535
2536 user = relationship('User')
2537 user_group = relationship('UserGroup')
2538 permission = relationship('Permission')
2539
2540 @classmethod
2541 def create(cls, user, user_group, permission):
2542 n = cls()
2543 n.user = user
2544 n.user_group = user_group
2545 n.permission = permission
2546 Session().add(n)
2547 return n
2548
2549 def __unicode__(self):
2550 return u'<%s => %s >' % (self.user, self.user_group)
2551
2552
2553 class UserToPerm(Base, BaseModel):
2554 __tablename__ = 'user_to_perm'
2555 __table_args__ = (
2556 UniqueConstraint('user_id', 'permission_id'),
2557 {'extend_existing': True, 'mysql_engine': 'InnoDB',
2558 'mysql_charset': 'utf8', 'sqlite_autoincrement': True}
2559 )
2560 user_to_perm_id = Column("user_to_perm_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
2561 user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=None, default=None)
2562 permission_id = Column("permission_id", Integer(), ForeignKey('permissions.permission_id'), nullable=False, unique=None, default=None)
2563
2564 user = relationship('User')
2565 permission = relationship('Permission', lazy='joined')
2566
2567 def __unicode__(self):
2568 return u'<%s => %s >' % (self.user, self.permission)
2569
2570
2571 class UserGroupRepoToPerm(Base, BaseModel):
2572 __tablename__ = 'users_group_repo_to_perm'
2573 __table_args__ = (
2574 UniqueConstraint('repository_id', 'users_group_id', 'permission_id'),
2575 {'extend_existing': True, 'mysql_engine': 'InnoDB',
2576 'mysql_charset': 'utf8', 'sqlite_autoincrement': True}
2577 )
2578 users_group_to_perm_id = Column("users_group_to_perm_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
2579 users_group_id = Column("users_group_id", Integer(), ForeignKey('users_groups.users_group_id'), nullable=False, unique=None, default=None)
2580 permission_id = Column("permission_id", Integer(), ForeignKey('permissions.permission_id'), nullable=False, unique=None, default=None)
2581 repository_id = Column("repository_id", Integer(), ForeignKey('repositories.repo_id'), nullable=False, unique=None, default=None)
2582
2583 users_group = relationship('UserGroup')
2584 permission = relationship('Permission')
2585 repository = relationship('Repository')
2586
2587 @classmethod
2588 def create(cls, users_group, repository, permission):
2589 n = cls()
2590 n.users_group = users_group
2591 n.repository = repository
2592 n.permission = permission
2593 Session().add(n)
2594 return n
2595
2596 def __unicode__(self):
2597 return u'<UserGroupRepoToPerm:%s => %s >' % (self.users_group, self.repository)
2598
2599
2600 class UserGroupUserGroupToPerm(Base, BaseModel):
2601 __tablename__ = 'user_group_user_group_to_perm'
2602 __table_args__ = (
2603 UniqueConstraint('target_user_group_id', 'user_group_id', 'permission_id'),
2604 CheckConstraint('target_user_group_id != user_group_id'),
2605 {'extend_existing': True, 'mysql_engine': 'InnoDB',
2606 'mysql_charset': 'utf8', 'sqlite_autoincrement': True}
2607 )
2608 user_group_user_group_to_perm_id = Column("user_group_user_group_to_perm_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
2609 target_user_group_id = Column("target_user_group_id", Integer(), ForeignKey('users_groups.users_group_id'), nullable=False, unique=None, default=None)
2610 permission_id = Column("permission_id", Integer(), ForeignKey('permissions.permission_id'), nullable=False, unique=None, default=None)
2611 user_group_id = Column("user_group_id", Integer(), ForeignKey('users_groups.users_group_id'), nullable=False, unique=None, default=None)
2612
2613 target_user_group = relationship('UserGroup', primaryjoin='UserGroupUserGroupToPerm.target_user_group_id==UserGroup.users_group_id')
2614 user_group = relationship('UserGroup', primaryjoin='UserGroupUserGroupToPerm.user_group_id==UserGroup.users_group_id')
2615 permission = relationship('Permission')
2616
2617 @classmethod
2618 def create(cls, target_user_group, user_group, permission):
2619 n = cls()
2620 n.target_user_group = target_user_group
2621 n.user_group = user_group
2622 n.permission = permission
2623 Session().add(n)
2624 return n
2625
2626 def __unicode__(self):
2627 return u'<UserGroupUserGroup:%s => %s >' % (self.target_user_group, self.user_group)
2628
2629
2630 class UserGroupToPerm(Base, BaseModel):
2631 __tablename__ = 'users_group_to_perm'
2632 __table_args__ = (
2633 UniqueConstraint('users_group_id', 'permission_id',),
2634 {'extend_existing': True, 'mysql_engine': 'InnoDB',
2635 'mysql_charset': 'utf8', 'sqlite_autoincrement': True}
2636 )
2637 users_group_to_perm_id = Column("users_group_to_perm_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
2638 users_group_id = Column("users_group_id", Integer(), ForeignKey('users_groups.users_group_id'), nullable=False, unique=None, default=None)
2639 permission_id = Column("permission_id", Integer(), ForeignKey('permissions.permission_id'), nullable=False, unique=None, default=None)
2640
2641 users_group = relationship('UserGroup')
2642 permission = relationship('Permission')
2643
2644
2645 class UserRepoGroupToPerm(Base, BaseModel):
2646 __tablename__ = 'user_repo_group_to_perm'
2647 __table_args__ = (
2648 UniqueConstraint('user_id', 'group_id', 'permission_id'),
2649 {'extend_existing': True, 'mysql_engine': 'InnoDB',
2650 'mysql_charset': 'utf8', 'sqlite_autoincrement': True}
2651 )
2652
2653 group_to_perm_id = Column("group_to_perm_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
2654 user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=None, default=None)
2655 group_id = Column("group_id", Integer(), ForeignKey('groups.group_id'), nullable=False, unique=None, default=None)
2656 permission_id = Column("permission_id", Integer(), ForeignKey('permissions.permission_id'), nullable=False, unique=None, default=None)
2657
2658 user = relationship('User')
2659 group = relationship('RepoGroup')
2660 permission = relationship('Permission')
2661
2662 @classmethod
2663 def create(cls, user, repository_group, permission):
2664 n = cls()
2665 n.user = user
2666 n.group = repository_group
2667 n.permission = permission
2668 Session().add(n)
2669 return n
2670
2671
2672 class UserGroupRepoGroupToPerm(Base, BaseModel):
2673 __tablename__ = 'users_group_repo_group_to_perm'
2674 __table_args__ = (
2675 UniqueConstraint('users_group_id', 'group_id'),
2676 {'extend_existing': True, 'mysql_engine': 'InnoDB',
2677 'mysql_charset': 'utf8', 'sqlite_autoincrement': True}
2678 )
2679
2680 users_group_repo_group_to_perm_id = Column("users_group_repo_group_to_perm_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
2681 users_group_id = Column("users_group_id", Integer(), ForeignKey('users_groups.users_group_id'), nullable=False, unique=None, default=None)
2682 group_id = Column("group_id", Integer(), ForeignKey('groups.group_id'), nullable=False, unique=None, default=None)
2683 permission_id = Column("permission_id", Integer(), ForeignKey('permissions.permission_id'), nullable=False, unique=None, default=None)
2684
2685 users_group = relationship('UserGroup')
2686 permission = relationship('Permission')
2687 group = relationship('RepoGroup')
2688
2689 @classmethod
2690 def create(cls, user_group, repository_group, permission):
2691 n = cls()
2692 n.users_group = user_group
2693 n.group = repository_group
2694 n.permission = permission
2695 Session().add(n)
2696 return n
2697
2698 def __unicode__(self):
2699 return u'<UserGroupRepoGroupToPerm:%s => %s >' % (self.users_group, self.group)
2700
2701
2702 class Statistics(Base, BaseModel):
2703 __tablename__ = 'statistics'
2704 __table_args__ = (
2705 UniqueConstraint('repository_id'),
2706 {'extend_existing': True, 'mysql_engine': 'InnoDB',
2707 'mysql_charset': 'utf8', 'sqlite_autoincrement': True}
2708 )
2709 stat_id = Column("stat_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
2710 repository_id = Column("repository_id", Integer(), ForeignKey('repositories.repo_id'), nullable=False, unique=True, default=None)
2711 stat_on_revision = Column("stat_on_revision", Integer(), nullable=False)
2712 commit_activity = Column("commit_activity", LargeBinary(1000000), nullable=False)#JSON data
2713 commit_activity_combined = Column("commit_activity_combined", LargeBinary(), nullable=False)#JSON data
2714 languages = Column("languages", LargeBinary(1000000), nullable=False)#JSON data
2715
2716 repository = relationship('Repository', single_parent=True)
2717
2718
2719 class UserFollowing(Base, BaseModel):
2720 __tablename__ = 'user_followings'
2721 __table_args__ = (
2722 UniqueConstraint('user_id', 'follows_repository_id'),
2723 UniqueConstraint('user_id', 'follows_user_id'),
2724 {'extend_existing': True, 'mysql_engine': 'InnoDB',
2725 'mysql_charset': 'utf8', 'sqlite_autoincrement': True}
2726 )
2727
2728 user_following_id = Column("user_following_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
2729 user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=None, default=None)
2730 follows_repo_id = Column("follows_repository_id", Integer(), ForeignKey('repositories.repo_id'), nullable=True, unique=None, default=None)
2731 follows_user_id = Column("follows_user_id", Integer(), ForeignKey('users.user_id'), nullable=True, unique=None, default=None)
2732 follows_from = Column('follows_from', DateTime(timezone=False), nullable=True, unique=None, default=datetime.datetime.now)
2733
2734 user = relationship('User', primaryjoin='User.user_id==UserFollowing.user_id')
2735
2736 follows_user = relationship('User', primaryjoin='User.user_id==UserFollowing.follows_user_id')
2737 follows_repository = relationship('Repository', order_by='Repository.repo_name')
2738
2739 @classmethod
2740 def get_repo_followers(cls, repo_id):
2741 return cls.query().filter(cls.follows_repo_id == repo_id)
2742
2743
2744 class CacheKey(Base, BaseModel):
2745 __tablename__ = 'cache_invalidation'
2746 __table_args__ = (
2747 UniqueConstraint('cache_key'),
2748 Index('key_idx', 'cache_key'),
2749 {'extend_existing': True, 'mysql_engine': 'InnoDB',
2750 'mysql_charset': 'utf8', 'sqlite_autoincrement': True},
2751 )
2752 CACHE_TYPE_ATOM = 'ATOM'
2753 CACHE_TYPE_RSS = 'RSS'
2754 CACHE_TYPE_README = 'README'
2755
2756 cache_id = Column("cache_id", Integer(), nullable=False, unique=True, default=None, primary_key=True)
2757 cache_key = Column("cache_key", String(255), nullable=True, unique=None, default=None)
2758 cache_args = Column("cache_args", String(255), nullable=True, unique=None, default=None)
2759 cache_active = Column("cache_active", Boolean(), nullable=True, unique=None, default=False)
2760
2761 def __init__(self, cache_key, cache_args=''):
2762 self.cache_key = cache_key
2763 self.cache_args = cache_args
2764 self.cache_active = False
2765
2766 def __unicode__(self):
2767 return u"<%s('%s:%s[%s]')>" % (
2768 self.__class__.__name__,
2769 self.cache_id, self.cache_key, self.cache_active)
2770
2771 def _cache_key_partition(self):
2772 prefix, repo_name, suffix = self.cache_key.partition(self.cache_args)
2773 return prefix, repo_name, suffix
2774
2775 def get_prefix(self):
2776 """
2777 Try to extract prefix from existing cache key. The key could consist
2778 of prefix, repo_name, suffix
2779 """
2780 # this returns prefix, repo_name, suffix
2781 return self._cache_key_partition()[0]
2782
2783 def get_suffix(self):
2784 """
2785 get suffix that might have been used in _get_cache_key to
2786 generate self.cache_key. Only used for informational purposes
2787 in repo_edit.html.
2788 """
2789 # prefix, repo_name, suffix
2790 return self._cache_key_partition()[2]
2791
2792 @classmethod
2793 def delete_all_cache(cls):
2794 """
2795 Delete all cache keys from database.
2796 Should only be run when all instances are down and all entries
2797 thus stale.
2798 """
2799 cls.query().delete()
2800 Session().commit()
2801
2802 @classmethod
2803 def get_cache_key(cls, repo_name, cache_type):
2804 """
2805
2806 Generate a cache key for this process of RhodeCode instance.
2807 Prefix most likely will be process id or maybe explicitly set
2808 instance_id from .ini file.
2809 """
2810 import rhodecode
2811 prefix = safe_unicode(rhodecode.CONFIG.get('instance_id') or '')
2812
2813 repo_as_unicode = safe_unicode(repo_name)
2814 key = u'{}_{}'.format(repo_as_unicode, cache_type) \
2815 if cache_type else repo_as_unicode
2816
2817 return u'{}{}'.format(prefix, key)
2818
2819 @classmethod
2820 def set_invalidate(cls, repo_name, delete=False):
2821 """
2822 Mark all caches of a repo as invalid in the database.
2823 """
2824
2825 try:
2826 qry = Session().query(cls).filter(cls.cache_args == repo_name)
2827 if delete:
2828 log.debug('cache objects deleted for repo %s',
2829 safe_str(repo_name))
2830 qry.delete()
2831 else:
2832 log.debug('cache objects marked as invalid for repo %s',
2833 safe_str(repo_name))
2834 qry.update({"cache_active": False})
2835
2836 Session().commit()
2837 except Exception:
2838 log.exception(
2839 'Cache key invalidation failed for repository %s',
2840 safe_str(repo_name))
2841 Session().rollback()
2842
2843 @classmethod
2844 def get_active_cache(cls, cache_key):
2845 inv_obj = cls.query().filter(cls.cache_key == cache_key).scalar()
2846 if inv_obj:
2847 return inv_obj
2848 return None
2849
2850 @classmethod
2851 def repo_context_cache(cls, compute_func, repo_name, cache_type,
2852 thread_scoped=False):
2853 """
2854 @cache_region('long_term')
2855 def _heavy_calculation(cache_key):
2856 return 'result'
2857
2858 cache_context = CacheKey.repo_context_cache(
2859 _heavy_calculation, repo_name, cache_type)
2860
2861 with cache_context as context:
2862 context.invalidate()
2863 computed = context.compute()
2864
2865 assert computed == 'result'
2866 """
2867 from rhodecode.lib import caches
2868 return caches.InvalidationContext(
2869 compute_func, repo_name, cache_type, thread_scoped=thread_scoped)
2870
2871
2872 class ChangesetComment(Base, BaseModel):
2873 __tablename__ = 'changeset_comments'
2874 __table_args__ = (
2875 Index('cc_revision_idx', 'revision'),
2876 {'extend_existing': True, 'mysql_engine': 'InnoDB',
2877 'mysql_charset': 'utf8', 'sqlite_autoincrement': True},
2878 )
2879
2880 COMMENT_OUTDATED = u'comment_outdated'
2881
2882 comment_id = Column('comment_id', Integer(), nullable=False, primary_key=True)
2883 repo_id = Column('repo_id', Integer(), ForeignKey('repositories.repo_id'), nullable=False)
2884 revision = Column('revision', String(40), nullable=True)
2885 pull_request_id = Column("pull_request_id", Integer(), ForeignKey('pull_requests.pull_request_id'), nullable=True)
2886 pull_request_version_id = Column("pull_request_version_id", Integer(), ForeignKey('pull_request_versions.pull_request_version_id'), nullable=True)
2887 line_no = Column('line_no', Unicode(10), nullable=True)
2888 hl_lines = Column('hl_lines', Unicode(512), nullable=True)
2889 f_path = Column('f_path', Unicode(1000), nullable=True)
2890 user_id = Column('user_id', Integer(), ForeignKey('users.user_id'), nullable=False)
2891 text = Column('text', UnicodeText().with_variant(UnicodeText(25000), 'mysql'), nullable=False)
2892 created_on = Column('created_on', DateTime(timezone=False), nullable=False, default=datetime.datetime.now)
2893 modified_at = Column('modified_at', DateTime(timezone=False), nullable=False, default=datetime.datetime.now)
2894 renderer = Column('renderer', Unicode(64), nullable=True)
2895 display_state = Column('display_state', Unicode(128), nullable=True)
2896
2897 author = relationship('User', lazy='joined')
2898 repo = relationship('Repository')
2899 status_change = relationship('ChangesetStatus', cascade="all, delete, delete-orphan")
2900 pull_request = relationship('PullRequest', lazy='joined')
2901 pull_request_version = relationship('PullRequestVersion')
2902
2903 @classmethod
2904 def get_users(cls, revision=None, pull_request_id=None):
2905 """
2906 Returns user associated with this ChangesetComment. ie those
2907 who actually commented
2908
2909 :param cls:
2910 :param revision:
2911 """
2912 q = Session().query(User)\
2913 .join(ChangesetComment.author)
2914 if revision:
2915 q = q.filter(cls.revision == revision)
2916 elif pull_request_id:
2917 q = q.filter(cls.pull_request_id == pull_request_id)
2918 return q.all()
2919
2920 def render(self, mentions=False):
2921 from rhodecode.lib import helpers as h
2922 return h.render(self.text, renderer=self.renderer, mentions=mentions)
2923
2924 def __repr__(self):
2925 if self.comment_id:
2926 return '<DB:ChangesetComment #%s>' % self.comment_id
2927 else:
2928 return '<DB:ChangesetComment at %#x>' % id(self)
2929
2930
2931 class ChangesetStatus(Base, BaseModel):
2932 __tablename__ = 'changeset_statuses'
2933 __table_args__ = (
2934 Index('cs_revision_idx', 'revision'),
2935 Index('cs_version_idx', 'version'),
2936 UniqueConstraint('repo_id', 'revision', 'version'),
2937 {'extend_existing': True, 'mysql_engine': 'InnoDB',
2938 'mysql_charset': 'utf8', 'sqlite_autoincrement': True}
2939 )
2940 STATUS_NOT_REVIEWED = DEFAULT = 'not_reviewed'
2941 STATUS_APPROVED = 'approved'
2942 STATUS_REJECTED = 'rejected'
2943 STATUS_UNDER_REVIEW = 'under_review'
2944
2945 STATUSES = [
2946 (STATUS_NOT_REVIEWED, _("Not Reviewed")), # (no icon) and default
2947 (STATUS_APPROVED, _("Approved")),
2948 (STATUS_REJECTED, _("Rejected")),
2949 (STATUS_UNDER_REVIEW, _("Under Review")),
2950 ]
2951
2952 changeset_status_id = Column('changeset_status_id', Integer(), nullable=False, primary_key=True)
2953 repo_id = Column('repo_id', Integer(), ForeignKey('repositories.repo_id'), nullable=False)
2954 user_id = Column("user_id", Integer(), ForeignKey('users.user_id'), nullable=False, unique=None)
2955 revision = Column('revision', String(40), nullable=False)
2956 status = Column('status', String(128), nullable=False, default=DEFAULT)
2957 changeset_comment_id = Column('changeset_comment_id', Integer(), ForeignKey('changeset_comments.comment_id'))
2958 modified_at = Column('modified_at', DateTime(), nullable=False, default=datetime.datetime.now)
2959 version = Column('version', Integer(), nullable=False, default=0)
2960 pull_request_id = Column("pull_request_id", Integer(), ForeignKey('pull_requests.pull_request_id'), nullable=True)
2961
2962 author = relationship('User', lazy='joined')
2963 repo = relationship('Repository')
2964 comment = relationship('ChangesetComment', lazy='joined')
2965 pull_request = relationship('PullRequest', lazy='joined')
2966
2967 def __unicode__(self):
2968 return u"<%s('%s[%s]:%s')>" % (
2969 self.__class__.__name__,
2970 self.status, self.version, self.author
2971 )
2972
2973 @classmethod
2974 def get_status_lbl(cls, value):
2975 return dict(cls.STATUSES).get(value)
2976
2977 @property
2978 def status_lbl(self):
2979 return ChangesetStatus.get_status_lbl(self.status)
2980
2981
2982 class _PullRequestBase(BaseModel):
2983 """
2984 Common attributes of pull request and version entries.
2985 """
2986
2987 # .status values
2988 STATUS_NEW = u'new'
2989 STATUS_OPEN = u'open'
2990 STATUS_CLOSED = u'closed'
2991
2992 title = Column('title', Unicode(255), nullable=True)
2993 description = Column(
2994 'description', UnicodeText().with_variant(UnicodeText(10240), 'mysql'),
2995 nullable=True)
2996 # new/open/closed status of pull request (not approve/reject/etc)
2997 status = Column('status', Unicode(255), nullable=False, default=STATUS_NEW)
2998 created_on = Column(
2999 'created_on', DateTime(timezone=False), nullable=False,
3000 default=datetime.datetime.now)
3001 updated_on = Column(
3002 'updated_on', DateTime(timezone=False), nullable=False,
3003 default=datetime.datetime.now)
3004
3005 @declared_attr
3006 def user_id(cls):
3007 return Column(
3008 "user_id", Integer(), ForeignKey('users.user_id'), nullable=False,
3009 unique=None)
3010
3011 # 500 revisions max
3012 _revisions = Column(
3013 'revisions', UnicodeText().with_variant(UnicodeText(20500), 'mysql'))
3014
3015 @declared_attr
3016 def source_repo_id(cls):
3017 # TODO: dan: rename column to source_repo_id
3018 return Column(
3019 'org_repo_id', Integer(), ForeignKey('repositories.repo_id'),
3020 nullable=False)
3021
3022 source_ref = Column('org_ref', Unicode(255), nullable=False)
3023
3024 @declared_attr
3025 def target_repo_id(cls):
3026 # TODO: dan: rename column to target_repo_id
3027 return Column(
3028 'other_repo_id', Integer(), ForeignKey('repositories.repo_id'),
3029 nullable=False)
3030
3031 target_ref = Column('other_ref', Unicode(255), nullable=False)
3032
3033 # TODO: dan: rename column to last_merge_source_rev
3034 _last_merge_source_rev = Column(
3035 'last_merge_org_rev', String(40), nullable=True)
3036 # TODO: dan: rename column to last_merge_target_rev
3037 _last_merge_target_rev = Column(
3038 'last_merge_other_rev', String(40), nullable=True)
3039 _last_merge_status = Column('merge_status', Integer(), nullable=True)
3040 merge_rev = Column('merge_rev', String(40), nullable=True)
3041
3042 @hybrid_property
3043 def revisions(self):
3044 return self._revisions.split(':') if self._revisions else []
3045
3046 @revisions.setter
3047 def revisions(self, val):
3048 self._revisions = ':'.join(val)
3049
3050 @declared_attr
3051 def author(cls):
3052 return relationship('User', lazy='joined')
3053
3054 @declared_attr
3055 def source_repo(cls):
3056 return relationship(
3057 'Repository',
3058 primaryjoin='%s.source_repo_id==Repository.repo_id' % cls.__name__)
3059
3060 @property
3061 def source_ref_parts(self):
3062 refs = self.source_ref.split(':')
3063 return Reference(refs[0], refs[1], refs[2])
3064
3065 @declared_attr
3066 def target_repo(cls):
3067 return relationship(
3068 'Repository',
3069 primaryjoin='%s.target_repo_id==Repository.repo_id' % cls.__name__)
3070
3071 @property
3072 def target_ref_parts(self):
3073 refs = self.target_ref.split(':')
3074 return Reference(refs[0], refs[1], refs[2])
3075
3076
3077 class PullRequest(Base, _PullRequestBase):
3078 __tablename__ = 'pull_requests'
3079 __table_args__ = (
3080 {'extend_existing': True, 'mysql_engine': 'InnoDB',
3081 'mysql_charset': 'utf8', 'sqlite_autoincrement': True},
3082 )
3083
3084 pull_request_id = Column(
3085 'pull_request_id', Integer(), nullable=False, primary_key=True)
3086
3087 def __repr__(self):
3088 if self.pull_request_id:
3089 return '<DB:PullRequest #%s>' % self.pull_request_id
3090 else:
3091 return '<DB:PullRequest at %#x>' % id(self)
3092
3093 reviewers = relationship('PullRequestReviewers',
3094 cascade="all, delete, delete-orphan")
3095 statuses = relationship('ChangesetStatus')
3096 comments = relationship('ChangesetComment',
3097 cascade="all, delete, delete-orphan")
3098 versions = relationship('PullRequestVersion',
3099 cascade="all, delete, delete-orphan")
3100
3101 def is_closed(self):
3102 return self.status == self.STATUS_CLOSED
3103
3104 def get_api_data(self):
3105 from rhodecode.model.pull_request import PullRequestModel
3106 pull_request = self
3107 merge_status = PullRequestModel().merge_status(pull_request)
3108 data = {
3109 'pull_request_id': pull_request.pull_request_id,
3110 'url': url('pullrequest_show', repo_name=self.target_repo.repo_name,
3111 pull_request_id=self.pull_request_id,
3112 qualified=True),
3113 'title': pull_request.title,
3114 'description': pull_request.description,
3115 'status': pull_request.status,
3116 'created_on': pull_request.created_on,
3117 'updated_on': pull_request.updated_on,
3118 'commit_ids': pull_request.revisions,
3119 'review_status': pull_request.calculated_review_status(),
3120 'mergeable': {
3121 'status': merge_status[0],
3122 'message': unicode(merge_status[1]),
3123 },
3124 'source': {
3125 'clone_url': pull_request.source_repo.clone_url(),
3126 'repository': pull_request.source_repo.repo_name,
3127 'reference': {
3128 'name': pull_request.source_ref_parts.name,
3129 'type': pull_request.source_ref_parts.type,
3130 'commit_id': pull_request.source_ref_parts.commit_id,
3131 },
3132 },
3133 'target': {
3134 'clone_url': pull_request.target_repo.clone_url(),
3135 'repository': pull_request.target_repo.repo_name,
3136 'reference': {
3137 'name': pull_request.target_ref_parts.name,
3138 'type': pull_request.target_ref_parts.type,
3139 'commit_id': pull_request.target_ref_parts.commit_id,
3140 },
3141 },
3142 'author': pull_request.author.get_api_data(include_secrets=False,
3143 details='basic'),
3144 'reviewers': [
3145 {
3146 'user': reviewer.get_api_data(include_secrets=False,
3147 details='basic'),
3148 'review_status': st[0][1].status if st else 'not_reviewed',
3149 }
3150 for reviewer, st in pull_request.reviewers_statuses()
3151 ]
3152 }
3153
3154 return data
3155
3156 def __json__(self):
3157 return {
3158 'revisions': self.revisions,
3159 }
3160
3161 def calculated_review_status(self):
3162 # TODO: anderson: 13.05.15 Used only on templates/my_account_pullrequests.html
3163 # because it's tricky on how to use ChangesetStatusModel from there
3164 warnings.warn("Use calculated_review_status from ChangesetStatusModel", DeprecationWarning)
3165 from rhodecode.model.changeset_status import ChangesetStatusModel
3166 return ChangesetStatusModel().calculated_review_status(self)
3167
3168 def reviewers_statuses(self):
3169 warnings.warn("Use reviewers_statuses from ChangesetStatusModel", DeprecationWarning)
3170 from rhodecode.model.changeset_status import ChangesetStatusModel
3171 return ChangesetStatusModel().reviewers_statuses(self)
3172
3173
3174 class PullRequestVersion(Base, _PullRequestBase):
3175 __tablename__ = 'pull_request_versions'
3176 __table_args__ = (
3177 {'extend_existing': True, 'mysql_engine': 'InnoDB',
3178 'mysql_charset': 'utf8', 'sqlite_autoincrement': True},
3179 )
3180
3181 pull_request_version_id = Column(
3182 'pull_request_version_id', Integer(), nullable=False, primary_key=True)
3183 pull_request_id = Column(
3184 'pull_request_id', Integer(),
3185 ForeignKey('pull_requests.pull_request_id'), nullable=False)
3186 pull_request = relationship('PullRequest')
3187
3188 def __repr__(self):
3189 if self.pull_request_version_id:
3190 return '<DB:PullRequestVersion #%s>' % self.pull_request_version_id
3191 else:
3192 return '<DB:PullRequestVersion at %#x>' % id(self)
3193
3194
3195 class PullRequestReviewers(Base, BaseModel):
3196 __tablename__ = 'pull_request_reviewers'
3197 __table_args__ = (
3198 {'extend_existing': True, 'mysql_engine': 'InnoDB',
3199 'mysql_charset': 'utf8', 'sqlite_autoincrement': True},
3200 )
3201
3202 def __init__(self, user=None, pull_request=None):
3203 self.user = user
3204 self.pull_request = pull_request
3205
3206 pull_requests_reviewers_id = Column(
3207 'pull_requests_reviewers_id', Integer(), nullable=False,
3208 primary_key=True)
3209 pull_request_id = Column(
3210 "pull_request_id", Integer(),
3211 ForeignKey('pull_requests.pull_request_id'), nullable=False)
3212 user_id = Column(
3213 "user_id", Integer(), ForeignKey('users.user_id'), nullable=True)
3214
3215 user = relationship('User')
3216 pull_request = relationship('PullRequest')
3217
3218
3219 class Notification(Base, BaseModel):
3220 __tablename__ = 'notifications'
3221 __table_args__ = (
3222 Index('notification_type_idx', 'type'),
3223 {'extend_existing': True, 'mysql_engine': 'InnoDB',
3224 'mysql_charset': 'utf8', 'sqlite_autoincrement': True},
3225 )
3226
3227 TYPE_CHANGESET_COMMENT = u'cs_comment'
3228 TYPE_MESSAGE = u'message'
3229 TYPE_MENTION = u'mention'
3230 TYPE_REGISTRATION = u'registration'
3231 TYPE_PULL_REQUEST = u'pull_request'
3232 TYPE_PULL_REQUEST_COMMENT = u'pull_request_comment'
3233
3234 notification_id = Column('notification_id', Integer(), nullable=False, primary_key=True)
3235 subject = Column('subject', Unicode(512), nullable=True)
3236 body = Column('body', UnicodeText().with_variant(UnicodeText(50000), 'mysql'), nullable=True)
3237 created_by = Column("created_by", Integer(), ForeignKey('users.user_id'), nullable=True)
3238 created_on = Column('created_on', DateTime(timezone=False), nullable=False, default=datetime.datetime.now)
3239 type_ = Column('type', Unicode(255))
3240
3241 created_by_user = relationship('User')
3242 notifications_to_users = relationship('UserNotification', lazy='joined',
3243 cascade="all, delete, delete-orphan")
3244
3245 @property
3246 def recipients(self):
3247 return [x.user for x in UserNotification.query()\
3248 .filter(UserNotification.notification == self)\
3249 .order_by(UserNotification.user_id.asc()).all()]
3250
3251 @classmethod
3252 def create(cls, created_by, subject, body, recipients, type_=None):
3253 if type_ is None:
3254 type_ = Notification.TYPE_MESSAGE
3255
3256 notification = cls()
3257 notification.created_by_user = created_by
3258 notification.subject = subject
3259 notification.body = body
3260 notification.type_ = type_
3261 notification.created_on = datetime.datetime.now()
3262
3263 for u in recipients:
3264 assoc = UserNotification()
3265 assoc.notification = notification
3266
3267 # if created_by is inside recipients mark his notification
3268 # as read
3269 if u.user_id == created_by.user_id:
3270 assoc.read = True
3271
3272 u.notifications.append(assoc)
3273 Session().add(notification)
3274
3275 return notification
3276
3277 @property
3278 def description(self):
3279 from rhodecode.model.notification import NotificationModel
3280 return NotificationModel().make_description(self)
3281
3282
3283 class UserNotification(Base, BaseModel):
3284 __tablename__ = 'user_to_notification'
3285 __table_args__ = (
3286 UniqueConstraint('user_id', 'notification_id'),
3287 {'extend_existing': True, 'mysql_engine': 'InnoDB',
3288 'mysql_charset': 'utf8', 'sqlite_autoincrement': True}
3289 )
3290 user_id = Column('user_id', Integer(), ForeignKey('users.user_id'), primary_key=True)
3291 notification_id = Column("notification_id", Integer(), ForeignKey('notifications.notification_id'), primary_key=True)
3292 read = Column('read', Boolean, default=False)
3293 sent_on = Column('sent_on', DateTime(timezone=False), nullable=True, unique=None)
3294
3295 user = relationship('User', lazy="joined")
3296 notification = relationship('Notification', lazy="joined",
3297 order_by=lambda: Notification.created_on.desc(),)
3298
3299 def mark_as_read(self):
3300 self.read = True
3301 Session().add(self)
3302
3303
3304 class Gist(Base, BaseModel):
3305 __tablename__ = 'gists'
3306 __table_args__ = (
3307 Index('g_gist_access_id_idx', 'gist_access_id'),
3308 Index('g_created_on_idx', 'created_on'),
3309 {'extend_existing': True, 'mysql_engine': 'InnoDB',
3310 'mysql_charset': 'utf8', 'sqlite_autoincrement': True}
3311 )
3312 GIST_PUBLIC = u'public'
3313 GIST_PRIVATE = u'private'
3314 DEFAULT_FILENAME = u'gistfile1.txt'
3315
3316 ACL_LEVEL_PUBLIC = u'acl_public'
3317 ACL_LEVEL_PRIVATE = u'acl_private'
3318
3319 gist_id = Column('gist_id', Integer(), primary_key=True)
3320 gist_access_id = Column('gist_access_id', Unicode(250))
3321 gist_description = Column('gist_description', UnicodeText().with_variant(UnicodeText(1024), 'mysql'))
3322 gist_owner = Column('user_id', Integer(), ForeignKey('users.user_id'), nullable=True)
3323 gist_expires = Column('gist_expires', Float(53), nullable=False)
3324 gist_type = Column('gist_type', Unicode(128), nullable=False)
3325 created_on = Column('created_on', DateTime(timezone=False), nullable=False, default=datetime.datetime.now)
3326 modified_at = Column('modified_at', DateTime(timezone=False), nullable=False, default=datetime.datetime.now)
3327 acl_level = Column('acl_level', Unicode(128), nullable=True)
3328
3329 owner = relationship('User')
3330
3331 def __repr__(self):
3332 return '<Gist:[%s]%s>' % (self.gist_type, self.gist_access_id)
3333
3334 @classmethod
3335 def get_or_404(cls, id_):
3336 res = cls.query().filter(cls.gist_access_id == id_).scalar()
3337 if not res:
3338 raise HTTPNotFound
3339 return res
3340
3341 @classmethod
3342 def get_by_access_id(cls, gist_access_id):
3343 return cls.query().filter(cls.gist_access_id == gist_access_id).scalar()
3344
3345 def gist_url(self):
3346 import rhodecode
3347 alias_url = rhodecode.CONFIG.get('gist_alias_url')
3348 if alias_url:
3349 return alias_url.replace('{gistid}', self.gist_access_id)
3350
3351 return url('gist', gist_id=self.gist_access_id, qualified=True)
3352
3353 @classmethod
3354 def base_path(cls):
3355 """
3356 Returns base path when all gists are stored
3357
3358 :param cls:
3359 """
3360 from rhodecode.model.gist import GIST_STORE_LOC
3361 q = Session().query(RhodeCodeUi)\
3362 .filter(RhodeCodeUi.ui_key == URL_SEP)
3363 q = q.options(FromCache("sql_cache_short", "repository_repo_path"))
3364 return os.path.join(q.one().ui_value, GIST_STORE_LOC)
3365
3366 def get_api_data(self):
3367 """
3368 Common function for generating gist related data for API
3369 """
3370 gist = self
3371 data = {
3372 'gist_id': gist.gist_id,
3373 'type': gist.gist_type,
3374 'access_id': gist.gist_access_id,
3375 'description': gist.gist_description,
3376 'url': gist.gist_url(),
3377 'expires': gist.gist_expires,
3378 'created_on': gist.created_on,
3379 'modified_at': gist.modified_at,
3380 'content': None,
3381 'acl_level': gist.acl_level,
3382 }
3383 return data
3384
3385 def __json__(self):
3386 data = dict(
3387 )
3388 data.update(self.get_api_data())
3389 return data
3390 # SCM functions
3391
3392 def scm_instance(self, **kwargs):
3393 full_repo_path = os.path.join(self.base_path(), self.gist_access_id)
3394 return get_vcs_instance(
3395 repo_path=safe_str(full_repo_path), create=False)
3396
3397
3398 class DbMigrateVersion(Base, BaseModel):
3399 __tablename__ = 'db_migrate_version'
3400 __table_args__ = (
3401 {'extend_existing': True, 'mysql_engine': 'InnoDB',
3402 'mysql_charset': 'utf8', 'sqlite_autoincrement': True},
3403 )
3404 repository_id = Column('repository_id', String(250), primary_key=True)
3405 repository_path = Column('repository_path', Text)
3406 version = Column('version', Integer)
3407
3408
3409 class ExternalIdentity(Base, BaseModel):
3410 __tablename__ = 'external_identities'
3411 __table_args__ = (
3412 Index('local_user_id_idx', 'local_user_id'),
3413 Index('external_id_idx', 'external_id'),
3414 {'extend_existing': True, 'mysql_engine': 'InnoDB',
3415 'mysql_charset': 'utf8'})
3416
3417 external_id = Column('external_id', Unicode(255), default=u'',
3418 primary_key=True)
3419 external_username = Column('external_username', Unicode(1024), default=u'')
3420 local_user_id = Column('local_user_id', Integer(),
3421 ForeignKey('users.user_id'), primary_key=True)
3422 provider_name = Column('provider_name', Unicode(255), default=u'',
3423 primary_key=True)
3424 access_token = Column('access_token', String(1024), default=u'')
3425 alt_token = Column('alt_token', String(1024), default=u'')
3426 token_secret = Column('token_secret', String(1024), default=u'')
3427
3428 @classmethod
3429 def by_external_id_and_provider(cls, external_id, provider_name,
3430 local_user_id=None):
3431 """
3432 Returns ExternalIdentity instance based on search params
3433
3434 :param external_id:
3435 :param provider_name:
3436 :return: ExternalIdentity
3437 """
3438 query = cls.query()
3439 query = query.filter(cls.external_id == external_id)
3440 query = query.filter(cls.provider_name == provider_name)
3441 if local_user_id:
3442 query = query.filter(cls.local_user_id == local_user_id)
3443 return query.first()
3444
3445 @classmethod
3446 def user_by_external_id_and_provider(cls, external_id, provider_name):
3447 """
3448 Returns User instance based on search params
3449
3450 :param external_id:
3451 :param provider_name:
3452 :return: User
3453 """
3454 query = User.query()
3455 query = query.filter(cls.external_id == external_id)
3456 query = query.filter(cls.provider_name == provider_name)
3457 query = query.filter(User.user_id == cls.local_user_id)
3458 return query.first()
3459
3460 @classmethod
3461 def by_local_user_id(cls, local_user_id):
3462 """
3463 Returns all tokens for user
3464
3465 :param local_user_id:
3466 :return: ExternalIdentity
3467 """
3468 query = cls.query()
3469 query = query.filter(cls.local_user_id == local_user_id)
3470 return query
3471
3472
3473 class Integration(Base, BaseModel):
3474 __tablename__ = 'integrations'
3475 __table_args__ = (
3476 {'extend_existing': True, 'mysql_engine': 'InnoDB',
3477 'mysql_charset': 'utf8', 'sqlite_autoincrement': True}
3478 )
3479
3480 integration_id = Column('integration_id', Integer(), primary_key=True)
3481 integration_type = Column('integration_type', String(255))
3482 enabled = Column('enabled', Boolean(), nullable=False)
3483 name = Column('name', String(255), nullable=False)
3484
3485 settings = Column(
3486 'settings_json', MutationObj.as_mutable(
3487 JsonType(dialect_map=dict(mysql=UnicodeText(16384)))))
3488 repo_id = Column(
3489 'repo_id', Integer(), ForeignKey('repositories.repo_id'),
3490 nullable=True, unique=None, default=None)
3491 repo = relationship('Repository', lazy='joined')
3492
3493 repo_group_id = Column(
3494 'repo_group_id', Integer(), ForeignKey('groups.group_id'),
3495 nullable=True, unique=None, default=None)
3496 repo_group = relationship('RepoGroup', lazy='joined')
3497
3498 def __repr__(self):
3499 if self.repo:
3500 scope = 'repo=%r' % self.repo
3501 elif self.repo_group:
3502 scope = 'repo_group=%r' % self.repo_group
3503 else:
3504 scope = 'global'
3505
3506 return '<Integration(%r, %r)>' % (self.integration_type, scope)
@@ -0,0 +1,36 b''
1 import logging
2 import datetime
3
4 from sqlalchemy import *
5 from sqlalchemy.exc import DatabaseError
6 from sqlalchemy.orm import relation, backref, class_mapper, joinedload
7 from sqlalchemy.orm.session import Session
8 from sqlalchemy.ext.declarative import declarative_base
9
10 from rhodecode.lib.dbmigrate.migrate import *
11 from rhodecode.lib.dbmigrate.migrate.changeset import *
12 from rhodecode.lib.utils2 import str2bool
13
14 from rhodecode.model.meta import Base
15 from rhodecode.model import meta
16 from rhodecode.lib.dbmigrate.versions import _reset_base, notify
17
18 log = logging.getLogger(__name__)
19
20
21 def upgrade(migrate_engine):
22 """
23 Upgrade operations go here.
24 Don't create your own engine; bind migrate_engine to your metadata
25 """
26 _reset_base(migrate_engine)
27 from rhodecode.lib.dbmigrate.schema import db_4_4_0_0
28
29 tbl = db_4_4_0_0.Integration.__table__
30 repo_group_id = db_4_4_0_0.Integration.repo_group_id
31 repo_group_id.create(table=tbl)
32
33
34 def downgrade(migrate_engine):
35 meta = MetaData()
36 meta.bind = migrate_engine
@@ -51,7 +51,7 b' PYRAMID_SETTINGS = {}'
51 51 EXTENSIONS = {}
52 52
53 53 __version__ = ('.'.join((str(each) for each in VERSION[:3])))
54 __dbversion__ = 55 # defines current db version for migrations
54 __dbversion__ = 56 # defines current db version for migrations
55 55 __platform__ = platform.system()
56 56 __license__ = 'AGPLv3, and Commercial License'
57 57 __author__ = 'RhodeCode GmbH'
General Comments 0
You need to be logged in to leave comments. Login now