Flask validates decorator multiple fields simultaneously - python

I have been using the #validates decorator in sqlalchemy.orm from flask to validate fields, and all has gone well as long as all of the fields are independent of one another such as:
#validates('field_one')
def validates_field_one(self, key, value):
#field one validation
#validates('field_two')
def validates_field_two(self, key, value):
#field two validation
However, now I need to do some validation that will require access to field_one and field_two simultaneously. It looks like validates accepts multiple arguments to the validates decorator, however, it will simply run the validation function once for each argument, as such:
#validates('field_one', 'field_two')
def validates_fields(self, keys, values):
#field validation
Results in a work flow of validate field_one and then validate field_two. However, I would like to validate both at the same time(a trivial example of which would be assert that the value of field_one is not the value of field_two, an example of which would be disallowing self-loops in a graph where field_one and field_two refer to nodes and it is performing validation on an edge). How would be the best way to go about doing that?

Order the fields in the order they were defined on the model. Then check if the last field is the one being validated. Otherwise just return the value unchecked. If the validator is validating one of the earlier fields, some of them will not be set yet.
#validates('field_one', 'field_two')
def validates_fields(self, key, value):
if key == 'field_two':
assert self.field_one != value
return value
See this example.

Adding another answer here, as the accepted one didn't quite meet my use case for using another field to validate and modify relationship/collection fields, which are not really compatible with #validates. In this case you can use an event listener for the before_flush event to achieve what you're looking for:
#event.listens_for(Session, 'before_flush')
def validate_and_modify_relationships(session, flush_context, instances):
"""
Complex validation that cannot be performed with #valdiates
"""
# new records only (for updates only, use session.dirty)
for instance in session.new:
if isinstance(instance, MyData):
if instance.some_other_value:
instance.some_relation = []
More details here: Flask-SQLAlchemy validation: prevent adding to relationship based on other field

Related

Django - NEVER update a column when saving

