def test_view_route(self): view.register_route('test-route', '/test/') self._init_ptah() request_iface = config.registry.getUtility( IRouteRequest, name='test-route') self.assertIsNotNone(request_iface)
def test_view_route_global_view(self): view.register_route('test-route', '/test/', use_global_views=True) self._init_ptah() request_iface = config.registry.getUtility( IRouteRequest, name='test-route') self.assertTrue(request_iface.isOrExtends(IRequest))
def test_layout_for_route(self): view.register_route("test-route", "/test/", use_global_views=True) view.register_layout("test", route="test-route") self._init_ptah() layout = query_layout(self.request, Context(), "test") self.assertIsNone(layout) request_iface = config.registry.getUtility(IRouteRequest, name="test-route") interface.directlyProvides(self.request, request_iface) layout = query_layout(self.request, Context(), "test") self.assertIsNotNone(layout)
def test_layout_for_route(self): view.register_route('test-route', '/test/', use_global_views=True) view.register_layout('test', route = 'test-route') self._init_ptah() layout = query_layout(Context(), self.request, 'test') self.assertIsNone(layout) request_iface = config.registry.getUtility( IRouteRequest, name='test-route') interface.directlyProvides(self.request, request_iface) layout = query_layout(Context(), self.request, 'test') self.assertIsNotNone(layout)
def test_view_for_route(self): view.register_route('test-route', '/test/') @view.pview(route='test-route') def render(request): return '<html>Route view</html>' self._init_ptah() request_iface = config.registry.getUtility( IRouteRequest, name='test-route') interface.directlyProvides(self.request, request_iface) v = self._view('', None, self.request) self.assertEqual(v.body, '<html>Route view</html>')
""" A route and 2 views for Content """ import cgi from paste.httpserver import serve from ptah import view, cms view.register_route('show_models', '/show_models') @view.pview(route='show_models') def show_models(request): models = cms.Session.query(cms.Content).all() return cgi.escape(str(models)) @view.pview('show_info', context=cms.Content) def show_info(context, request): return cgi.escape(str(context.info())) @view.pview('list_children', context=cms.Container) def list_children(context, request): out = [] for name, child in context.items(): if isinstance(child, cms.Container): href = '<a href="%slist_children">%s</a>' #XXX extra /? href = href % (request.resource_url(child), child.title) else: href = '<a href="%sshow_info">%s</a>' href = href % (request.resource_url(child), child.title) out.append(href) return '<br />'.join(out)
actions = [(a.title, a.name, a.description) for a in srv.actions.values()] actions.sort() for title, name, description in actions: info['actions'].append( OrderedDict( (('name', name), ('link', '%s/%s'%(url, name)), ('title', title), ('description', description)))) return info view.register_route( 'ptah-rest-login', '/__rest__/login') view.register_route( 'ptah-rest', '/__rest__/{service}/*subpath', use_global_views=True) def dthandler(obj): return obj.isoformat() if isinstance(obj, datetime) else None class Login(object): view.pview(route='ptah-rest-login') def __init__(self, request): self.request = request
class ValidationTemplate(mail.MailTemplate): subject = 'Activate Your Account' template = view.template('ptah.crowd:templates/validate_email.txt') def update(self): super(ValidationTemplate, self).update() self.url = '%s/validateaccount.html?token=%s'%( self.request.application_url, self.token) principal = self.context self.to_address = mail.formataddr((principal.name, self.email)) view.register_route('ptah-principal-validate', '/validateaccount.html') @view.pview(route='ptah-principal-validate') def validate(request): """Validate account""" t = request.GET.get('token') data = token.service.get(t) if data is not None: props = query_properties(data) if props is not None: props.validated = True token.service.remove(t) view.add_message(request, "Account has been successfully validated.") config.notify(
from paste.httpserver import serve import sqlalchemy as sqla from pyramid.request import Request import ptah from ptah import cms, config, view FOO = cms.ApplicationFactory( '/foo', 'foo', 'Foo Application') view.register_route( 'foo', '/foo/*traverse', factory = FOO, use_global_views = True) if __name__ == '__main__': app = ptah.make_wsgi_app({'settings':r'./ptah.ini'}) request = Request.blank('/') # all pyramid requests have a root foo = FOO() request.root = foo request.registry = app.registry from ptah.cmsapp.content import Page # Only using the API all events will notify if 'page1' not in foo: page1 = foo.create(Page.__type__.__uri__, 'page1', text='<p> some html</p>') print'page1', foo['page1'], request.resource_url(page1) # We can use SQLAlchemy and Container API, which will notify if 'page2' not in foo:
""" user registration form """ from ptah import config, view, form from pyramid import security from pyramid.httpexceptions import HTTPFound, HTTPForbidden import ptah from ptah.password import PasswordSchema from ptah.events import PrincipalRegisteredEvent from settings import _, CROWD from schemas import RegistrationSchema from provider import Session, CrowdUser from validation import initiate_email_validation view.register_route('ptah-join', '/join.html') class Registration(form.Form): view.pview(route = 'ptah-join', layout='ptah-page') label = _("Registration") fields = form.Fieldset(RegistrationSchema, PasswordSchema) autocomplete = 'off' def update(self): uri = ptah.authService.get_userid() if uri is not None: raise HTTPFound(location = self.request.application_url) if not CROWD.join:
from ploud.frontend import decorators from ploud.frontend.config import CHOICES import utils, validators from config import PLOUD, LOGIN_TOKEN_TYPE from models import Session, Site, User, Host from root import PloudApplicationRoot def addVirtualHost(site, hostname): host = Host(site.id, hostname.lower()) Session.add(host) addVirtualHosts((str(hostname),), 'plone41') #view.register_route('frontend-dactions', '/actions.html') view.register_route('dactions-vtransfer', '/actions.html/validateTransfer') view.register_route('dactions-transfer', '/actions.html/transfer') view.register_route('dactions-remove', '/actions.html/remove') view.register_route('dactions-login', '/actions.html/login') view.register_route('frontend-dashboard', '/dashboard.html') #class DashboardActions(view.View): # view.pview('actions.html', PloudApplicationRoot, layout='page', # permission = decorators.require_object_login) @view.pview(route='dactions-vtransfer', permission = decorators.require_object_login) def validateTransfer_view(request): errors. data = validateTransfer(request)
""" login form """ from ptah import crowd, view, form from pyramid import security from pyramid.httpexceptions import HTTPFound import ptah from ptah.settings import MAIL from settings import _, CROWD view.register_route('ptah-login', '/login.html') view.register_route('ptah-logout', '/logout.html') view.register_route('ptah-login-success', '/login-success.html') view.register_route('ptah-login-suspended', '/login-suspended.html') class LoginForm(form.Form): view.pview( route='ptah-login', layout='ptah-page', template = view.template("ptah.crowd:templates/login.pt")) id = 'login-form' title = _('Login') fields = form.Fieldset( form.fields.TextField( 'login', title = _(u'Login Name'), description = _('Login names are case sensitive, '\ 'make sure the caps lock key is not enabled.'), default = u''),
def test_view_route_conflict(self): view.register_route('test-route', '/test/') view.register_route('test-route', '/test2/') self.assertRaises(config.ConflictError, self._init_ptah)
from ptah import view, config, form from ploud.utils import ploud_config from ploud.utils.policy import POLICIES from ploud.frontend import utils, validators from ploud.frontend.config import log, PLOUD, ALLOWED_SITE_NAME_CHARS from ploud.frontend.models import Session, User, Site from root import PloudApplicationRoot def lower(s): if isinstance(s, basestring): return s.lower() return s view.register_route('frontend-signup-validate', '/signup/validate') view.register_route('frontend-validate', '/validate') SignupSchema = form.Fieldset( form.TextField( 'signup-email', preparer = lower, validator = form.All(form.Email(), validators.checkEmail)), form.TextField( 'signup-site-name', preparer = lower, validator = validators.checkSitename), )
""" reset password form """ from datetime import datetime from ptah import config, form, view from pyramid import security from pyramid.httpexceptions import HTTPFound from ptah import mail from ptah import authService from ptah import passwordTool from ptah.password import PasswordSchema from ptah.events import ResetPasswordInitiatedEvent from ptah.events import PrincipalPasswordChangedEvent from settings import _ view.register_route('ptah-resetpassword', '/resetpassword.html') view.register_route('ptah-resetpassword-form', '/resetpassword.html/*subpath') class ResetPassword(form.Form): view.pview( route = 'ptah-resetpassword', layout='ptah-page', template = view.template('ptah.crowd:templates/resetpassword.pt')) fields = form.Fieldset( form.FieldFactory( 'text', 'login', title = _(u'Login Name'), description = _('Login names are not case sensitive.'), missing = u'',
import simplejson as json from datetime import datetime, timedelta from email.mime.text import MIMEText from sqlalchemy.sql import expression as expr from pyramid.httpexceptions import HTTPFound from pyramid.security import authenticated_userid, remember, forget from ptah import view, config from ptah.token import service as tokenService from ploud.frontend.models import Session, User from ploud.frontend.config import log, PLOUD, PASSWORD_RESET_TOKEN_TYPE view.register_route('frontend-login', '/login.html') view.register_route('frontend-login-validate', '/login.html/validate') view.register_route('frontend-logout', '/logout.html') view.register_route('frontend-resetpw', '/reset-password.html') view.register_route('frontend-resetpw-v','/reset-password.html/validate') view.register_route('frontend-changepw', '/change-password.html') @view.pview(route='frontend-login-validate') def validate_login_view(request): errors = validate_login(request) response = request.response response.content_type = 'text/json' response.body = json.dumps(errors) return response
""" membership """ from ptah import view from pyramid.security import authenticated_userid from models import User view.register_route('frontend-membership', '/membership.html') view.register_route('frontend-membership1', '/membership-free.html') view.register_route('frontend-membership2', '/membership-1.html') view.register_route('frontend-membership3', '/membership-2.html') class MembershipView(view.View): view.pview(route='frontend-membership', layout='page', template = view.template('newui/membership.pt')) policy = 0 def update(self): uri = authenticated_userid(self.request) user = User.getByURI(uri) info = {'email': user.email, 'policy': getattr(user, 'type', 0)} self.policy = info['policy'] self.user = user self.isAnon = user is None return info view.register_view(
@view.pview("", IPloudApplicationRoot) def default(request): raise HTTPFound(location="/index.html") @view.pview(route="frontend-home", layout="page", template=view.template("newui/homepage.pt")) def homepage(request): return {"isanon": 1 if authenticated_userid(request) else 0} @view.pview(route="frontend-themes") def themes(request): raise HTTPFound(location="/themes/") view.register_route("frontend-home", "/index.html") view.register_route("frontend-favicon", "/favicon.ico") view.register_route("frontend-robots", "/robots.txt") view.register_route("frontend-policy", "/privacy-policy.html") view.register_route("frontend-toc", "/terms-of-service.html") view.register_route("frontend-disabled", "/disabled.html") view.register_route("frontend-404", "/404.html") view.register_route("frontend-themes", "/themes") view.register_view(route="frontend-policy", layout="page", template=view.template("newui/privacy-policy.pt")) view.register_view(route="frontend-toc", layout="page", template=view.template("newui/terms-of-service.pt")) view.register_view(route="frontend-disabled", layout="page", template=view.template("newui/disabled_site.pt"))
if not ACCESS_MANAGER(userid): raise HTTPForbidden() def __getitem__(self, key): if key not in PTAH_CONFIG.disable_modules: mod = config.registry.storage[MANAGE_ID].get(key) if mod is not None: return mod(self, self.request) raise KeyError(key) view.register_route( 'ptah-manage-view','/ptah-manage', PtahManageRoute, use_global_views=True) view.register_route( 'ptah-manage','/ptah-manage/*traverse', PtahManageRoute, use_global_views=True) view.snippettype('ptah-module-actions', PtahModule) view.register_snippet( 'ptah-module-actions', PtahModule, template = view.template('ptah.manage:templates/moduleactions.pt')) view.register_layout( '', PtahManageRoute, parent='ptah-manage', template=view.template("ptah.manage:templates/ptah-layout.pt"))
""" reset password form """ from datetime import datetime from ptah import config, form, view from pyramid import security from pyramid.httpexceptions import HTTPFound from ptah import mail from ptah import authService from ptah import passwordTool from ptah.password import PasswordSchema from ptah.events import ResetPasswordInitiatedEvent from ptah.events import PrincipalPasswordChangedEvent from settings import _ view.register_route("ptah-resetpassword", "/resetpassword.html") view.register_route("ptah-resetpassword-form", "/resetpasswordform.html") class ResetPassword(form.Form): view.pview( route="ptah-resetpassword", layout="ptah-page", template=view.template("ptah.crowd:templates/resetpassword.pt") ) csrf = True fields = form.Fieldset( form.FieldFactory( "text", "login", title=_(u"Login Name"), description=_("Login names are not case sensitive."),