def fork_create(self, repo_name): self.__load_defaults() c.repo_info = Repository.get_by_repo_name(repo_name) _form = RepoForkForm(old_data={'repo_type': c.repo_info.repo_type}, repo_groups=c.repo_groups_choices, landing_revs=c.landing_revs_choices)() form_result = {} try: form_result = _form.to_python(dict(request.POST)) # an approximation that is better than nothing if not RhodeCodeUi.get_by_key(RhodeCodeUi.HOOK_UPDATE).ui_active: form_result['update_after_clone'] = False # create fork is done sometimes async on celery, db transaction # management is handled there. RepoModel().create_fork(form_result, self.rhodecode_user.user_id) fork_url = h.link_to(form_result['repo_name_full'], h.url('summary_home', repo_name=form_result['repo_name_full'])) h.flash(h.literal(_('Forked repository %s as %s') \ % (repo_name, fork_url)), category='success') except formencode.Invalid, errors: c.new_repo = errors.value['repo_name'] return htmlfill.render( render('forks/fork.html'), defaults=errors.value, errors=errors.error_dict or {}, prefix_error=False, encoding="UTF-8")
def create(self): """POST /users: Create a new item""" # url('users') c.default_extern_type = auth_rhodecode.RhodeCodeAuthPlugin.name user_model = UserModel() user_form = UserForm()() try: form_result = user_form.to_python(dict(request.POST)) user = user_model.create(form_result) Session().flush() username = form_result['username'] action_logger(c.rhodecode_user, 'admin_created_user:%s' % username, None, self.ip_addr, self.sa) user_link = h.link_to(h.escape(username), url('edit_user', user_id=user.user_id)) h.flash(h.literal( _('Created user %(user_link)s') % {'user_link': user_link}), category='success') Session().commit() except formencode.Invalid as errors: return htmlfill.render(render('admin/users/user_add.html'), defaults=errors.value, errors=errors.error_dict or {}, prefix_error=False, encoding="UTF-8", force_defaults=False) except UserCreationError as e: h.flash(e, 'error') except Exception: log.exception("Exception creation of user") h.flash(_('Error occurred during creation of user %s') % request.POST.get('username'), category='error') return redirect(url('users'))
def _ignorews_url(GET, fileid=None): fileid = str(fileid) if fileid else None params = defaultdict(list) _update_with_GET(params, GET) label = _('Show whitespace') tooltiplbl = _('Show whitespace for all diffs') ig_ws = get_ignore_ws(fileid, GET) ln_ctx = get_line_ctx(fileid, GET) if ig_ws is None: params['ignorews'] += [1] label = _('Ignore whitespace') tooltiplbl = _('Ignore whitespace for all diffs') ctx_key = 'context' ctx_val = ln_ctx # if we have passed in ln_ctx pass it along to our params if ln_ctx: params[ctx_key] += [ctx_val] if fileid: params['anchor'] = 'a_' + fileid return h.link_to(label, h.url.current(**params), title=tooltiplbl, class_='tooltip')
def _ignorews_url(GET, fileid=None): fileid = str(fileid) if fileid else None params = defaultdict(list) _update_with_GET(params, GET) lbl = _('show white space') ig_ws = get_ignore_ws(fileid, GET) ln_ctx = get_line_ctx(fileid, GET) # global option if fileid is None: if ig_ws is None: params['ignorews'] += [1] lbl = _('ignore white space') ctx_key = 'context' ctx_val = ln_ctx # per file options else: if ig_ws is None: params[fileid] += ['WS:1'] lbl = _('ignore white space') ctx_key = fileid ctx_val = 'C:%s' % ln_ctx # if we have passed in ln_ctx pass it along to our params if ln_ctx: params[ctx_key] += [ctx_val] params['anchor'] = fileid img = h.image(h.url('/images/icons/text_strikethrough.png'), lbl, class_='icon') return h.link_to(img, h.url.current(**params), title=lbl, class_='tooltip')
def _context_url(GET, fileid=None): """ Generates a url for context lines. :param fileid: """ fileid = str(fileid) if fileid else None ig_ws = get_ignore_ws(fileid, GET) ln_ctx = (get_line_ctx(fileid, GET) or 3) * 2 params = defaultdict(list) _update_with_GET(params, GET) if ln_ctx > 0: params['context'] += [ln_ctx] if ig_ws: ig_ws_key = 'ignorews' ig_ws_val = 1 params[ig_ws_key] += [ig_ws_val] lbl = _('Increase context') tooltiplbl = _('Increase context for all diffs') if fileid: params['anchor'] = 'a_' + fileid return h.link_to(lbl, h.url.current(**params), title=tooltiplbl, class_='tooltip')
def fork_create(self, repo_name): self.__load_defaults() c.repo_info = Repository.get_by_repo_name(repo_name) _form = RepoForkForm(old_data={'repo_type': c.repo_info.repo_type}, repo_groups=c.repo_groups_choices, landing_revs=c.landing_revs_choices)() form_result = {} try: form_result = _form.to_python(dict(request.POST)) # an approximation that is better than nothing if not RhodeCodeUi.get_by_key(RhodeCodeUi.HOOK_UPDATE).ui_active: form_result['update_after_clone'] = False # create fork is done sometimes async on celery, db transaction # management is handled there. RepoModel().create_fork(form_result, self.rhodecode_user.user_id) fork_url = h.link_to( form_result['repo_name_full'], h.url('summary_home', repo_name=form_result['repo_name_full'])) h.flash(h.literal(_('Forked repository %s as %s') \ % (repo_name, fork_url)), category='success') except formencode.Invalid, errors: c.new_repo = errors.value['repo_name'] return htmlfill.render(render('forks/fork.html'), defaults=errors.value, errors=errors.error_dict or {}, prefix_error=False, encoding="UTF-8")
def create(self, created_by, org_repo, org_ref, other_repo, other_ref, revisions, reviewers, title, description=None): created_by_user = self._get_user(created_by) org_repo = self._get_repo(org_repo) other_repo = self._get_repo(other_repo) new = PullRequest() new.org_repo = org_repo new.org_ref = org_ref new.other_repo = other_repo new.other_ref = other_ref new.revisions = revisions new.title = title new.description = description new.author = created_by_user self.sa.add(new) Session().flush() #members for member in reviewers: _usr = self._get_user(member) reviewer = PullRequestReviewers(_usr, new) self.sa.add(reviewer) #notification to reviewers notif = NotificationModel() pr_url = h.url('pullrequest_show', repo_name=other_repo.repo_name, pull_request_id=new.pull_request_id, qualified=True, ) subject = safe_unicode( h.link_to( _('%(user)s wants you to review pull request #%(pr_id)s') % \ {'user': created_by_user.username, 'pr_id': new.pull_request_id}, pr_url ) ) body = description kwargs = { 'pr_title': title, 'pr_user_created': h.person(created_by_user.email), 'pr_repo_url': h.url('summary_home', repo_name=other_repo.repo_name, qualified=True,), 'pr_url': pr_url, 'pr_revisions': revisions } notif.create(created_by=created_by_user, subject=subject, body=body, recipients=reviewers, type_=Notification.TYPE_PULL_REQUEST, email_kwargs=kwargs) return new
def create(self): """POST /repo_groups: Create a new item""" # url('repo_groups') parent_group_id = safe_int(request.POST.get('group_parent_id')) can_create = self._can_create_repo_group(parent_group_id) self.__load_defaults() # permissions for can create group based on parent_id are checked # here in the Form available_groups = map(lambda k: unicode(k[0]), c.repo_groups) repo_group_form = RepoGroupForm(available_groups=available_groups, can_create_in_root=can_create)() try: owner = c.rhodecode_user form_result = repo_group_form.to_python(dict(request.POST)) RepoGroupModel().create( group_name=form_result['group_name_full'], group_description=form_result['group_description'], owner=owner.user_id, copy_permissions=form_result['group_copy_permissions']) Session().commit() _new_group_name = form_result['group_name_full'] repo_group_url = h.link_to( _new_group_name, h.url('repo_group_home', group_name=_new_group_name)) h.flash(h.literal( _('Created repository group %s') % repo_group_url), category='success') # TODO: in futureaction_logger(, '', '', '', self.sa) except formencode.Invalid as errors: return htmlfill.render( render('admin/repo_groups/repo_group_add.html'), defaults=errors.value, errors=errors.error_dict or {}, prefix_error=False, encoding="UTF-8", force_defaults=False) except Exception: log.exception("Exception during creation of repository group") h.flash( _('Error occurred during creation of repository group %s') % request.POST.get('group_name'), category='error') # TODO: maybe we should get back to the main view, not the admin one return redirect(url('repo_groups', parent_group=parent_group_id))
def __get_cs_or_redirect(self, rev, repo_name, redirect_after=True): """ Safe way to get changeset if error occur it redirects to tip with proper message :param rev: revision to fetch :param repo_name: repo name to redirect after """ try: return c.rhodecode_repo.get_changeset(rev) except EmptyRepositoryError, e: if not redirect_after: return None url_ = url("files_add_home", repo_name=c.repo_name, revision=0, f_path="") add_new = h.link_to(_("Click here to add new file"), url_) h.flash(h.literal(_("There are no files yet %s") % add_new), category="warning") redirect(h.url("summary_home", repo_name=repo_name))
def create(self): """ POST /repos: Create a new item""" # url('repos') self.__load_defaults() form_result = {} try: form_result = RepoForm(repo_groups=c.repo_groups_choices, landing_revs=c.landing_revs_choices)()\ .to_python(dict(request.POST)) new_repo = RepoModel().create(form_result, self.rhodecode_user.user_id) if form_result['clone_uri']: h.flash(_('Created repository %s from %s') \ % (form_result['repo_name'], form_result['clone_uri']), category='success') else: repo_url = h.link_to( form_result['repo_name'], h.url('summary_home', repo_name=form_result['repo_name_full'])) h.flash(h.literal(_('Created repository %s') % repo_url), category='success') if request.POST.get('user_created'): # created by regular non admin user action_logger(self.rhodecode_user, 'user_created_repo', form_result['repo_name_full'], self.ip_addr, self.sa) else: action_logger(self.rhodecode_user, 'admin_created_repo', form_result['repo_name_full'], self.ip_addr, self.sa) Session().commit() except formencode.Invalid, errors: return htmlfill.render(render('admin/repos/repo_add.html'), defaults=errors.value, errors=errors.error_dict or {}, prefix_error=False, encoding="UTF-8")
def create(self): """POST /users_groups: Create a new item""" # url('users_groups') users_group_form = UserGroupForm()() try: form_result = users_group_form.to_python(dict(request.POST)) user_group = UserGroupModel().create( name=form_result['users_group_name'], description=form_result['user_group_description'], owner=c.rhodecode_user.user_id, active=form_result['users_group_active']) Session().flush() user_group_name = form_result['users_group_name'] action_logger(c.rhodecode_user, 'admin_created_users_group:%s' % user_group_name, None, self.ip_addr, self.sa) user_group_link = h.link_to( h.escape(user_group_name), url('edit_users_group', user_group_id=user_group.users_group_id)) h.flash(h.literal( _('Created user group %(user_group_link)s') % {'user_group_link': user_group_link}), category='success') Session().commit() except formencode.Invalid as errors: return htmlfill.render( render('admin/user_groups/user_group_add.html'), defaults=errors.value, errors=errors.error_dict or {}, prefix_error=False, encoding="UTF-8", force_defaults=False) except Exception: log.exception("Exception creating user group") h.flash(_('Error occurred during creation of user group %s') \ % request.POST.get('users_group_name'), category='error') return redirect( url('edit_users_group', user_group_id=user_group.users_group_id))
def create(self): """ POST /repos: Create a new item""" # url('repos') self.__load_defaults() form_result = {} try: form_result = RepoForm(repo_groups=c.repo_groups_choices, landing_revs=c.landing_revs_choices)()\ .to_python(dict(request.POST)) new_repo = RepoModel().create(form_result, self.rhodecode_user.user_id) if form_result['clone_uri']: h.flash(_('Created repository %s from %s') \ % (form_result['repo_name'], form_result['clone_uri']), category='success') else: repo_url = h.link_to(form_result['repo_name'], h.url('summary_home', repo_name=form_result['repo_name_full'])) h.flash(h.literal(_('Created repository %s') % repo_url), category='success') if request.POST.get('user_created'): # created by regular non admin user action_logger(self.rhodecode_user, 'user_created_repo', form_result['repo_name_full'], self.ip_addr, self.sa) else: action_logger(self.rhodecode_user, 'admin_created_repo', form_result['repo_name_full'], self.ip_addr, self.sa) Session().commit() except formencode.Invalid, errors: return htmlfill.render( render('admin/repos/repo_add.html'), defaults=errors.value, errors=errors.error_dict or {}, prefix_error=False, encoding="UTF-8")
def __get_cs_or_redirect(self, rev, repo_name, redirect_after=True): """ Safe way to get changeset if error occur it redirects to tip with proper message :param rev: revision to fetch :param repo_name: repo name to redirect after """ try: return c.rhodecode_repo.get_changeset(rev) except EmptyRepositoryError, e: if not redirect_after: return None url_ = url('files_add_home', repo_name=c.repo_name, revision=0, f_path='') add_new = h.link_to(_('Click here to add new file'), url_) h.flash(h.literal(_('There are no files yet %s') % add_new), category='warning') redirect(h.url('summary_home', repo_name=repo_name))
def __get_commit_or_redirect(self, commit_id, repo_name, redirect_after=True): """ This is a safe way to get commit. If an error occurs it redirects to tip with proper message :param commit_id: id of commit to fetch :param repo_name: repo name to redirect after :param redirect_after: toggle redirection """ try: return c.rhodecode_repo.get_commit(commit_id) except EmptyRepositoryError: if not redirect_after: return None url_ = url('files_add_home', repo_name=c.repo_name, revision=0, f_path='', anchor='edit') if h.HasRepoPermissionAny('repository.write', 'repository.admin')(c.repo_name): add_new = h.link_to(_('Click here to add a new file.'), url_, class_="alert-link") else: add_new = "" h.flash(h.literal(_('There are no files yet. %s') % add_new), category='warning') redirect(h.url('summary_home', repo_name=repo_name)) except (CommitDoesNotExistError, LookupError): msg = _('No such commit exists for this repository') h.flash(msg, category='error') raise HTTPNotFound() except RepositoryError as e: h.flash(safe_str(e), category='error') raise HTTPNotFound()
def repo_check(self, repo_name): c.repo = repo_name task_id = request.GET.get('task_id') if task_id and task_id not in ['None']: import rhodecode from celery.result import AsyncResult if rhodecode.CELERY_ENABLED: task = AsyncResult(task_id) if task.failed(): msg = self._log_creation_exception(task.result, c.repo) h.flash(msg, category='error') return redirect(url('home'), code=501) repo = Repository.get_by_repo_name(repo_name) if repo and repo.repo_state == Repository.STATE_CREATED: if repo.clone_uri: clone_uri = repo.clone_uri_hidden h.flash(_('Created repository %s from %s') % (repo.repo_name, clone_uri), category='success') else: repo_url = h.link_to( repo.repo_name, h.url('summary_home', repo_name=repo.repo_name)) fork = repo.fork if fork: fork_name = fork.repo_name h.flash(h.literal( _('Forked repository %s as %s') % (fork_name, repo_url)), category='success') else: h.flash(h.literal(_('Created repository %s') % repo_url), category='success') return {'result': True} return {'result': False}
def _context_url(GET, fileid=None): """ Generates url for context lines :param fileid: """ fileid = str(fileid) if fileid else None ig_ws = get_ignore_ws(fileid, GET) ln_ctx = (get_line_ctx(fileid, GET) or 3) * 2 params = defaultdict(list) _update_with_GET(params, GET) # global option if fileid is None: if ln_ctx > 0: params['context'] += [ln_ctx] if ig_ws: ig_ws_key = 'ignorews' ig_ws_val = 1 # per file option else: params[fileid] += ['C:%s' % ln_ctx] ig_ws_key = fileid ig_ws_val = 'WS:%s' % 1 if ig_ws: params[ig_ws_key] += [ig_ws_val] lbl = _('%s line context') % ln_ctx params['anchor'] = fileid img = h.image(h.url('/images/icons/table_add.png'), lbl, class_='icon') return h.link_to(img, h.url.current(**params), title=lbl, class_='tooltip')
def create(self, text, repo, user, revision=None, pull_request=None, f_path=None, line_no=None, status_change=None): """ Creates new comment for changeset or pull request. IF status_change is not none this comment is associated with a status change of changeset or changesets associated with pull request :param text: :param repo: :param user: :param revision: :param pull_request: :param f_path: :param line_no: :param status_change: """ if not text: return repo = self._get_repo(repo) user = self._get_user(user) comment = ChangesetComment() comment.repo = repo comment.author = user comment.text = text comment.f_path = f_path comment.line_no = line_no if revision: cs = repo.scm_instance.get_changeset(revision) desc = "%s - %s" % (cs.short_id, h.shorter(cs.message, 256)) author_email = cs.author_email comment.revision = revision elif pull_request: pull_request = self.__get_pull_request(pull_request) comment.pull_request = pull_request desc = pull_request.pull_request_id else: raise Exception('Please specify revision or pull_request_id') self.sa.add(comment) self.sa.flush() # make notification line = '' body = text #changeset if revision: if line_no: line = _('on line %s') % line_no subj = safe_unicode( h.link_to('Re commit: %(desc)s %(line)s' % \ {'desc': desc, 'line': line}, h.url('changeset_home', repo_name=repo.repo_name, revision=revision, anchor='comment-%s' % comment.comment_id, qualified=True, ) ) ) notification_type = Notification.TYPE_CHANGESET_COMMENT # get the current participants of this changeset recipients = ChangesetComment.get_users(revision=revision) # add changeset author if it's in rhodecode system recipients += [User.get_by_email(author_email)] email_kwargs = { 'status_change': status_change, } #pull request elif pull_request: _url = h.url('pullrequest_show', repo_name=pull_request.other_repo.repo_name, pull_request_id=pull_request.pull_request_id, anchor='comment-%s' % comment.comment_id, qualified=True, ) subj = safe_unicode( h.link_to('Re pull request: %(desc)s %(line)s' % \ {'desc': desc, 'line': line}, _url) ) notification_type = Notification.TYPE_PULL_REQUEST_COMMENT # get the current participants of this pull request recipients = ChangesetComment.get_users(pull_request_id= pull_request.pull_request_id) # add pull request author recipients += [pull_request.author] # add the reviewers to notification recipients += [x.user for x in pull_request.reviewers] #set some variables for email notification email_kwargs = { 'pr_id': pull_request.pull_request_id, 'status_change': status_change, 'pr_comment_url': _url, 'pr_comment_user': h.person(user.email), 'pr_target_repo': h.url('summary_home', repo_name=pull_request.other_repo.repo_name, qualified=True) } # create notification objects, and emails NotificationModel().create( created_by=user, subject=subj, body=body, recipients=recipients, type_=notification_type, email_kwargs=email_kwargs ) mention_recipients = set(self._extract_mentions(body))\ .difference(recipients) if mention_recipients: email_kwargs.update({'pr_mention': True}) subj = _('[Mention]') + ' ' + subj NotificationModel().create( created_by=user, subject=subj, body=body, recipients=mention_recipients, type_=notification_type, email_kwargs=email_kwargs ) return comment
def create(self, created_by, org_repo, org_ref, other_repo, other_ref, revisions, reviewers, title, description=None): from rhodecode.model.changeset_status import ChangesetStatusModel created_by_user = self._get_user(created_by) org_repo = self._get_repo(org_repo) other_repo = self._get_repo(other_repo) new = PullRequest() new.org_repo = org_repo new.org_ref = org_ref new.other_repo = other_repo new.other_ref = other_ref new.revisions = revisions new.title = title new.description = description new.author = created_by_user Session().add(new) Session().flush() #members for member in set(reviewers): _usr = self._get_user(member) reviewer = PullRequestReviewers(_usr, new) Session().add(reviewer) #reset state to under-review ChangesetStatusModel().set_status( repo=org_repo, status=ChangesetStatus.STATUS_UNDER_REVIEW, user=created_by_user, pull_request=new ) revision_data = [(x.raw_id, x.message) for x in map(org_repo.get_changeset, revisions)] #notification to reviewers pr_url = h.url('pullrequest_show', repo_name=other_repo.repo_name, pull_request_id=new.pull_request_id, qualified=True, ) subject = safe_unicode( h.link_to( _('%(user)s wants you to review pull request #%(pr_id)s: %(pr_title)s') % \ {'user': created_by_user.username, 'pr_title': new.title, 'pr_id': new.pull_request_id}, pr_url ) ) body = description kwargs = { 'pr_title': title, 'pr_user_created': h.person(created_by_user.email), 'pr_repo_url': h.url('summary_home', repo_name=other_repo.repo_name, qualified=True,), 'pr_url': pr_url, 'pr_revisions': revision_data } NotificationModel().create(created_by=created_by_user, subject=subject, body=body, recipients=reviewers, type_=Notification.TYPE_PULL_REQUEST, email_kwargs=kwargs) return new
def create(self, text, repo, user, revision=None, pull_request=None, f_path=None, line_no=None, status_change=None, closing_pr=False, send_email=True, renderer=None): """ Creates new comment for commit or pull request. IF status_change is not none this comment is associated with a status change of commit or commit associated with pull request :param text: :param repo: :param user: :param revision: :param pull_request: :param f_path: :param line_no: :param status_change: :param closing_pr: :param send_email: """ if not text: log.warning('Missing text for comment, skipping...') return if not renderer: renderer = self._get_renderer() repo = self._get_repo(repo) user = self._get_user(user) comment = ChangesetComment() comment.renderer = renderer comment.repo = repo comment.author = user comment.text = text comment.f_path = f_path comment.line_no = line_no #TODO (marcink): fix this and remove revision as param commit_id = revision pull_request_id = pull_request commit_obj = None pull_request_obj = None if commit_id: notification_type = EmailNotificationModel.TYPE_COMMIT_COMMENT # do a lookup, so we don't pass something bad here commit_obj = repo.scm_instance().get_commit(commit_id=commit_id) comment.revision = commit_obj.raw_id elif pull_request_id: notification_type = EmailNotificationModel.TYPE_PULL_REQUEST_COMMENT pull_request_obj = self.__get_pull_request(pull_request_id) comment.pull_request = pull_request_obj else: raise Exception('Please specify commit or pull_request_id') Session().add(comment) Session().flush() if send_email: kwargs = { 'user': user, 'renderer_type': renderer, 'repo_name': repo.repo_name, 'status_change': status_change, 'comment_body': text, 'comment_file': f_path, 'comment_line': line_no, } if commit_obj: recipients = ChangesetComment.get_users( revision=commit_obj.raw_id) # add commit author if it's in RhodeCode system cs_author = User.get_from_cs_author(commit_obj.author) if not cs_author: # use repo owner if we cannot extract the author correctly cs_author = repo.user recipients += [cs_author] commit_comment_url = h.url( 'changeset_home', repo_name=repo.repo_name, revision=commit_obj.raw_id, anchor='comment-%s' % comment.comment_id, qualified=True, ) target_repo_url = h.link_to( repo.repo_name, h.url('summary_home', repo_name=repo.repo_name, qualified=True)) # commit specifics kwargs.update({ 'commit': commit_obj, 'commit_message': commit_obj.message, 'commit_target_repo': target_repo_url, 'commit_comment_url': commit_comment_url, }) elif pull_request_obj: # get the current participants of this pull request recipients = ChangesetComment.get_users( pull_request_id=pull_request_obj.pull_request_id) # add pull request author recipients += [pull_request_obj.author] # add the reviewers to notification recipients += [x.user for x in pull_request_obj.reviewers] pr_target_repo = pull_request_obj.target_repo pr_source_repo = pull_request_obj.source_repo pr_comment_url = h.url( 'pullrequest_show', repo_name=pr_target_repo.repo_name, pull_request_id=pull_request_obj.pull_request_id, anchor='comment-%s' % comment.comment_id, qualified=True, ) # set some variables for email notification pr_target_repo_url = h.url('summary_home', repo_name=pr_target_repo.repo_name, qualified=True) pr_source_repo_url = h.url('summary_home', repo_name=pr_source_repo.repo_name, qualified=True) # pull request specifics kwargs.update({ 'pull_request': pull_request_obj, 'pr_id': pull_request_obj.pull_request_id, 'pr_target_repo': pr_target_repo, 'pr_target_repo_url': pr_target_repo_url, 'pr_source_repo': pr_source_repo, 'pr_source_repo_url': pr_source_repo_url, 'pr_comment_url': pr_comment_url, 'pr_closing': closing_pr, }) # pre-generate the subject for notification itself ( subject, _h, _e, # we don't care about those body_plaintext) = EmailNotificationModel().render_email( notification_type, **kwargs) mention_recipients = set( self._extract_mentions(text)).difference(recipients) # create notification objects, and emails NotificationModel().create( created_by=user, notification_subject=subject, notification_body=body_plaintext, notification_type=notification_type, recipients=recipients, mention_recipients=mention_recipients, email_kwargs=kwargs, ) action = ('user_commented_pull_request:{}'.format( comment.pull_request.pull_request_id) if comment.pull_request else 'user_commented_revision:{}'.format(comment.revision)) action_logger(user, action, comment.repo) return comment
def _get_notification_data(self, repo, comment, user, comment_text, line_no=None, revision=None, pull_request=None, status_change=None, closing_pr=False): """ Get notification data :param comment_text: :param line: :returns: tuple (subj,body,recipients,notification_type,email_kwargs) """ # make notification body = comment_text # text of the comment line = '' if line_no: line = _('on line %s') % line_no #changeset if revision: notification_type = Notification.TYPE_CHANGESET_COMMENT cs = repo.scm_instance.get_changeset(revision) desc = "%s" % (cs.short_id) _url = h.url( 'changeset_home', repo_name=repo.repo_name, revision=revision, anchor='comment-%s' % comment.comment_id, qualified=True, ) subj = safe_unicode( h.link_to('Re changeset: %(desc)s %(line)s' % \ {'desc': desc, 'line': line}, _url) ) email_subject = 'User %s commented on changeset %s' % \ (user.username, h.short_id(revision)) # get the current participants of this changeset recipients = ChangesetComment.get_users(revision=revision) # add changeset author if it's in rhodecode system cs_author = User.get_from_cs_author(cs.author) if not cs_author: #use repo owner if we cannot extract the author correctly cs_author = repo.user recipients += [cs_author] email_kwargs = { 'status_change': status_change, 'cs_comment_user': h.person(user.email), 'cs_target_repo': h.url('summary_home', repo_name=repo.repo_name, qualified=True), 'cs_comment_url': _url, 'raw_id': revision, 'message': cs.message } #pull request elif pull_request: notification_type = Notification.TYPE_PULL_REQUEST_COMMENT desc = comment.pull_request.title _url = h.url( 'pullrequest_show', repo_name=pull_request.other_repo.repo_name, pull_request_id=pull_request.pull_request_id, anchor='comment-%s' % comment.comment_id, qualified=True, ) subj = safe_unicode( h.link_to('Re pull request #%(pr_id)s: %(desc)s %(line)s' % \ {'desc': desc, 'pr_id': comment.pull_request.pull_request_id, 'line': line}, _url) ) email_subject = 'User %s commented on pull request #%s' % \ (user.username, comment.pull_request.pull_request_id) # get the current participants of this pull request recipients = ChangesetComment.get_users( pull_request_id=pull_request.pull_request_id) # add pull request author recipients += [pull_request.author] # add the reviewers to notification recipients += [x.user for x in pull_request.reviewers] #set some variables for email notification email_kwargs = { 'pr_id': pull_request.pull_request_id, 'status_change': status_change, 'closing_pr': closing_pr, 'pr_comment_url': _url, 'pr_comment_user': h.person(user.email), 'pr_target_repo': h.url('summary_home', repo_name=pull_request.other_repo.repo_name, qualified=True) } return subj, body, recipients, notification_type, email_kwargs, email_subject
def _get_notification_data(self, repo, comment, user, comment_text, line_no=None, revision=None, pull_request=None, status_change=None, closing_pr=False): """ Get notification data :param comment_text: :param line: :returns: tuple (subj,body,recipients,notification_type,email_kwargs) """ # make notification body = comment_text # text of the comment line = '' if line_no: line = _('on line %s') % line_no #changeset if revision: notification_type = Notification.TYPE_CHANGESET_COMMENT cs = repo.scm_instance.get_changeset(revision) desc = "%s" % (cs.short_id) _url = h.url('changeset_home', repo_name=repo.repo_name, revision=revision, anchor='comment-%s' % comment.comment_id, qualified=True, ) subj = safe_unicode( h.link_to('Re changeset: %(desc)s %(line)s' % \ {'desc': desc, 'line': line}, _url) ) email_subject = '%s commented on changeset %s' % \ (user.username, h.short_id(revision)) # get the current participants of this changeset recipients = ChangesetComment.get_users(revision=revision) # add changeset author if it's in rhodecode system cs_author = User.get_from_cs_author(cs.author) if not cs_author: #use repo owner if we cannot extract the author correctly cs_author = repo.user recipients += [cs_author] email_kwargs = { 'status_change': status_change, 'cs_comment_user': h.person(user.email), 'cs_target_repo': h.url('summary_home', repo_name=repo.repo_name, qualified=True), 'cs_comment_url': _url, 'raw_id': revision, 'message': cs.message } #pull request elif pull_request: notification_type = Notification.TYPE_PULL_REQUEST_COMMENT desc = comment.pull_request.title _url = h.url('pullrequest_show', repo_name=pull_request.other_repo.repo_name, pull_request_id=pull_request.pull_request_id, anchor='comment-%s' % comment.comment_id, qualified=True, ) subj = safe_unicode( h.link_to('Re pull request #%(pr_id)s: %(desc)s %(line)s' % \ {'desc': desc, 'pr_id': comment.pull_request.pull_request_id, 'line': line}, _url) ) email_subject = '%s commented on pull request #%s' % \ (user.username, comment.pull_request.pull_request_id) # get the current participants of this pull request recipients = ChangesetComment.get_users(pull_request_id= pull_request.pull_request_id) # add pull request author recipients += [pull_request.author] # add the reviewers to notification recipients += [x.user for x in pull_request.reviewers] #set some variables for email notification email_kwargs = { 'pr_title': pull_request.title, 'pr_id': pull_request.pull_request_id, 'status_change': status_change, 'closing_pr': closing_pr, 'pr_comment_url': _url, 'pr_comment_user': h.person(user.email), 'pr_target_repo': h.url('summary_home', repo_name=pull_request.other_repo.repo_name, qualified=True) } return subj, body, recipients, notification_type, email_kwargs, email_subject
def index(self): """GET /users: All items in the collection""" # url('users') from rhodecode.lib.utils import PartialRenderer _render = PartialRenderer('data_table/_dt_elements.html') def grav_tmpl(user_email, size): return _render("user_gravatar", user_email, size) def username(user_id, username): return _render("user_name", user_id, username) def user_actions(user_id, username): return _render("user_actions", user_id, username) # json generate c.users_list = User.query()\ .filter(User.username != User.DEFAULT_USER) \ .all() users_data = [] for user in c.users_list: users_data.append({ "gravatar": grav_tmpl(user.email, 20), "username": h.link_to(user.username, h.url('user_profile', username=user.username)), "username_raw": user.username, "email": user.email, "first_name": h.escape(user.name), "last_name": h.escape(user.lastname), "last_login": h.format_date(user.last_login), "last_login_raw": datetime_to_time(user.last_login), "last_activity": h.format_date( h.time_to_datetime(user.user_data.get('last_activity', 0))), "last_activity_raw": user.user_data.get('last_activity', 0), "active": h.bool2icon(user.active), "active_raw": user.active, "admin": h.bool2icon(user.admin), "admin_raw": user.admin, "extern_type": user.extern_type, "extern_name": user.extern_name, "action": user_actions(user.user_id, user.username), }) c.data = json.dumps(users_data) return render('admin/users/users.html')
def create(self, text, repo_id, user_id, revision, f_path=None, line_no=None): """ Creates new comment for changeset :param text: :param repo_id: :param user_id: :param revision: :param f_path: :param line_no: """ if text: repo = Repository.get(repo_id) cs = repo.scm_instance.get_changeset(revision) desc = "%s - %s" % (cs.short_id, h.shorter(cs.message, 256)) author_email = cs.author_email comment = ChangesetComment() comment.repo = repo comment.user_id = user_id comment.revision = revision comment.text = text comment.f_path = f_path comment.line_no = line_no self.sa.add(comment) self.sa.flush() # make notification line = '' if line_no: line = _('on line %s') % line_no subj = safe_unicode( h.link_to('Re commit: %(commit_desc)s %(line)s' % \ {'commit_desc': desc, 'line': line}, h.url('changeset_home', repo_name=repo.repo_name, revision=revision, anchor='comment-%s' % comment.comment_id, qualified=True, ) ) ) body = text # get the current participants of this changeset recipients = ChangesetComment.get_users(revision=revision) # add changeset author if it's in rhodecode system recipients += [User.get_by_email(author_email)] NotificationModel().create( created_by=user_id, subject=subj, body=body, recipients=recipients, type_=Notification.TYPE_CHANGESET_COMMENT) mention_recipients = set(self._extract_mentions(body))\ .difference(recipients) if mention_recipients: subj = _('[Mention]') + ' ' + subj NotificationModel().create( created_by=user_id, subject=subj, body=body, recipients=mention_recipients, type_=Notification.TYPE_CHANGESET_COMMENT) return comment
def create(self, text, repo_id, user_id, revision, f_path=None, line_no=None): """ Creates new comment for changeset :param text: :param repo_id: :param user_id: :param revision: :param f_path: :param line_no: """ if text: repo = Repository.get(repo_id) cs = repo.scm_instance.get_changeset(revision) desc = "%s - %s" % (cs.short_id, h.shorter(cs.message, 256)) author_email = cs.author_email comment = ChangesetComment() comment.repo = repo comment.user_id = user_id comment.revision = revision comment.text = text comment.f_path = f_path comment.line_no = line_no self.sa.add(comment) self.sa.flush() # make notification line = '' if line_no: line = _('on line %s') % line_no subj = safe_unicode( h.link_to('Re commit: %(commit_desc)s %(line)s' % \ {'commit_desc': desc, 'line': line}, h.url('changeset_home', repo_name=repo.repo_name, revision=revision, anchor='comment-%s' % comment.comment_id, qualified=True, ) ) ) body = text # get the current participants of this changeset recipients = ChangesetComment.get_users(revision=revision) # add changeset author if it's in rhodecode system recipients += [User.get_by_email(author_email)] NotificationModel().create( created_by=user_id, subject=subj, body=body, recipients=recipients, type_=Notification.TYPE_CHANGESET_COMMENT ) mention_recipients = set(self._extract_mentions(body))\ .difference(recipients) if mention_recipients: subj = _('[Mention]') + ' ' + subj NotificationModel().create( created_by=user_id, subject=subj, body=body, recipients=mention_recipients, type_=Notification.TYPE_CHANGESET_COMMENT ) return comment
def create(self, created_by, org_repo, org_ref, other_repo, other_ref, revisions, reviewers, title, description=None): from rhodecode.model.changeset_status import ChangesetStatusModel created_by_user = self._get_user(created_by) org_repo = self._get_repo(org_repo) other_repo = self._get_repo(other_repo) new = PullRequest() new.org_repo = org_repo new.org_ref = org_ref new.other_repo = other_repo new.other_ref = other_ref new.revisions = revisions new.title = title new.description = description new.author = created_by_user Session().add(new) Session().flush() #members for member in set(reviewers): _usr = self._get_user(member) reviewer = PullRequestReviewers(_usr, new) Session().add(reviewer) #reset state to under-review ChangesetStatusModel().set_status( repo=org_repo, status=ChangesetStatus.STATUS_UNDER_REVIEW, user=created_by_user, pull_request=new ) revision_data = [(x.raw_id, x.message) for x in map(org_repo.get_changeset, revisions)] #notification to reviewers notif = NotificationModel() pr_url = h.url('pullrequest_show', repo_name=other_repo.repo_name, pull_request_id=new.pull_request_id, qualified=True, ) subject = safe_unicode( h.link_to( _('%(user)s wants you to review pull request #%(pr_id)s: %(pr_title)s') % \ {'user': created_by_user.username, 'pr_title': new.title, 'pr_id': new.pull_request_id}, pr_url ) ) body = description kwargs = { 'pr_title': title, 'pr_user_created': h.person(created_by_user.email), 'pr_repo_url': h.url('summary_home', repo_name=other_repo.repo_name, qualified=True,), 'pr_url': pr_url, 'pr_revisions': revision_data } notif.create(created_by=created_by_user, subject=subject, body=body, recipients=reviewers, type_=Notification.TYPE_PULL_REQUEST, email_kwargs=kwargs) return new