0

I created a Flask app, in my local computer everything works fine, I can insert data from the form on the HTML page and then pass the data to the program to elaborate it.

I wanted to move this application on an Ubuntu server, I set up all the stuff like nginx, python, flask, uswgi using this guide. The web server works, I can access the web pages and insert all of my data, but when the program passes the data to another page is not sorted correctly, maybe the third field is in the second position and so on.

I don't understand why this is not happening on my local machine.

here an example:

ImmutableMultiDict
([('group-0[1][macchine]', 'M02'), ('group-0[1][tempo_previsto_setup]', '17'),  ('group-0[0][tempo_previsto_setup]', '13'), 
('group-0[0][macchine]', 'M01'),  ('group-0[0][tempo_previsto_lavorazione]' 
('group-0[2][macchine]', 'M03'),  ('group-0[2][tempo_previsto_lavorazione]', '22'), ('group-0[0][tipo_lavorazione]', '')

this are just few fields but the list is way longer, as you can see the M01 is in the second position, but in the web page is in the first field.

basically each group-0[1][macchine] should have his group-0[1][tempo_previsto_setup]', 'group-0[0][tempo_previsto_lavorazione]' but as you can see the M02 has two 'group-0[0][tempo_previsto_lavorazione] one with the [1](that is correct) and one with [0]that should be owned by the M01

this is causing me a lot of problems because the program was written to get the data sorted correctly

What can be wrong with nginx?


I'm getting the ImmutableMultiDict like so:

@app.route('/ordini/conferma', methods=['GET', 'POST'])
def conferma_ordine():
    global data

    if request.method == 'POST':
        data = request.form
        print(data)
        Calcolo_tempistiche.ottenimento_variabili(data)
        array_ordine = Calcolo_tempistiche.array_ordine
        return render_template('ordine_eseguito.html', **locals())

    data = []
Arghya Saha
  • 5,599
  • 4
  • 26
  • 48
alessandro buffoli
  • 658
  • 2
  • 10
  • 29
  • are `group-0[1][macchine]` etc. the keys of the `ImmutableMultiDict`? – Nils Werner Aug 28 '18 at 10:47
  • @NilsWerner the `group-0[number]` are keys autocreated because in that page is it possible to create textboxes clicking on the button add, so that `group-0[number]` are just the name for each textbox, what I really need is to sort are the `M01`, `M02`.. – alessandro buffoli Aug 28 '18 at 10:52

2 Answers2

1

There is nothing wrong with nginx. Basically you are using ImmutableMultiDict and the order is not maintained for such datatypes. If you want a particular order of data then you should use ImmutableOrderedMultiDict to store your data and send it in response.

UPDATE:

Disclaimer: I'm not well versed with flask and my solution would be a hack, which I don't recommend to use in production without getting it verified from someone who knows flask very well.

As per the code you have written to change the ImmutableMultiDict format to ImmutableOrderedMultiDict. First you need to convert it into normal dictionary, use this answer to follow.

imd = ImmutableMultiDict([('default', u''), ('required', u'on'), ('name', u'short_text'), ('name', u'another')])
x = imd.to_dict(flat=False)

Now as per the order you need the data, you should add it.

new_imd = ImmutableOrderedMultiDict([('default', x['default'][0]), ('required', x['required'][0]), ('name', x['name'][0]), ('name', x['name'][1])])
Arghya Saha
  • 5,599
  • 4
  • 26
  • 48
  • thanks for your reply, but i can't find any right explanation of how to use it, can you give me an example? – alessandro buffoli Aug 29 '18 at 09:17
  • @alessandrobuffoli could you share the flask code where you are creating it? – Arghya Saha Aug 29 '18 at 10:49
  • @alessandrobuffoli Sorry man. I'm don't have idea about flask in that depth. So can you tell me the exact line and the variable which needs to be converted? – Arghya Saha Aug 29 '18 at 14:17
  • I'm passing the variables from an html page to flask, I'm getting all the data with `data = request.form` and then I pass the data to my program using `locals()` – alessandro buffoli Aug 29 '18 at 14:46
  • 1
    @alessandrobuffoli I have updated the code, but I would recommend you to check if there is any alternate way. Also your logic should never be depending on the the order of the dictionary, this is something which cannot be controlled, even when using `ImmutableOrderedMultiDict` you are manually creating the new order which depends on the random order which you got. So I don't think you can generalise it. You should try and use different keys, than single key for multiple values – Arghya Saha Aug 30 '18 at 06:42
  • thankyou for your reply, yesterday night infact I wrote a cycle to order every variable, now it works just fine! – alessandro buffoli Aug 30 '18 at 06:46
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/179085/discussion-between-argo-and-alessandro-buffoli). – Arghya Saha Aug 30 '18 at 06:47
0

I didn't find the solution for my question but I did find a way to order every variable.

the complete list of varibles was : ImmutableMultiDict([('id_ordine', '1650958108523345 '), ('data_start', '2018-08-06'), ('group-a[0][id_prodotto]', 'Frutta'), ('group-a[0][quantita]', '2'), ('group-0[0][macchine]', 'M01'), ('group-0[0][tipo_lavorazione]', ''), ('group-0[0][tempo_previsto_checkup]', '12'), ('group-0[0][tempo_previsto_setup]', '12'), ('group-0[0][tempo_previsto_lavorazione]', '14'), ('group-0[0][nota]', ''), ('group-0[1][macchine]', 'M02'), ('group-0[1][tipo_lavorazione]', ''), ('group-0[1][tempo_previsto_checkup]', '13'), ('group-0[1][tempo_previsto_setup]', '14'), ('group-0[1][tempo_previsto_lavorazione]', '15'), ('group-0[1][nota]', ''), ('group-a[1][id_prodotto]', 'verdura'), ('group-a[1][quantita]', '2'), ('group-1[0][macchine]', 'M03'), ('group-1[0][tipo_lavorazione]', ''), ('group-1[0][tempo_previsto_checkup]', '14'), ('group-1[0][tempo_previsto_setup]', '15'), ('group-1[0][tempo_previsto_lavorazione]', '16'), ('group-1[0][nota]', '')])

I'll write the code here to order that list, maybe it could be helpfull to someone:

num_pr = 0

for pr in range(len(variabili)):
    campo = []

    for pr in variabili:
        prodotto = []
        if pr == "group-a[" + str(num_pr) + "][id_prodotto]":
            campo = [variabili["group-a[" + str(num_pr) + "][id_prodotto]"]]
            prodotto.append(campo)

            if "group-a[" + str(num_pr) + "][quantita]" in variabili:
                campo = [variabili["group-a[" + str(num_pr) + "][quantita]"]]
                prodotto.append(campo)

                num_mc = 0
                for _ in range(len(variabili)):

                    for _ in range(len(variabili)):
                        if 'group-'+ str(num_pr) +'[' + str(num_mc) + '][macchine]' in variabili:
                            campo = [variabili['group-'+ str(num_pr) +'[' + str(num_mc) + '][macchine]']]

                            for tmp in variabili:
                                if 'group-'+ str(num_pr) +'[' + str(num_mc) + '][tempo_previsto_checkup]' in variabili:
                                    campo.append(variabili['group-'+ str(num_pr) +'[' + str(num_mc) + '][tempo_previsto_checkup]'])

                                    if 'group-'+ str(num_pr) +'[' + str(num_mc) + '][tempo_previsto_setup]' in variabili:
                                        campo.append(variabili['group-'+ str(num_pr) +'[' + str(num_mc) + '][tempo_previsto_setup]'])

                                        if 'group-'+ str(num_pr) +'[' + str(num_mc) + '][tempo_previsto_lavorazione]' in variabili:
                                            campo.append(variabili['group-'+ str(num_pr) +'[' + str(num_mc) + '][tempo_previsto_lavorazione]'])
                                            prodotto.append(campo)
                                            break

                            num_mc += 1

                ordine.append(prodotto)

    num_pr += 1

print(ordine)
alessandro buffoli
  • 658
  • 2
  • 10
  • 29