-
Notifications
You must be signed in to change notification settings - Fork 28
Generic assignment (Auto Coercion) #98
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Thanks for the nice description, and sorry for late. I'll check it asap. |
Please accept my apologies because of the late. Yes, you are right, but the In the At the other side, I think the idea of setting the So, I'll add some tests inside a new branch named After reading this, I found the title: Many thanks for the contribution. |
No need to apologize, really! Thanks a lot for this handy add-on! But I'm not sure I understand what you are suggesting: are you going to tweak the |
Currently, I'm working on that to get rid of this situation. After fixing this issue, These lines should be working: person1.cv = filelike
person1.cv = filelike, mimetype
person1.cv = filelike, mimetype, filename Any suggestion? |
Please take a look at this test case: class AutoCoerceFile(File):
__auto_coercion__ = True
class AutoCoercionTestCase(TempStoreTestCase):
def test_file_assignment(self):
class Person(self.Base):
__tablename__ = 'person'
id = Column(Integer, primary_key=True)
cv = Column(AutoCoerceFile.as_mutable(Json))
session = self.create_all_and_get_session()
person1 = Person()
resume = io.BytesIO(b'This is my resume')
with StoreManager(session):
person1.cv = resume
self.assertIsNone(person1.cv.content_type)
self.assertIsNone(person1.cv.extension)
self.assertTrue(exists(join(self.temp_path, person1.cv.path)))
person1.cv = resume, 'text/plain'
self.assertEqual(person1.cv.content_type, 'text/plain')
self.assertEqual(person1.cv.extension, '.txt')
self.assertTrue(exists(join(self.temp_path, person1.cv.path)))
person1.cv = resume, 'text/plain', 'myfile.note'
self.assertEqual(person1.cv.content_type, 'text/plain')
self.assertEqual(person1.cv.extension, '.note')
self.assertTrue(exists(join(self.temp_path, person1.cv.path The |
Great, thank you! I cannot try it out immediately, but of course I will do it as soon as possible. |
Pending on #101 |
First of all I'm sorry about the title, couldn't figure out a better phrase... second, sorry again for the length of this!
My use case is the following: I have different SA classes containing different Image kinds (that is, with different constraints such as max_length or pre_processors), for example:
I have then a generic function that, given the class of an entity and basically a dictionary coming from a data entry form, creates a new instance and initialize its content, greatly simplified as this:
For the
image
attribute, thevalue
is actually acgi.FieldStorage
dictionary, and thus I have to handle it in a special way, and here are my two problems:Image
class (i.e. given aTool
find out that itsimage
column is actually anIconImage
), so that I could write:This is because the actual class is buried very deep in the SA Mutable machinery, and thus
class_.image.property.expression
gives youJson
, notIconImage
...to delegate the logic to the
Attachment.coerce()
class method, but that fails too, due to the way it forwards the argument toAttachment.create_from()
: IMHO it should handle the case whenvalue
is a tuple, and when that's the case callcls.create_from(*value)
... and indeed the following monkey patch seems to satisfy my needs:What do you think? Am I either missing something or abusing the API, or is there a better way to accomplish the above?
Thank you in advance for any hint!
The text was updated successfully, but these errors were encountered: