Exemplo n.º 1
0
 def _clean(self, value, bfield):
     app = bfield.request.app
     # Get a reference to the object data mapper
     odm = app.odm()
     model = self.model(app)
     db_model = model.db_model()
     # TODO: this works but it is not general
     # pkname = db_model.__mapper__.primary_key[0].key
     if not self.multiple:
         value = (value, )
     idcolumn = getattr(db_model, model.id_field)
     try:
         with odm.begin() as session:
             all = session.query(db_model).filter(idcolumn.in_(value))
             if self.multiple:
                 return list(all)
             else:
                 if all.count() == 1:
                     instance = all.one()
                     if self.get_field:
                         return getattr(instance, self.get_field)
                     else:
                         return instance
                 else:
                     raise forms.ValidationError(
                         self.validation_error.format(model))
     except forms.ValidationError:
         raise
     except Exception as exc:
         app.logger.exception(str(exc))
         raise forms.ValidationError(self.validation_error.format(model))
Exemplo n.º 2
0
 def clean_layout(self, value):
     if not isinstance(value, dict):
         raise forms.ValidationError('Layout must be a dictionary')
     layout = {}
     if 'components' in value:
         components = value['components']
         if not isinstance(components, list):
             raise forms.ValidationError('componets must be a list')
         layout['components'] = components
     if 'rows' in value:
         rows = value['rows']
         if not isinstance(rows, list):
             raise forms.ValidationError('rows must be a list')
         layout['rows'] = rows
     return layout
Exemplo n.º 3
0
 def clean(self):
     '''Process login'''
     request = self.request
     permissions = request.app.permissions
     username = self.cleaned_data['username']
     user = yield permissions.get_user(request, username=username)
     if not user:
         raise forms.ValidationError(self.error_message)
     password = self.cleaned_data['password']
     try:
         user = yield permissions.authenticate_and_login(request,
                                                         user,
                                                         password=password)
     except AuthenticationError:
         raise forms.ValidationError(self.error_message)
     except LoginError as e:
         raise forms.ValidationError(str(e))
     coroutine_return(user)
Exemplo n.º 4
0
 def _clean(self, value, bfield):
     try:
         request = bfield.request
         model = request.app.models.get(self.model)
         kwargs = model.model_url_params(request, value)
         if self.multiple:
             return model.get_list(request, **kwargs)
         else:
             try:
                 return model.get_instance(request, **kwargs)
             except Http404:
                 raise forms.ValidationError(
                     self.validation_error.format(model))
     except forms.ValidationError:
         raise
     except Exception:
         request.logger.exception('Critical exception while validating')
         raise forms.ValidationError(self.validation_error.format(model))
Exemplo n.º 5
0
    def __call__(self, value, bfield):
        model_name = self.model or bfield.form.model
        field = self.field or bfield.name
        if not model_name:
            raise forms.ValidationError('No model')

        if not value and self.nullable:
            return value

        request = bfield.request
        app = request.app
        model = app.models.get(model_name)

        if not model:
            raise forms.ValidationError('No model %s' % model_name)

        kwargs = {field: value}
        kwargs.update(model.model_url_params(request))
        return self.test(value, bfield, model, **kwargs)
Exemplo n.º 6
0
 def clean_old_password(self, value):
     request = self.request
     user = request.cache.user
     auth_backend = request.cache.auth_backend
     try:
         if user.is_authenticated():
             auth_backend.authenticate(request, user=user, password=value)
         else:
             raise AuthenticationError('not authenticated')
     except AuthenticationError as exc:
         raise forms.ValidationError(str(exc))
     return value
Exemplo n.º 7
0
 def test(self, value, bfield, model, **kwargs):
     request = bfield.request
     previous_state = bfield.form.previous_state
     try:
         instance = model.get_instance(request, **kwargs)
     except Http404:
         pass
     else:
         if instance != previous_state:
             raise forms.ValidationError(
                 self.validation_error.format(value))
     return value
Exemplo n.º 8
0
 def clean(self):
     user = self.request.cache.user
     model = self.request.app.models['mailinglist']
     topic = self.cleaned_data['topic']
     with model.session(self.request) as session:
         query = model.get_query(session)
         if user.is_anonymous():
             if not user.is_authenticated():
                 raise Http401('Token')
             query.filter(email=self.cleaned_data['email'], topic=topic)
         else:
             self.cleaned_data['user'] = user
             query.filter(user=user, topic=topic)
         try:
             query.one()
         except Http404:
             pass
         else:
             raise forms.ValidationError('Already subscribed')
Exemplo n.º 9
0
 def clean_body(self):
     request = self.request
     key = request.app.config.get('AKISMET_API_KEY')
     body = self.cleaned_data.get('body')
     if body and key:
         akismet_api = Akismet(key=key,
                               blog_url='http://%s/' % request.get_host())
         if akismet_api.verify_key():
             akismet_data = {
                 'comment_type': 'comment',
                 'referer': request.get('HTTP_REFERER', ''),
                 'user_ip': request.get('REMOTE_ADDR', ''),
                 'user_agent': request.get('HTTP_USER_AGENT', '')
             }
             if akismet_api.comment_check(body,
                                          data=akismet_data,
                                          build_data=True):
                 raise forms.ValidationError(
                     "Akismet thinks this message is spam")
     return body
Exemplo n.º 10
0
 def __call__(self, value, bfield):
     model = self.model or bfield.form.model
     field = self.field or bfield.name
     previous_state = bfield.form.previous_state
     assert model, 'model not available'
     assert field, 'field not available'
     app = bfield.request.app
     # Get a reference to the object data mapper
     odm = app.odm()
     model = odm[model]
     pkey = inspect(model).primary_key
     with odm.begin() as session:
         q = session.query(model).filter_by(**{field: value})
         if previous_state:
             for pkey_col in pkey:
                 q = q.filter(
                     pkey_col != getattr(previous_state, pkey_col.name))
         if q.count():
             raise forms.ValidationError(
                 self.validation_error.format(value))
         return value
Exemplo n.º 11
0
 def clean(self):
     password = self.cleaned_data['password']
     password_repeat = self.cleaned_data['password_repeat']
     if password != password_repeat:
         raise forms.ValidationError('Passwords did not match')
Exemplo n.º 12
0
 def clean_username(self, value):
     request = self.request
     if request.config['CHECK_USERNAME'](request, value):
         return value
     else:
         raise forms.ValidationError('Username not available')
Exemplo n.º 13
0
 def clean(self):
     raise forms.ValidationError('wrong data')
Exemplo n.º 14
0
 def clean_email(self, email):
     user = self.request.cache.user
     if not email or user.email:
         raise forms.ValidationError('required')
     return email