def __delitem__(self, item, flush=True): """Delete a value from the container using the key.""" if isinstance(item, basestring): item = self[item] if item.__parent_uri__ == self.__uri__: if isinstance(item, Container): for key in item.keys(): item.__delitem__(key, False) config.notify(events.ContentDeletingEvent(item)) name = item.__name__ if self._v_keys: self._v_keys.remove(name) if self._v_items and name in self._v_items: del self._v_items[name] if item in Session: try: Session.delete(item) if flush: Session.flush() except: pass return raise KeyError(item)
def register_handler(self): data, errors = self.extract() if errors: self.message(errors, 'form-error') return user = self.create(data) config.notify(PrincipalRegisteredEvent(user)) # validation if CROWD.validation: initiate_email_validation(user.email, user, self.request) self.message('Validation email has been sent.') if not CROWD['allow-unvalidated']: raise HTTPFound(location=self.request.application_url) # authenticate info = ptah.authService.authenticate( {'login': user.login, 'password': user.password}) if info.status: headers = security.remember(self.request, user.uri) raise HTTPFound( location='%s/login-success.html'%self.request.application_url, headers = headers) else: self.message(info.message) # pragma: no cover
def update(self, **data): if self.__type__: tinfo = self.__type__ for field in tinfo.fieldset.fields(): val = data.get(field.name, field.default) if val is not form.null: setattr(self, field.name, val) config.notify(events.ContentModifiedEvent(self))
def test_validation_registered_no_validation(self): from ptah.crowd.provider import CrowdUser user = CrowdUser('name', 'login', 'email') ptah.crowd.CONFIG['validation'] = False config.notify(ptah.events.PrincipalRegisteredEvent(user)) props = ptah.crowd.get_properties(user.uri) self.assertTrue(props.validated)
def __setitem__(self, key, item): """Set a new item in the container.""" if not isinstance(item, Content): raise ValueError("Content object is required") if item.__uri__ == self.__uri__: raise ValueError("Can't set to it self") parents = [p.__uri__ for p in load_parents(self)] if item.__uri__ in parents: raise TypeError("Can't itself to chidlren") if key in self.keys(): raise KeyError(key) if item.__parent_uri__ is None: event = events.ContentAddedEvent(item) else: event = events.ContentMovedEvent(item) item.__name__ = key item.__parent__ = self item.__parent_uri__ = self.__uri__ item.__path__ = '%s%s/'%(self.__path__, key) if item not in Session: Session.add(item) # temporary keys if not self._v_items: self._v_items = {key: item} else: self._v_items[key] = item if key not in self._v_keys: self._v_keys.append(key) # recursevly update children paths def update_path(container): path = container.__path__ for item in container.values(): item.__path__ = '%s%s/'%(path, item.__name__) if isinstance(item, Container): update_path(item) if isinstance(item, Container): update_path(item) config.notify(event)
def test_content_set_owner_on_create(self): import ptah, ptah.cms class MyContent(ptah.cms.Content): __mapper_args__ = {'polymorphic_identity': 'mycontent'} __uri_factory__ = ptah.UriFactory('mycontent') content = MyContent() config.notify(ptah.cms.ContentCreatedEvent(content)) self.assertEqual(content.__owner__, None) ptah.authService.set_userid('user') config.notify(ptah.cms.ContentCreatedEvent(content)) self.assertEqual(content.__owner__, 'user')
def test_content_events(self): import ptah.cms class MyContent(ptah.cms.Content): __mapper_args__ = {'polymorphic_identity': 'mycontent'} __uri_factory__ = ptah.UriFactory('mycontent') content = MyContent() config.notify(ptah.cms.ContentCreatedEvent(content)) self.assertTrue(isinstance(content.created, datetime)) self.assertTrue(isinstance(content.modified, datetime)) time.sleep(0.1) config.notify(ptah.cms.ContentModifiedEvent(content)) self.assertTrue(content.modified != content.created)
def test_content_update(self): import ptah, ptah.cms class MyContent(ptah.cms.Content): __type__ = ptah.cms.Type('mycontent', 'MyContent') content = MyContent() config.notify(ptah.cms.ContentCreatedEvent(content)) modified = content.modified time.sleep(0.1) content.update(title='Test title') info = content.info() self.assertEqual(info['title'], 'Test title') self.assertEqual(content.title, 'Test title') self.assertTrue(content.modified > modified)
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( ptah.events.PrincipalValidatedEvent(ptah.resolve(props.uri))) headers = remember(request, props.uri) raise HTTPFound(location=request.application_url, headers=headers) view.add_message(request, "Can't validate email address.", 'warning') raise HTTPFound(location=request.application_url)
def test_content_info(self): import ptah, ptah.cms class MyContent(ptah.cms.Content): __mapper_args__ = {'polymorphic_identity': 'mycontent'} __uri_factory__ = ptah.UriFactory('mycontent') content = MyContent() config.notify(ptah.cms.ContentCreatedEvent(content)) info = content.info() self.assertIn('__name__', info) self.assertIn('__type__', info) class MyContent(ptah.cms.Content): __type__ = ptah.cms.Type('mycontent', 'MyContent') content = MyContent() config.notify(ptah.cms.ContentCreatedEvent(content)) info = content.info() self.assertIn('title', info) self.assertIn('description', info)
def create(self, **data): content = self.cls(**data) config.notify(ContentCreatedEvent(content)) return content
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: page2 = Page(text='</p>page 2 html</p>') cms.Session.add(page2) foo['page2'] = page2 print 'page2', foo['page2'] # We can just use SQLAlchemy and manually notify application #if not cms.Session.query(Page).filter_by(__name__='page3').all(): if 'page3' not in foo: page3 = Page(text='<p>page 3 html</p>', __name__ = 'page3', __parent__ = foo) cms.Session.add(page3) config.notify(cms.events.ContentCreatedEvent(page3)) import transaction; transaction.commit() print 'See foo Application, http://localhost:8080/foo/' serve(app, '0.0.0.0')