def test_delete_association(self): notification = NotificationModel().create( created_by=self.u1, notification_subject=u'title', notification_body=u'hi there3', recipients=[self.u3, self.u1, self.u2]) Session().commit() unotification = (UserNotification.query().filter( UserNotification.notification == notification).filter( UserNotification.user_id == self.u3).scalar()) assert unotification.user_id == self.u3 NotificationModel().delete(self.u3, notification.notification_id) Session().commit() u3notification = (UserNotification.query().filter( UserNotification.notification == notification).filter( UserNotification.user_id == self.u3).scalar()) assert u3notification is None # notification object is still there assert Notification.query().all() == [notification] # u1 and u2 still have assignments u1notification = (UserNotification.query().filter( UserNotification.notification == notification).filter( UserNotification.user_id == self.u1).scalar()) assert u1notification is not None u2notification = (UserNotification.query().filter( UserNotification.notification == notification).filter( UserNotification.user_id == self.u2).scalar()) assert u2notification is not None
def mark_all_read(self): if request.environ.get('HTTP_X_PARTIAL_XHR'): nm = NotificationModel() # mark all read nm.mark_all_read_for_user(self.rhodecode_user.user_id) Session.commit() c.user = self.rhodecode_user notif = nm.get_for_user(self.rhodecode_user.user_id) c.notifications = Page(notif, page=1, items_per_page=10) return render('admin/notifications/notifications_data.html')
def mark_all_read(self): if request.environ.get("HTTP_X_PARTIAL_XHR"): nm = NotificationModel() # mark all read nm.mark_all_read_for_user(self.rhodecode_user.user_id) Session.commit() c.user = self.rhodecode_user notif = nm.get_for_user(self.rhodecode_user.user_id) c.notifications = Page(notif, page=1, items_per_page=10) return render("admin/notifications/notifications_data.html")
def mark_all_read(self): if request.environ.get('HTTP_X_PARTIAL_XHR'): nm = NotificationModel() # mark all read nm.mark_all_read_for_user(self.rhodecode_user.user_id, filter_=request.GET.getall('type')) Session().commit() c.user = self.rhodecode_user notif = nm.get_for_user(self.rhodecode_user.user_id, filter_=request.GET.getall('type')) c.notifications = Page(notif, page=1, items_per_page=10) return render('admin/notifications/notifications_data.html')
def test_create_notification_fails_for_invalid_recipients(self): with pytest.raises(Exception): NotificationModel().create(created_by=self.u1, notification_subject=u'subj', notification_body=u'hi there', recipients=['bad_user_id']) with pytest.raises(Exception): NotificationModel().create(created_by=self.u1, notification_subject=u'subj', notification_body=u'hi there', recipients=[])
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 test_notification_counter(self): self._clean_notifications() self.assertEqual([], Notification.query().all()) self.assertEqual([], UserNotification.query().all()) NotificationModel().create(created_by=self.u1, subject=u'title', body=u'hi there_delete', recipients=[self.u3, self.u1]) Session.commit() self.assertEqual(NotificationModel().get_unread_cnt_for_user(self.u1), 1) self.assertEqual(NotificationModel().get_unread_cnt_for_user(self.u2), 0) self.assertEqual(NotificationModel().get_unread_cnt_for_user(self.u3), 1) notification = NotificationModel().create( created_by=self.u1, subject=u'title', body=u'hi there3', recipients=[self.u3, self.u1, self.u2]) Session.commit() self.assertEqual(NotificationModel().get_unread_cnt_for_user(self.u1), 2) self.assertEqual(NotificationModel().get_unread_cnt_for_user(self.u2), 1) self.assertEqual(NotificationModel().get_unread_cnt_for_user(self.u3), 2)
def test_create_notification(self): self.assertEqual([], Notification.query().all()) self.assertEqual([], UserNotification.query().all()) usrs = [self.u1, self.u2] notification = NotificationModel().create(created_by=self.u1, subject=u'subj', body=u'hi there', recipients=usrs) Session().commit() u1 = User.get(self.u1) u2 = User.get(self.u2) u3 = User.get(self.u3) notifications = Notification.query().all() self.assertEqual(len(notifications), 1) self.assertEqual(notifications[0].recipients, [u1, u2]) self.assertEqual(notification.notification_id, notifications[0].notification_id) unotification = UserNotification.query()\ .filter(UserNotification.notification == notification).all() self.assertEqual(len(unotification), len(usrs)) self.assertEqual(set([x.user.user_id for x in unotification]), set(usrs))
def test_delete(self): self.log_user() cur_user = self._get_logged_user() u1 = UserModel().create_or_update(username='******', password='******', email='*****@*****.**', firstname='u1', lastname='u1') u2 = UserModel().create_or_update(username='******', password='******', email='*****@*****.**', firstname='u2', lastname='u2') # make notifications notification = NotificationModel().create(created_by=cur_user, subject=u'test', body=u'hi there', recipients=[cur_user, u1, u2]) Session().commit() u1 = User.get(u1.user_id) u2 = User.get(u2.user_id) # check DB get_notif = lambda un: [x.notification for x in un] self.assertEqual(get_notif(cur_user.notifications), [notification]) self.assertEqual(get_notif(u1.notifications), [notification]) self.assertEqual(get_notif(u2.notifications), [notification]) cur_usr_id = cur_user.user_id response = self.app.delete(url('notification', notification_id= notification.notification_id)) self.assertEqual(response.body, 'ok') cur_user = User.get(cur_usr_id) self.assertEqual(cur_user.notifications, [])
def __before__(self): """ __before__ is called before controller methods and after __call__ """ c.rhodecode_version = __version__ c.rhodecode_instanceid = config.get('instance_id') c.rhodecode_name = config.get('rhodecode_title') c.rhodecode_bugtracker = config.get('bugtracker', 'http://bitbucket.org/marcinkuzminski/rhodecode/issues') c.use_gravatar = str2bool(config.get('use_gravatar')) c.ga_code = config.get('rhodecode_ga_code') # Visual options c.visual = AttributeDict({}) rc_config = RhodeCodeSetting.get_app_settings() ## DB stored c.visual.show_public_icon = str2bool(rc_config.get('rhodecode_show_public_icon')) c.visual.show_private_icon = str2bool(rc_config.get('rhodecode_show_private_icon')) c.visual.stylify_metatags = str2bool(rc_config.get('rhodecode_stylify_metatags')) c.visual.dashboard_items = safe_int(rc_config.get('rhodecode_dashboard_items', 100)) c.visual.repository_fields = str2bool(rc_config.get('rhodecode_repository_fields')) c.visual.show_version = str2bool(rc_config.get('rhodecode_show_version')) ## INI stored self.cut_off_limit = int(config.get('cut_off_limit')) c.visual.allow_repo_location_change = str2bool(config.get('allow_repo_location_change', True)) c.visual.allow_custom_hooks_settings = str2bool(config.get('allow_custom_hooks_settings', True)) c.repo_name = get_repo_slug(request) # can be empty c.backends = BACKENDS.keys() c.unread_notifications = NotificationModel()\ .get_unread_cnt_for_user(c.rhodecode_user.user_id) self.sa = meta.Session self.scm_model = ScmModel(self.sa)
def index(self): """GET /_admin/notifications: All items in the collection""" # url('notifications') c.user = c.rhodecode_user notif = NotificationModel().get_for_user( c.rhodecode_user.user_id, filter_=request.GET.getall('type')) p = safe_int(request.GET.get('page', 1), 1) notifications_url = webhelpers.paginate.PageURL( url('notifications'), request.GET) c.notifications = Page(notif, page=p, items_per_page=10, url=notifications_url) c.pull_request_type = Notification.TYPE_PULL_REQUEST c.comment_type = [ Notification.TYPE_CHANGESET_COMMENT, Notification.TYPE_PULL_REQUEST_COMMENT ] _current_filter = request.GET.getall('type') c.current_filter = 'all' if _current_filter == [c.pull_request_type]: c.current_filter = 'pull_request' elif _current_filter == c.comment_type: c.current_filter = 'comment' if request.is_xhr: return render('admin/notifications/notifications_data.html') return render('admin/notifications/notifications.html')
def create_registration(self, form_data): from rhodecode.model.notification import NotificationModel try: form_data['admin'] = False new_user = self.create(form_data) self.sa.add(new_user) self.sa.flush() # notification to admins subject = _('New user registration') body = ('New user registration\n' '---------------------\n' '- Username: %s\n' '- Full Name: %s\n' '- Email: %s\n') body = body % (new_user.username, new_user.full_name, new_user.email) edit_url = url('edit_user', id=new_user.user_id, qualified=True) kw = {'registered_user_url': edit_url} NotificationModel().create(created_by=new_user, subject=subject, body=body, recipients=None, type_=Notification.TYPE_REGISTRATION, email_kwargs=kw) except Exception: log.error(traceback.format_exc()) raise
def test_show(self, user, password): self.log_user(user, password) cur_user = self._get_logged_user() u1 = UserModel().create_or_update(username='******', password='******', email='*****@*****.**', firstname='u1', lastname='u1') u2 = UserModel().create_or_update(username='******', password='******', email='*****@*****.**', firstname='u2', lastname='u2') self.destroy_users.add('u1') self.destroy_users.add('u2') subject = u'test' notif_body = u'hi there' notification = NotificationModel().create( created_by=cur_user, notification_subject=subject, notification_body=notif_body, recipients=[cur_user, u1, u2]) response = self.app.get( url('notification', notification_id=notification.notification_id)) response.mustcontain(subject) response.mustcontain(notif_body)
def test_delete_association(self): self.assertEqual([], Notification.query().all()) self.assertEqual([], UserNotification.query().all()) notification = NotificationModel().create( created_by=self.u1, subject=u'title', body=u'hi there3', recipients=[self.u3, self.u1, self.u2]) Session.commit() unotification = UserNotification.query()\ .filter(UserNotification.notification == notification)\ .filter(UserNotification.user_id == self.u3)\ .scalar() self.assertEqual(unotification.user_id, self.u3) NotificationModel().delete(self.u3, notification.notification_id) Session.commit() u3notification = UserNotification.query()\ .filter(UserNotification.notification == notification)\ .filter(UserNotification.user_id == self.u3)\ .scalar() self.assertEqual(u3notification, None) # notification object is still there self.assertEqual(Notification.query().all(), [notification]) #u1 and u2 still have assignments u1notification = UserNotification.query()\ .filter(UserNotification.notification == notification)\ .filter(UserNotification.user_id == self.u1)\ .scalar() self.assertNotEqual(u1notification, None) u2notification = UserNotification.query()\ .filter(UserNotification.notification == notification)\ .filter(UserNotification.user_id == self.u2)\ .scalar() self.assertNotEqual(u2notification, None)
def test_user_notifications(self): notification1 = NotificationModel().create( created_by=self.u1, notification_subject=u'subj', notification_body=u'hi there1', recipients=[self.u3]) Session().commit() notification2 = NotificationModel().create( created_by=self.u1, notification_subject=u'subj', notification_body=u'hi there2', recipients=[self.u3]) Session().commit() u3 = Session().query(User).get(self.u3) assert sorted([x.notification for x in u3.notifications]) ==\ sorted([notification2, notification1])
def mark_all_read(self): if request.is_xhr: nm = NotificationModel() # mark all read nm.mark_all_read_for_user(c.rhodecode_user.user_id, filter_=request.GET.getall('type')) Session().commit() c.user = c.rhodecode_user notif = nm.get_for_user(c.rhodecode_user.user_id, filter_=request.GET.getall('type')) notifications_url = webhelpers.paginate.PageURL( url('notifications'), request.GET) c.notifications = Page(notif, page=1, items_per_page=10, url=notifications_url) return render('admin/notifications/notifications_data.html')
def index(self, format='html'): """GET /_admin/notifications: All items in the collection""" # url('notifications') c.user = self.rhodecode_user notif = NotificationModel().get_for_user(self.rhodecode_user.user_id) p = int(request.params.get('page', 1)) c.notifications = Page(notif, page=p, items_per_page=10) return render('admin/notifications/notifications.html')
def test_user_notifications(self): self.assertEqual([], Notification.query().all()) self.assertEqual([], UserNotification.query().all()) notification1 = NotificationModel().create(created_by=self.u1, subject=u'subj', body=u'hi there1', recipients=[self.u3]) Session.commit() notification2 = NotificationModel().create(created_by=self.u1, subject=u'subj', body=u'hi there2', recipients=[self.u3]) Session.commit() u3 = Session.query(User).get(self.u3) self.assertEqual(sorted([x.notification for x in u3.notifications]), sorted([notification2, notification1]))
def show(self, notification_id, format="html"): """GET /_admin/notifications/id: Show a specific item""" # url('notification', notification_id=ID) c.user = self.rhodecode_user no = Notification.get(notification_id) owner = lambda: (no.notifications_to_users.user.user_id == c.user.user_id) if no and (h.HasPermissionAny("hg.admin", "repository.admin")() or owner): unotification = NotificationModel().get_user_notification(c.user.user_id, no) # if this association to user is not valid, we don't want to show # this message if unotification: if unotification.read is False: unotification.mark_as_read() Session.commit() c.notification = no return render("admin/notifications/show_notification.html") return redirect(url("notifications"))
def show(self, notification_id): """GET /_admin/notifications/id: Show a specific item""" # url('notification', notification_id=ID) c.user = c.rhodecode_user no = Notification.get(notification_id) if no and self._has_permissions(no): unotification = NotificationModel()\ .get_user_notification(c.user.user_id, no) # if this association to user is not valid, we don't want to show # this message if unotification: if not unotification.read: unotification.mark_as_read() Session().commit() c.notification = no return render('admin/notifications/show_notification.html') return abort(403)
def __before__(self): c.rhodecode_version = __version__ c.rhodecode_instanceid = config.get('instance_id') c.rhodecode_name = config.get('rhodecode_title') c.use_gravatar = str2bool(config.get('use_gravatar')) c.ga_code = config.get('rhodecode_ga_code') c.repo_name = get_repo_slug(request) c.backends = BACKENDS.keys() c.unread_notifications = NotificationModel()\ .get_unread_cnt_for_user(c.rhodecode_user.user_id) self.cut_off_limit = int(config.get('cut_off_limit')) self.sa = meta.Session self.scm_model = ScmModel(self.sa)
def show(self, notification_id, format='html'): """GET /_admin/notifications/id: Show a specific item""" # url('notification', notification_id=ID) c.user = self.rhodecode_user no = Notification.get(notification_id) owner = lambda: (no.notifications_to_users.user.user_id == c.user. user_id) if no and (h.HasPermissionAny('hg.admin', 'repository.admin')() or owner): unotification = NotificationModel()\ .get_user_notification(c.user.user_id, no) # if this association to user is not valid, we don't want to show # this message if unotification: if unotification.read is False: unotification.mark_as_read() Session.commit() c.notification = no return render('admin/notifications/show_notification.html') return redirect(url('notifications'))
def show(self, notification_id, format='html'): """GET /_admin/notifications/id: Show a specific item""" # url('notification', notification_id=ID) c.user = self.rhodecode_user no = Notification.get(notification_id) owner = any(un.user.user_id == c.rhodecode_user.user_id for un in no.notifications_to_users) if no and (h.HasPermissionAny('hg.admin', 'repository.admin')() or owner): unotification = NotificationModel()\ .get_user_notification(c.user.user_id, no) # if this association to user is not valid, we don't want to show # this message if unotification: if not unotification.read: unotification.mark_as_read() Session().commit() c.notification = no return render('admin/notifications/show_notification.html') return abort(403)
def test_show(self): self.log_user() cur_user = self._get_logged_user() u1 = UserModel().create_or_update(username='******', password='******', email='*****@*****.**', firstname='u1', lastname='u1') u2 = UserModel().create_or_update(username='******', password='******', email='*****@*****.**', firstname='u2', lastname='u2') notification = NotificationModel().create(created_by=cur_user, subject=u'test', body=u'hi there', recipients=[cur_user, u1, u2]) response = self.app.get(url('notification', notification_id=notification.notification_id))
def test_delete(self, user, password): self.log_user(user, password) cur_user = self._get_logged_user() u1 = UserModel().create_or_update(username='******', password='******', email='*****@*****.**', firstname='u1', lastname='u1') u2 = UserModel().create_or_update(username='******', password='******', email='*****@*****.**', firstname='u2', lastname='u2') self.destroy_users.add('u1') self.destroy_users.add('u2') # make notifications notification = NotificationModel().create( created_by=cur_user, notification_subject=u'test', notification_body=u'hi there', recipients=[cur_user, u1, u2]) Session().commit() u1 = User.get(u1.user_id) u2 = User.get(u2.user_id) # check DB get_notif = lambda un: [x.notification for x in un] assert get_notif(cur_user.notifications) == [notification] assert get_notif(u1.notifications) == [notification] assert get_notif(u2.notifications) == [notification] cur_usr_id = cur_user.user_id response = self.app.post(url( 'notification', notification_id=notification.notification_id), params={ '_method': 'delete', 'csrf_token': self.csrf_token }) assert response.body == 'ok' cur_user = User.get(cur_usr_id) assert cur_user.notifications == []
def test_delete_notifications(self): notification = NotificationModel().create( created_by=self.u1, notification_subject=u'title', notification_body=u'hi there3', recipients=[self.u3, self.u1, self.u2]) Session().commit() notifications = Notification.query().all() assert notification in notifications Notification.delete(notification.notification_id) Session().commit() notifications = Notification.query().all() assert notification not in notifications un = UserNotification.query().filter( UserNotification.notification == notification).all() assert un == []
def delete(self, notification_id): """DELETE /_admin/notifications/id: Delete an existing item""" # Forms posted to this method should contain a hidden field: # <input type="hidden" name="_method" value="DELETE" /> # Or using helpers: # h.form(url('notification', notification_id=ID), # method='delete') # url('notification', notification_id=ID) try: no = Notification.get(notification_id) if self._has_permissions(no): # deletes only notification2user NotificationModel().delete(c.rhodecode_user.user_id, no) Session().commit() return 'ok' except Exception: Session().rollback() log.exception("Exception deleting a notification item") raise HTTPBadRequest()
def test_index(self): self.log_user() u1 = UserModel().create_or_update(username='******', password='******', email='*****@*****.**', firstname='u1', lastname='u1') u1 = u1.user_id response = self.app.get(url('notifications')) response.mustcontain('<div class="table">No notifications here yet</div>') cur_user = self._get_logged_user() NotificationModel().create(created_by=u1, subject=u'test_notification_1', body=u'notification_1', recipients=[cur_user]) Session().commit() response = self.app.get(url('notifications')) response.mustcontain(u'test_notification_1')
def test_notification_counter(self): NotificationModel().create(created_by=self.u1, notification_subject=u'title', notification_body=u'hi there_delete', recipients=[self.u3, self.u1]) Session().commit() # creator has it's own notification marked as read assert NotificationModel().get_unread_cnt_for_user(self.u1) == 0 assert NotificationModel().get_unread_cnt_for_user(self.u2) == 0 assert NotificationModel().get_unread_cnt_for_user(self.u3) == 1 NotificationModel().create(created_by=self.u1, notification_subject=u'title', notification_body=u'hi there3', recipients=[self.u3, self.u1, self.u2]) Session().commit() # creator has it's own notification marked as read assert NotificationModel().get_unread_cnt_for_user(self.u1) == 0 assert NotificationModel().get_unread_cnt_for_user(self.u2) == 1 assert NotificationModel().get_unread_cnt_for_user(self.u3) == 2
def test_update(self, user, password): self.log_user(user, password) cur_user = self._get_logged_user() u1 = UserModel().create_or_update(username='******', password='******', email='*****@*****.**', firstname='u1', lastname='u1') u2 = UserModel().create_or_update(username='******', password='******', email='*****@*****.**', firstname='u2', lastname='u2') self.destroy_users.add('u1') self.destroy_users.add('u2') # make notifications recipients = [cur_user, u1, u2] notification = NotificationModel().create( created_by=cur_user, notification_subject=u'test', notification_body=u'hi there', recipients=recipients) Session().commit() for u_obj in recipients: # if it's current user, he has his message already read read = u_obj.username == user assert len(u_obj.notifications) == 1 assert u_obj.notifications[0].read == read response = self.app.post(url( 'notification', notification_id=notification.notification_id), params={ '_method': 'put', 'csrf_token': self.csrf_token }) assert response.body == 'ok' cur_user = self._get_logged_user() assert True == cur_user.notifications[0].read
def update(self, notification_id): """PUT /_admin/notifications/id: Update an existing item""" # Forms posted to this method should contain a hidden field: # <input type="hidden" name="_method" value="PUT" /> # Or using helpers: # h.form(url('notification', notification_id=ID), # method='put') # url('notification', notification_id=ID) try: no = Notification.get(notification_id) owner = all(un.user.user_id == c.rhodecode_user.user_id for un in no.notifications_to_users) if h.HasPermissionAny('hg.admin')() or owner: NotificationModel().mark_read(c.rhodecode_user.user_id, no) Session().commit() return 'ok' except Exception: Session().rollback() log.error(traceback.format_exc()) return 'fail'
def index(self, format='html'): """GET /_admin/notifications: All items in the collection""" # url('notifications') c.user = self.rhodecode_user notif = NotificationModel().get_for_user(self.rhodecode_user.user_id, filter_=request.GET.getall('type')) p = safe_int(request.GET.get('page', 1), 1) c.notifications = Page(notif, page=p, items_per_page=10) c.pull_request_type = Notification.TYPE_PULL_REQUEST c.comment_type = [Notification.TYPE_CHANGESET_COMMENT, Notification.TYPE_PULL_REQUEST_COMMENT] _current_filter = request.GET.getall('type') c.current_filter = 'all' if _current_filter == [c.pull_request_type]: c.current_filter = 'pull_request' elif _current_filter == c.comment_type: c.current_filter = 'comment' return render('admin/notifications/notifications.html')
def test_create_notification(self): usrs = [self.u1, self.u2] notification = NotificationModel().create( created_by=self.u1, notification_subject=u'subj', notification_body=u'hi there', recipients=usrs) Session().commit() u1 = User.get(self.u1) u2 = User.get(self.u2) notifications = Notification.query().all() assert len(notifications) == 1 assert notifications[0].recipients, [u1 == u2] assert notification.notification_id == notifications[0].notification_id unotification = UserNotification.query()\ .filter(UserNotification.notification == notification).all() assert len(unotification) == len(usrs) assert set([x.user.user_id for x in unotification]) == set(usrs)
def delete(self, notification_id): """DELETE /_admin/notifications/id: Delete an existing item""" # Forms posted to this method should contain a hidden field: # <input type="hidden" name="_method" value="DELETE" /> # Or using helpers: # h.form(url('notification', notification_id=ID), # method='delete') # url('notification', notification_id=ID) try: no = Notification.get(notification_id) owner = lambda: (no.notifications_to_users.user.user_id == c. rhodecode_user.user_id) if h.HasPermissionAny('hg.admin', 'repository.admin')() or owner: NotificationModel().delete(c.rhodecode_user.user_id, no) Session.commit() return 'ok' except Exception: Session.rollback() log.error(traceback.format_exc()) return 'fail'
def create_registration(self, form_data): from rhodecode.model.notification import NotificationModel from rhodecode.model.notification import EmailNotificationModel try: form_data['admin'] = False form_data['extern_name'] = 'rhodecode' form_data['extern_type'] = 'rhodecode' new_user = self.create(form_data) self.sa.add(new_user) self.sa.flush() user_data = new_user.get_dict() kwargs = { # use SQLALCHEMY safe dump of user data 'user': AttributeDict(user_data), 'date': datetime.datetime.now() } notification_type = EmailNotificationModel.TYPE_REGISTRATION # pre-generate the subject for notification itself (subject, _h, _e, # we don't care about those body_plaintext) = EmailNotificationModel().render_email( notification_type, **kwargs) # create notification objects, and emails NotificationModel().create( created_by=new_user, notification_subject=subject, notification_body=body_plaintext, notification_type=notification_type, recipients=None, # all admins email_kwargs=kwargs, ) return new_user except Exception: log.error(traceback.format_exc()) raise
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