def invitation(self, group): """Act on the invitation of the current user to this group.""" if hasattr(self, 'form_result'): invitations = meta.Session.query(PendingInvitation ).filter_by(group=group, user=c.user, active=True ).all() if invitations: if self.form_result.get('accept', '') == 'True': group.add_member(c.user) if c.user.location is None: c.user.location = group.location h.flash(_("Congratulations! You are now a member of the group '%s'") % group.title) else: h.flash(_("Invitation to group '%s' rejected.") % group.title) self._clear_requests(group, c.user) meta.Session.commit() came_from = self.form_result.get('came_from', None) if came_from is None: redirect(url(controller='group', action='home', id=group.group_id)) else: redirect(came_from.encode('utf-8')) else: redirect(url(controller='group', action='home', id=group.group_id))
def watch_subject(self): self._watch_subject() if request.params.has_key("js"): return "OK" else: h.flash(render_mako_def("subject/flash_messages.mako", "watch_subject", subject=self._getSubject())) redirect(request.referrer)
def edit_student_group(self, id): try: group = TeacherGroup.get(int(id)) except ValueError: abort(404) if group is None or group.teacher != c.user: abort(404) c.student_group = group defaults = { 'title' : group.title, 'email' : group.email, 'group_id' : group.id } if hasattr(self, 'form_result'): group.title = self.form_result['title'] group.email = self.form_result['email'] group.update_binding() meta.Session.commit() message = _(u'Group %(group_title)s (%(group_email)s) updated!') % { 'group_title': group.title, 'group_email': group.email} h.flash(message) redirect(url(controller='profile', action='dashboard')) return htmlfill.render(self._edit_student_group(), defaults=defaults)
def invite_fb(self, group): # Handle POST. invited = request.params.get('ids[]') if invited: ids = invited.split(',') for facebook_id in ids: group.create_pending_fb_invitation(int(facebook_id), c.user) make_facebook_invitations(ids, c.user, group.location) meta.Session.commit() h.flash(ungettext('Invited %(num)d friend.', 'Invited %(num)d friends.', len(ids)) % dict(num=len(ids))) redirect(c.group.url(action='members')) # Render page. fb_user = facebook.get_user_from_cookie(request.cookies, config['facebook.appid'], config['facebook.secret']) c.has_facebook = fb_user is not None if c.has_facebook: try: graph = facebook.GraphAPI(fb_user['access_token']) friends = graph.get_object("me/friends") except facebook.GraphAPIError: c.has_facebook = False if not c.has_facebook: # Ask to log on to facebook. return render('group/invite.mako') friend_ids = [f['id'] for f in friends['data']] friend_users = meta.Session.query(User).filter( User.facebook_id.in_(friend_ids)).all() c.exclude_ids = ','.join(str(u.facebook_id) for u in friend_users if c.group.is_member(u)) return render('group/invite.mako')
def document(self): resp = request.environ.get('pylons.original_response') req = request.environ.get('pylons.original_request') c.came_from = url.current() if resp is None: return render("/error.mako") c.reason = req.environ.get('ututi.access_denied_reason', None) if resp.status_int in [403, 404]: self.form_result = {} self._search() c.came_from = url('/') if resp.status_int == 403: return render("/access_denied.mako") elif resp.status_int == 404: h.flash(_("Document at %(url)s was not found, but maybe you are interested in something else?") % { 'url': req.url.encode('ascii', 'ignore')}) # if user is logged in, show search form, otherwise - login form try: if session['login']: return render('/search/index.mako') except KeyError: return render('/login.mako') return render("/error.mako")
def update(self): values = { 'fullname': None, 'description': None, 'profile_is_public': None, 'url_name': None, 'teacher_position': None, 'teacher_sub_department': None, } values.update(self.form_result) c.user.fullname = values['fullname'] if values['description'] is not None: # this check is needed because description field # is currently reused as teacher's information and # is not displayed for teacher in this form. c.user.description = values['description'] c.user.profile_is_public = bool(values['profile_is_public']) c.user.url_name = values['url_name'] if c.user.is_teacher: c.user.profile_is_public = True # teacher profile always public c.user.teacher_position = values['teacher_position'] # additional teacher fields sd_id = values['teacher_sub_department'] if sd_id is not None: if sd_id != '': sd = meta.Session.query(SubDepartment).filter_by(id=sd_id).one() if sd is not None: c.user.sub_department = sd else: c.user.sub_department = None meta.Session.commit() h.flash(_('Your profile was updated.')) redirect(url(controller='profile', action='edit'))
def invite_friends_fb(self): # handle facebook callback ids = request.params.get('ids[]') if ids: ids = map(int, ids.split(',')) invited = make_facebook_invitations(ids, c.user, c.user.location) meta.Session.commit() if invited: h.flash(ungettext('Invited %(num)d friend.', 'Invited %(num)d friends.', len(invited)) % dict(num=len(invited))) redirect(url(controller='profile', action='home')) # render page fb_user = facebook.get_user_from_cookie(request.cookies, config['facebook.appid'], config['facebook.secret']) c.has_facebook = fb_user is not None if c.has_facebook: try: graph = facebook.GraphAPI(fb_user['access_token']) friends = graph.get_object("me/friends") friend_ids = [f['id'] for f in friends['data']] friend_users = meta.Session.query(User)\ .filter(User.facebook_id.in_(friend_ids))\ .filter(User.location == c.user.location).all() c.exclude_ids = ','.join(str(u.facebook_id) for u in friend_users) except facebook.GraphAPIError: c.has_facebook = False return render('profile/invite_friends_fb.mako')
def delete_my_account(self): if hasattr(self, 'form_result'): h.flash(_('Your account has been successfully removed!')) c.user.delete_user() meta.Session.commit() redirect(url(controller='home', action='logout')) redirect(url(controller='profile', action='login_settings'))
def google_login(self): openid_session = session.get("openid_session", {}) openid_store = None # stateless cons = Consumer(openid_session, openid_store) GOOGLE_OPENID = 'https://www.google.com/accounts/o8/id' openid = GOOGLE_OPENID try: authrequest = cons.begin(openid) except DiscoveryFailure: h.flash(_('Authentication failed, please try again.')) redirect(c.came_from or url(controller='home', action='index')) ax_req = ax.FetchRequest() ax_req.add(ax.AttrInfo('http://axschema.org/namePerson/first', alias='firstname', required=True)) ax_req.add(ax.AttrInfo('http://axschema.org/namePerson/last', alias='lastname', required=True)) ax_req.add(ax.AttrInfo('http://schema.openid.net/contact/email', alias='email', required=True)) authrequest.addExtension(ax_req) kargs = self._auth_args() redirecturl = authrequest.redirectURL( url('frontpage', qualified=True), return_to=url(controller='federation', action='google_verify', qualified=True, **kargs)) session['openid_session'] = openid_session session.save() redirect(redirecturl)
def link_google(self, registration): openid_session = session.get("openid_session", {}) openid_store = None # stateless cons = Consumer(openid_session, openid_store) GOOGLE_OPENID = 'https://www.google.com/accounts/o8/id' try: authrequest = cons.begin(GOOGLE_OPENID) except DiscoveryFailure: h.flash(_('Authentication failed, please try again.')) redirect(registration.url(action='personal_info')) ax_req = ax.FetchRequest() ax_req.add(ax.AttrInfo('http://axschema.org/namePerson/first', alias='firstname', required=True)) ax_req.add(ax.AttrInfo('http://axschema.org/namePerson/last', alias='lastname', required=True)) ax_req.add(ax.AttrInfo('http://schema.openid.net/contact/email', alias='email', required=True)) authrequest.addExtension(ax_req) session['openid_session'] = openid_session session.save() realm = url(controller='home', action='index', qualified=True) return_to = registration.url(action='google_verify', qualified=True) redirect(authrequest.redirectURL(realm, return_to))
def register_teacher(self, location): # bounce existing users to different action if c.user is not None: redirect(location.url(action='register_teacher_existing')) if not hasattr(self, 'form_result'): return htmlfill.render(self._register_teacher_form()) email = self.form_result['email'] if User.get(email, location): h.flash(_('The email you entered is registered in Ututi. ' 'Please login to proceed.')) destination = location.url(action='register_teacher_existing') redirect(url(controller='home', action='login', email=email, came_from=destination)) # lookup/create registration entry and send confirmation code to user registration = UserRegistration.create_or_update(location, email) registration.teacher = True meta.Session.commit() registration.send_confirmation_email() # show confirmation page c.email = email return render('registration/email_approval.mako')
def _reject_post(self, group, thread, redirecturl=None, ajax=False): success = False if thread.in_moderation_queue: thread.reject() meta.Session.commit() success = True if ajax: if success: return render_mako_def('mailinglist/administration.mako', 'rejectedMessage') else: return render_mako_def('mailinglist/administration.mako', 'warningMessage') if success: h.flash(_("Message %(link_to_message)s has been rejected.") % { 'link_to_message': h.link_to(thread.subject, thread.url()) }) else: h.flash(_("Could not reject %(link_to_message)s as it was already approved.") % { 'link_to_message': h.link_to(thread.subject, thread.url()) }) if redirecturl is None: redirecturl = group.url(controller='mailinglist', action='administration') redirect(redirecturl)
def post_anonymous(self, group): post = post_message(group, c.user, self.form_result['subject'], self.form_result['message']) h.flash(_('Your message to the group was successfully sent.')) redirect(group.url())
def send_message(self): self._send_message( self.form_result['group'], self.form_result['subject'], self.form_result['message'], self.form_result.get('category_id', None)) h.flash(_('Message sent.')) self._redirect()
def delete_email_domain(self, id): domain = EmailDomain.get(id) if domain is not None: domain.delete() meta.Session.commit() else: h.flash('Email domain with id %s does not exist' % id) redirect(url(controller='admin', action='email_domains'))
def register_teacher_existing(self, location): if c.user.is_teacher: h.flash(_('You already have a teacher account.')) redirect(url(controller='profile', action='home')) teacher_request_email(c.user) h.flash(_('Thank You! Your request to become a teacher has been received. We will notify You once we grant You the rights of a teacher.')) redirect(location.url())
def update_wall_settings(self): if hasattr(self, 'form_result'): events = set(self.form_result.get('events', [])) events = list(set(Event.event_types()) - events) c.user.update_ignored_events(events) meta.Session.commit() h.flash(_('Your wall settings have been updated.')) redirect(url(controller='profile', action='wall_settings'))
def update_publications(self): if not hasattr(self, 'form_result'): redirect(url(controller='profile', action='edit_publications')) c.user.publications = self.form_result['publications'] meta.Session.commit() h.flash(_('Your publication page was updated.')) redirect(url(controller='profile', action='edit_publications'))
def send_sms(self): if hasattr(self, 'form_result'): msg = SMS(sender=c.user, recipient_number=self.form_result.get('number'), message_text=self.form_result.get('message')) meta.Session.add(msg) meta.Session.commit() h.flash('Message sent to number %s' % self.form_result.get('number')) redirect(url(controller='admin', action='sms'))
def delete(self, group): if len(group.members) > 1: h.flash(_("You can't delete a group while it has members!")) redirect(request.referrer) else: h.flash(_("Group '%(group_title)s' has been deleted!" % dict(group_title=group.title))) meta.Session.delete(group) meta.Session.commit() redirect(url(controller='profile', action='home'))
def update_page(self, group): page_content = self.form_result['page_content'] if page_content is None: page_content = u'' group.page = page_content group.page_public = (self.form_result.get('page_public', False) == 'public') meta.Session.commit() h.flash(_("The group's front page was updated.")) redirect(url(controller='group', action='page', id=group.group_id))
def edit_post(self, id, category_id, thread_id): if self.can_manage_post(c.thread): c.thread.message = self.form_result['message'] meta.Session.commit() flash(_("Post updated.")) else: flash(_("Unable to edit post, probably because somebody has already replied to your post.")) redirect(url(controller=c.controller, action='thread', id=id, category_id=category_id, thread_id=c.thread.thread_id))
def fn(*args, **kwargs): if c.group is not None: if not c.group.forum_is_public and not check_crowds(['member', 'moderator']): deny("This forum is not public", 401) if c.group.mailinglist_enabled: flash(_('The web-based forum for this group has been disabled.' ' Please use the mailing list instead.')) redirect(url(controller='mailinglist', action='index', id=c.group_id)) return m(*args, **kwargs)
def confirm_emails(self): emails = request.POST.getall('email') for email in emails: email_confirmation_request(c.user, email) h.flash(_('Confirmation message sent. Please check your email.')) dest = request.POST.get('came_from', None) if dest is not None: redirect(dest.encode('utf-8')) else: redirect(url(controller='profile', action='edit_contacts'))
def recover_password(self): if not c.user.recovery_key: c.user.gen_recovery_key() email_password_reset(c.user) meta.Session.commit() h.flash(_('Password recovery email sent to %(user_email)s. ' 'Please check your inbox.') % { 'user_email': c.user.email.email }) redirect(url(controller='profile', action='login_settings'))
def update_logo(self, location): if hasattr(self, 'form_result'): logo = self.form_result['logo'] if logo is not None: location.logo = logo.file.read() meta.Session.commit() if 'js' not in request.params: h.flash(_("Logo successfully updated.")) if 'js' in request.params: return 'OK' redirect(location.url(action='edit'))
def update(self, location): if hasattr(self, 'form_result'): location.title = self.form_result['title'] location.title_short = self.form_result['title_short'] location.site_url = self.form_result['site_url'] location.teachers_url = self.form_result['teachers_url'] location.country = self.form_result['country'] location.description = self.form_result['description'] meta.Session.commit() h.flash(_("Information updated.")) redirect(location.url(action='edit'))
def confirm_user_email(self, key): try: email = meta.Session.query(Email).filter_by(confirmation_key=key).one() email.confirmed = True email.confirmation_key = '' meta.Session.commit() h.flash(_("Your email %s has been confirmed, thanks." % email.email)) except NoResultFound: h.flash(_("Could not confirm email: invalid confirmation key.")) redirect(url(controller='profile', action='home'))
def create_wiki(self): if not hasattr(self, 'form_result'): self._redirect() target = Subject.get_by_id(self.form_result['rcpt_wiki']) self._create_wiki_page( target, self.form_result['page_title'], self.form_result['page_content']) h.flash(_('Wiki page created.')) self._redirect()
def update_photo(self): if hasattr(self, 'form_result'): logo = self.form_result['logo'] if logo is not None: c.user.logo = logo.file.read() meta.Session.commit() if 'js' not in request.params: h.flash(_("Your photo successfully updated.")) if 'js' in request.params: return 'OK' redirect(url(controller='profile', action='edit'))