I am trying to use citus data (https://www.citusdata.com/) with Django.
Most everything is working so far, except trying to save a model that has already been saved:
NotSupportedError: modifying the partition value of rows is not allowed
This is because django always includes every single field in the update SQL, even if that field has not changed.
In Citus, you must pick a field to be your partitioning field, and then you cannot change it. So, when I'm saving an object, it doesn't like that the partition key is in the update statement, even if it didn't change.
I know that you can pass the update_fields keyword arg to the save method, but I'm wondering if I can somehow tell django to NEVER include a field when updating?
Django does not provide this functionality "out of the box". You could override the save method of your class to set all fields other than your partition field as the value for update_fields
def save(self, **kwargs):
kwargs.setdefault('update_fields', ['field1', 'field2'])
return super(Class, self).save(**kwargs)
A more dynamic option, if you do not want to update this method everytime you change the fields of your class, would be to use the Meta API to get all fields of the class and exclude your partition field
def save(self, **kwargs):
kwargs.setdefault(
'update_fields',
[f.name for f in self.__class__._meta.get_fields() if f.name != 'partition_field']
)
return super(Class, self).save(**kwargs)
There are several other methods by which Django will attempt to update your models. Maybe a base class that all your models inherit from that implement these methods would work

How to get the default field values that will be in the form beforehand in Odoo?

I have some form views similar to account.voucher.receipt.dialog.form which is in the file:
/addons_path/account_voucher/voucher_payment_receipt_view.xml.
Some field tags get their default values which were defined in the Model,
Some field tags get their default values from on change methods (defined by on_change attributes).
I want to bypass these form views and automate the process, so I need to know in advance these default field values.
In that way, I only need to add additional field values if needed, then call the create method on the model.
I'm using Odoo v8.
How can I achieve that?
If you want to print in the log all the default values of your model you can do this:
from inspect import isfunction
#api.multi
def get_default_fields(self):
for key, value in self._fields.iteritems():
if value.name not in models.MAGIC_COLUMNS:
if self._defaults.get(value.name, False):
if isfunction(self._defaults[value.name]):
_logger.debug(self._defaults[value.name](
self, self.env.cr, self.env.uid, None
))
else:
_logger.debug(self._defaults[value.name])
I think you can adapt this code to your needs.
And if you want get the value of one field assigned by an onchange method maybe you have to run the method manually

Check if a form is valid from within a form field [Django]

I have subclassed a text-field form field in Django to create my own custom widget for a field. I was wondering if it's possible to check if all other fields of the form are valid (I want its server side behavior to vary based on the validation of other fields)
See comment
Something like:
class CustomField(TextInput):
def __init__(self, *args, **kwargs):
...
super(CustomField, self).__init__(*args, **kwargs)
input_type = 'hidden'
def value_from_datadict(self, data, files, name):
aws_file_key = data.get(name, None)
_media_bucket = boto.connect_s3(settings.AWS_ACCESS_KEY_ID,
settings.AWS_SECRET_ACCESS_KEY)\
.lookup(settings.AWS_MEDIA_STORAGE_BUCKET_NAME)
try:
key = _media_bucket.get_key(aws_file_key)
except:
print 'Failed to get key.'
key = None
if key and aws_file_key:
fh = tempfile.TemporaryFile()
key.get_contents_to_file(fh)
fh.seek(0)
files = SimpleUploadedFile(key.name, fh.read())
### IF FORM IS VALID DELETE KEY, OTHERWISE, KEEP IT.
if code_to_check_if_valid:
_media_bucket.delete_key(key)
fh.close()
return files
...... etc.
If you want to validate a certain field depending on the values of other fields, you need to to it at the form level and overwrite the field's clean method. Here's the docs on the subject - they are very good.
class CustomForm(forms.Form):
custom_field = CustomField()
def clean(self):
cleaned_data = super(CustomForm, self).clean()
custom_field = cleaned_data.get("custom_field")
...
If you look at the flow of how forms are validated, you will see that the clean method is run if all the other fields validate independently, so at this stage, the form can be considered valid:
These methods are run in the order given above, one field at a time. That is, for each field in the form (in the order they are declared in the form definition), the Field.clean() method (or its override) is run, then clean_<fieldname>(). Finally, once those two methods are run for every field, the Form.clean() method, or its override, is executed.
The final clean method is actually run regardless of if there's an error so you have to iterate through the cleaned_data to make sure there are no errors
The clean() method for the Form class or subclass is always run. If that method raises a ValidationError, cleaned_data will be an empty dictionary.
The previous paragraph means that if you are overriding Form.clean(), you should iterate through self.cleaned_data.items(), possibly considering the _errors dictionary attribute on the form as well. In this way, you will already know which fields have passed their individual validation requirements.
The clean methods for individual fields are called in the same order as the form declaration order or explicitly specified order. [django source code]
Although I wouldn't recommend this approach over using the clean method for multi-field validation, if your custom field is the last field in the order, you can expect self._errors to indicate whether all other fields have passed validation or not. However at this stage, non-field errors won't be available.

Include Validation in Clean Fields -Django

As the built in validator in django accepts even if just spaces are input into a field. I want a validation in which if only spaces are fed then it must raise a validation error. I have a field like this:
name = forms.CharField(max_length=200)
Using validators outside i.e. writing a function like this works.
def validate_spaces(value):
if value.strip() == '':
raise ValidationError(u"You must provide more than just whitespace.")
But, I was thinking if it could be done using the form.clean() methods, i.e. without writing any extra functions outside. Any help will be much appreciated.
You can add custom behaviour to form.clean() this way:
class YourForm(forms.Form):
# Everything as before.
...
def clean_name(self):
data = self.cleaned_data['name']
if data.strip() == '':
raise forms.ValidationError(u"You must provide more than just whitespace.")
# Always return the cleaned data, whether you have changed it or
# not.
return data
However, if you want to create a type of field that automatically gets this type of validation, you add a new class like this
class NoSpacesCharField(forms.CharField):
def validate(self, value):
# Use the parent's handling of required fields, etc.
super(NoSpacesCharField, self).validate(value)
if value.strip() == '':
raise ValidationError(u"You must provide more than just whitespace.")
Then uses NoSpacesCharField like you would ordinarily use forms.CharField.
I'm not currently in a position to test this code so there might be the odd kink in it, but it should get you most of the way there. For further info on form validation in Django see https://docs.djangoproject.com/en/dev/ref/forms/validation/

Django custom (multi)widget input validation

What is the correct method for validating input for a custom multiwidget in each of these cases:
if I want to implement a custom Field?
if I want to use an existing database field type (say DateField)?
The motivation for this comes from the following two questions:
How do I use django's multi-widget?
Django subclassing multiwidget
I am specifically interested in the fact that I feel I have cheated. I have used value_from_datadict() like so:
def value_from_datadict(self, data, files, name):
datelist = [widget.value_from_datadict(data, files, name + '_%s' % i) for i, widget in enumerate(self.widgets)]
try:
D = date(day=int(datelist[0]), month=int(datelist[1]), year=int(datelist[2]))
return str(D)
except ValueError:
return None
Which looks through the POST dictionary and constructs a value for my widget (see linked questions). However, at the same time I've tacked on some validation; namely if the creation of D as a date object fails, I'm returning None which will fail in the is_valid() check.
My third question therefore is should I be doing this some other way? For this case, I do not want a custom field.
Thanks.
You validate your form fields just like any other fields, implementing the clean_fieldname method in your form. If your validation logic spreads across many form fields (which is nto the same as many widgets!) you put it in your form's clean() method.
http://docs.djangoproject.com/en/1.2/ref/forms/validation/
According to the documentation, validation is the responsibility of the field behind the widget, not the widget itself. Widgets should do nothing but present the input for the user and pass input data back to the field.
So, if you want to validate data that's been submitted, you should write a validator.
This is especially important with MultiWidgets, as you can have more than one aspect of the data error out. Each aspect needs to be returned to the user for consideration, and the built in way to do that is to write validators and place them in the validators attribute of the field.
Contrary to the documentation, you don't have to do this per form. You can, instead, extend one of the built in forms and add an entry to default_validators.
One more note: If you're going to implement a MultiWidget, your form is going to pass some sort of 'compressed' data back to it to render. The docs say:
This method takes a single “compressed” value from the field and returns a list of “decompressed” values. The input value can be assumed valid, but not necessarily non-empty.
-Widgets
Just make sure you're handling that output correctly and you'll be fine.

Categories