- Joined
- Jan 24, 2006
- Location
- South Dakota
I am hoping someone can help me out with some Django stuff. By default there are a few things in Django's stdlib Storage that are causing me grief. The main one is that it is stripping anything that is not alpha-numeric. Specifically there is a text.py which has a get_valid_filename(s) method which I believe is the one doing this function... it reads as follows:
When I went to the #django IRC, they told me the best way to handle this is to write a custom storage see django docs. However I am not sure how to implement this. I have subclassed/overridden my own classes/methods but I dont really know how to do this with stdlib.
Do I have to reimplement everything (i.e. de-construct what calls are made one and redirect them to my override)?
Can anyone help me out?
EDIT: I have confirmed if I actually change this
To this
I get the expected result, but I know that editing the stdlib isnt the portable way of doing things
Code:
>>> def get_valid_filename(s):
... """
... Returns the given string converted to a string that can be used for a clean
... filename. Specifically, leading and trailing spaces are removed; other
... spaces are converted to underscores; and anything that is not a unicode
... alphanumeric, dash, underscore, or dot, is removed.
... >>> get_valid_filename("john's portrait in 2004.jpg")
... 'johns_portrait_in_2004.jpg'
... """
... s = force_text(s).strip().replace(' ', '_')
... return re.sub(r'(?u)[^-\w.]', '', s)
When I went to the #django IRC, they told me the best way to handle this is to write a custom storage see django docs. However I am not sure how to implement this. I have subclassed/overridden my own classes/methods but I dont really know how to do this with stdlib.
Do I have to reimplement everything (i.e. de-construct what calls are made one and redirect them to my override)?
Can anyone help me out?
EDIT: I have confirmed if I actually change this
Code:
return re.sub(r'(?u)[^-\w.]', '', s)
To this
Code:
return(s)
I get the expected result, but I know that editing the stdlib isnt the portable way of doing things
Last edited: