Modify Filename Before Saving in Django using model.FileField - django-models

I have a model.FileField(upload_to='%Y/%m/%d') field. This works great; however, I want to rename the file based on the context of the user uploading the file before it is saved. Is there a way of modifying the request object before it saved to accomplish this?
I have come across people with similar issues but the answers always point back to the Django documentation. I have tried figuring this out using the documentation but can't. Can someone provide some code to show hot to solve this?
Thanks in advance.

you can use function for upload_to value with instance and filename inputs and return path and file name
for example:
def upload_to_func(instance, filename):
now = datetime.now()
return os.path.join(str(now.year), str(now.month), str(now.day), filename)
field_x = model.FileField(upload_to=upload_to_func)
you can change path and filename in function

Related

How to store a variable in an array in a different file

What I've done/tried so far:
I've created a file called links.js which contains a single array.
I've got my main file (index.js) which you run and it scrapes URLs;
I need to save the URL in links.js in the format I created so I can run the second part of my program which requires links.js in a specific format and uses the URLs gathered from my index.js
I've tried fs and creating streams etc but I'm still learning nodejs so I don't really understand it and can't find an issue on stack similar enough to figure out what to do..
Overview of format required for the links.js file expanding the url array each time with another url from index.js:
This is the links.js array format I need it in as an example
url = ['url1.com','url2.com','url3.com'];
module.exports{url:url};
How would I code my index.js so it stores my variable in the format above in links.js (a different file from the one I'm in)?
my index.js currently stores the url in test2[1]
I'm able to verify that's what I want to store by doing console.log(test2[1]);
Sorry if that's hard to understand I'm not sure how to explain it much better but if you're confused about something let me know and I'll try and reword it!
Thanks for any help :)
Expectations/Desired Outcomes:
I expect test2[1] to return http://example.com on index.js
I expext index.js to then save test2[1] and put it in links.js but keeping the format of links.js as explained and shown above.
I expect it to not be overwritten as index uses pupeteer and loops through to get website URLs and each would need to be saved all in the single links.js folder just expanding the arrays size.
You can save it as a json file.
const fs = require('fs')
const file = "./urls.json"
const urls = require(file )
//Here you update urls array
urls.push("url4.com")
fs.writeFileSync(file, JSON.stringify(urls))
urls.json
["url1.com","url2.com","url3.com", "url4.com"]

drupal 7 how to use drupal_goto for file

I did some custom scripting in a custom module. After my scripting I would like to go to the file path of a file field.
How can I achive this?
I have the uri of the file to display it.
I tried
drupal_goto('$result')
where $result is result of query with uri field of file.
Although
drupal_goto($result) does not work.
Any suggestions??
drupal_goto(file_create_url($result));
The uri contains public://
If this doesnt work, print the result of file_create_url($result) and copy past it in your browser see if the file is really there. By the way, you should avoid querying the database just for one file and use a file_load() to avoid bad surprises.
Thanks this realy helped me in the good direction...
Now I used this
$file = file_load($fid);
$uri = $file->uri;
$url = file_create_url($uri);
drupal_goto($url);
Hope this will help someone :-)

Get values from an array. JMeter

I have values in file:
en-us, de-de, es-es, cs-cz, fr-fr, it-it, ja-jp, ko-kr, pl-pl, pt-br, ru-ru, tr-tr, zh-cn, zh-tw.
how can I get this values for one request?
I want to create a query that takes the value of these in turn and writes the variable
This scenario can be achieved using Jmeter component "CSV Data Set Config"
Please refer to below mentioned link:
Jmeter CSV Data Set Config
Hope this will help
Can't comment, not enough karma. In response to above questions your path is probably wrong. If you use a debug sampler to show what path the CSV reader is taking I think you will find it is looking at something like C:/Jmeter/C:/path/to/CSV/file.
Another option for completing this is to use inline CSVRead. In your HTTP request use code like this -
${__CSVRead(etc/filters.csv,0)}${__CSVRead(etc/filters.csv,next)}
etc/filters is the RELATIVE path from Jmeters active running directory. In my case this evaluates to
C:/git/JmeterScripts/etc/filters.csv
In either case, I am sure your problem is that Jmeters active running directory is not what you think it is. I have had this problem several times with the same error.

Getting a URL with GtkClipboard

I'm trying to check if a url is on the system clipboard and if so get it from the clipboard. While reading the GTK API docs I came across gtk_clipboard_wait_for_uris but it seems to always return NULL:
g_print("%s", gtk_clipboard_wait_for_uris(gtk_clipboard_get(GDK_SELECTION_PRIMARY)));
What would be the correct/best way of getting a url from the clipboard?
wait_for_uris only works if the clipboard contains data tagged as a URI list (text/uri-list), e.g. when you perform a copy action in a file manager. It won't work as you expect if you copy a piece of text that just happens to contain a URI, e.g. "http://stackoverflow.com/". When you copy that string, it's most likely tagged as plaintext (text/plain).
The solution is to use wait_for_text and check whether it's a URI.

Receive multi file post with google app engine

I want to receive multi file post from image uploader.(i use this)
Most examples show how to receive one image from post.
I tried many ways but never got the results.
For example
self.request.POST['Filename']
gives only first filename.
What to do when there are multiple files/images in post?
The reason for this is to resize before upload images, that are too big for google app engine
to upload.
EDIT:
self.request.POST.multi.__dict__
shows
{'_items':
[('Filename', 'camila1.jpg'),
('Filedata[]', FieldStorage('Filedata[]', 'camila1.jpg')),
('Upload', 'Submit Query\r\n--negpwjpcenudkacqrxpleuuubfqqftwm----negpwjpcenudkacqrxpleuuubfqqftwm\r\nContent-Disposition: form-data; name="Filename"\r\n\r\nbornToBeWild1.jpg'),
('Filedata[]', FieldStorage('Filedata[]', 'bornToBeWild1.jpg')),
('Upload', 'Submit Query')]}
Your flash uploader is designed to work with PHP and sends multiple Filedata[] fields (php interprets this as an array for easy access)
So you need to iterate and get them all:
def post(self):
for file_data in self.request.POST.getall('Filedata[]'):
logging.info(file_data.filename)
data should be file_data.value
Are you using the Django libraries available to you? If so, check this out.
Call self.request.POST.getall('Filename') to get a list of FieldStorage objects; each one contains one file. You can access the file data with .value, the name with .name, and the mimetype with .type.
I have no idea how that multi uploader works, I have made one in the past however and I just added a number on the end of input field name, then hide it. Then add a new file input field to add another file. The reason for this is that they don't let you play around with input file fields to much because you could make it upload files they didn't want you uploading.
Using my conventions, in your example the 2 files in your example would be "Filename0" and "Filename1". You could also use firebug to see what it renaming the input file fields to.
Edit: I had a look, and it's using flash. So i have no idea how it works.

Resources