Flask handle form array

To get the POST data on the Flask side you need to include the square brackets in the field name. You can retrieve all values of the list using MultiDict.getlist():

hello = request.form.getlist('hello[]')

You don’t have to use the [] convention at all, of course. Not appending the [] to the helloname will work perfectly fine, at which point you’d use request.form.getlist('hello') in Flask.

Advertisements

Multer upload with extension

var storage = multer.diskStorage({
  destination: function (req, file, cb) {
    cb(null, './uploads/')
  },
  filename: function (req, file, cb) {
    crypto.pseudoRandomBytes(16, function (err, raw) {
      cb(null, raw.toString('hex') + Date.now() + '.' + mime.extension(file.mimetype));
    });
  }
});
var upload = multer({ storage: storage });

Apache2 Proxy Forwarding with SSL

<VirtualHost *:80>
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
</VirtualHost>

<VirtualHost *:443>
ServerAdmin phakin.che@gmail.com
ServerName phakin.com

ErrorLog ${APACHE_LOG_DIR}/star_error.log
CustomLog ${APACHE_LOG_DIR}/star_access.log combined

SSLEngine on
SSLCertificateFile /home/ubuntu/Systems/Cert/star.crt
SSLCertificateKeyFile /home/ubuntu/Systems/Cert/star.key
SSLCertificateChainFile /home/ubuntu/Systems/Cert/star.ca-bundle

ProxyRequests off

<Proxy *>
Order deny,allow
Allow from all
</Proxy>

<Location />
ProxyPass http://localhost:5000/
ProxyPassReverse http://localhost:5000/
</Location>
</VirtualHost>

Undelete a BigQuery table

Undelete in BigQuery is possible via table copy and snapshot decorators. That is, you can copy a snapshot of the table from before the table was deleted.

There are a couple of caveats:

  1. You must do this in two steps; first, copy a snapshot of the table to a second table. Then you can copy that second table back to the original location.
  2. You can only undelete tables that were deleted within the last 2 days.
  3. If you have since recreated a table with the same name, you may not be able to undelete the old one.

Here is an example using bq, but you can do the same thing with the BigQuery Web UI.

First let’s create a dummy bigquery dataset and table that we’re going to delete:

$ bq mk -d dataset1
Dataset 'helixdata2:dataset1' successfully created.
$ bq query --destination_table=dataset1.table1 "SELECT 17 as a"
Waiting on bqjob_ra0dedbee5cb4228_0000014a5af133d6_1 ... (0s) 
Current status: DONE   
+----+
| a  |
+----+
| 17 |
+----+

Now, grab the current unix timestamp from a time when the table was alive.

$ date +%s
1418864998

Note that this time is in seconds, we’ll need miliseconds.

Remove the table ‘accidentally’

$ bq rm dataset1.table1
rm: remove table 'helixdata2:dataset1.table1'? (y/N) y

Now we can undelete the table by copying a snapshot:

$ bq cp dataset1.table1@1418864998000 dataset1.temp
Waiting on bqjob_r4d8174e2e41ae73_0000014a5af2a028_1 ... (0s) 
    Current status: DONE    
Tables 'helixdata2:dataset1.table1@1418864998000' successfully copied to     
    'helixdata2:dataset1.temp'

(note we multiplied the time by 1000 since we want milliseconds) This copied an old snapshot of the table to dataset1.temp. Let’s copy it back to the old location and then remove the temp table.

$ bq cp dataset1.temp dataset1.table1
Waiting on bqjob_r3c0bb9302fb81d59_0000014a5af2dc7b_1 ... (0s) 
    Current status: DONE    
Tables 'helixdata2:dataset1.temp' successfully copied to 
    'helixdata2:dataset1.table1'
$ bq rm dataset1.temp
rm: remove table 'helixdata2:dataset1.temp'? (y/N) y

Now let’s verify that the table has been restored:

$ bq query "select * from dataset1.table1"
Waiting on bqjob_r5967bea49ed9e97f_0000014a5af34dec_1 ... (0s) 
    Current status: DONE   
+----+
| a  |
+----+
| 17 |
+----+

 

PS> You might have to run this from Cloud Shell.