OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Get values from request

  • Thread starter Thread starter Oliver Bender
  • Start date Start date
O

Oliver Bender

Guest
I am trying to write values from a request.POST to a string format. By default, these always start with "epochs-NUMBER-". Therefore, I should draw a For loop so that a new line is created for each number.

The complete request looks like that. But I only need the 'epochs-NUMBER-' values

csrfmiddlewaretoken=hJmSuP3O6GmtwBeJhfqb99LaCVOKxgngGNIEqoqhQQR7tQELUVGHeXRXxfnRE1Ah&base-TOTAL_FORMS=1&base-INITIAL_FORMS=0&base-MIN_NUM_FORMS=0&base-MAX_NUM_FORMS=1000&base-0-time=25.06.2024+13%3A36%3A00&base-0-show_differences=on&base-0-show_null=1&source=Manual&epochs-TOTAL_FORMS=3&epochs-INITIAL_FORMS=3&epochs-MIN_NUM_FORMS=0&epochs-MAX_NUM_FORMS=1000&unit_selector=m&unit_selector=m&unit_selector=m&unit_selector=m&epochs-0-target_name=250.01.G&epochs-0-easting=55555&epochs-0-northing=5555&epochs-0-height=555&epochs-0-azimuth=&epochs-0-zenithangle=&epochs-0-slope_dist=&epochs-1-target_name=170.01.G&epochs-1-easting=44646&epochs-1-northing=6554&epochs-1-height=544&epochs-1-azimuth=&epochs-1-zenithangle=&epochs-1-slope_dist=&epochs-2-target_name=150.01.G&epochs-2-easting=78979878&epochs-2-northing=4645&epochs-2-height=455&epochs-2-azimuth=&epochs-2-zenithangle=&epochs-2-slope_dist=

The request looks like that: Request

Code:
def nmea_string(self, request, target_name):
   my_dict = {}
   keys = []
   values = []
   target_raw_string = ("PTER," + self.short_name + "_TRG," + target_name + "," +
                        self.time.strftime("%Y-%m-%d %H:%M:%S") + "," + str(self.status_target))
   for key, value in request.POST.items():
       keys.append(key)
       values.append(value)
   for i in range(len(keys)):
       my_dict.update({keys[i]: values[i]})
   
   for k, v in my_dict.items():
       if k.startswith('epochs-0-'):
           if v == '':
               v = '-9999.999900'
           if k.startswith('epochs-0-target_name'):
               target_raw_string += "," + v
           else:
               target_raw_string += "," + v + ",0.000000"

   tg_checksum = self.get_checksum(target_raw_string)
   tg_nmea_string = "$" + target_raw_string + "*" + tg_checksum + "\n"

   nmea_string += tg_nmea_string

   return nmea_string

Code:
The end result should look a bit like this:

Code:
$PTER,TS_TRG,TP-008,2024-04-22 23:00:00,129,675666.252000,0.000000,252132.398000,0.000000,412.137000,0.000000,-9999.999900,0.000000,-9999.999900,0.000000,-9999.999900,0.000000*1F
$PTER,TS_TRG,TP-009,2024-04-22 23:00:00,129,675704.440000,0.000000,252162.055000,0.000000,412.341000,0.000000,-9999.999900,0.000000,-9999.999900,0.000000,-9999.999900,0.000000*1A
<p>I am trying to write values from a request.POST to a string format.
By default, these always start with "epochs-NUMBER-".
Therefore, I should draw a For loop so that a new line is created for each number.</p>
<p>The complete request looks like that. But I only need the 'epochs-NUMBER-' values</p>
<p><code>csrfmiddlewaretoken=hJmSuP3O6GmtwBeJhfqb99LaCVOKxgngGNIEqoqhQQR7tQELUVGHeXRXxfnRE1Ah&base-TOTAL_FORMS=1&base-INITIAL_FORMS=0&base-MIN_NUM_FORMS=0&base-MAX_NUM_FORMS=1000&base-0-time=25.06.2024+13%3A36%3A00&base-0-show_differences=on&base-0-show_null=1&source=Manual&epochs-TOTAL_FORMS=3&epochs-INITIAL_FORMS=3&epochs-MIN_NUM_FORMS=0&epochs-MAX_NUM_FORMS=1000&unit_selector=m&unit_selector=m&unit_selector=m&unit_selector=m&epochs-0-target_name=250.01.G&epochs-0-easting=55555&epochs-0-northing=5555&epochs-0-height=555&epochs-0-azimuth=&epochs-0-zenithangle=&epochs-0-slope_dist=&epochs-1-target_name=170.01.G&epochs-1-easting=44646&epochs-1-northing=6554&epochs-1-height=544&epochs-1-azimuth=&epochs-1-zenithangle=&epochs-1-slope_dist=&epochs-2-target_name=150.01.G&epochs-2-easting=78979878&epochs-2-northing=4645&epochs-2-height=455&epochs-2-azimuth=&epochs-2-zenithangle=&epochs-2-slope_dist=</code></p>
<p>The request looks like that:
<a href="https://i.sstatic.net/56NFvJHO.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/56NFvJHO.png" alt="Request" /></a></p>
<pre><code>def nmea_string(self, request, target_name):
my_dict = {}
keys = []
values = []
target_raw_string = ("PTER," + self.short_name + "_TRG," + target_name + "," +
self.time.strftime("%Y-%m-%d %H:%M:%S") + "," + str(self.status_target))
for key, value in request.POST.items():
keys.append(key)
values.append(value)
for i in range(len(keys)):
my_dict.update({keys: values})

for k, v in my_dict.items():
if k.startswith('epochs-0-'):
if v == '':
v = '-9999.999900'
if k.startswith('epochs-0-target_name'):
target_raw_string += "," + v
else:
target_raw_string += "," + v + ",0.000000"

tg_checksum = self.get_checksum(target_raw_string)
tg_nmea_string = "$" + target_raw_string + "*" + tg_checksum + "\n"

nmea_string += tg_nmea_string

return nmea_string
</code></pre>
<pre><code>The end result should look a bit like this:
</code></pre>
<pre><code>$PTER,TS_TRG,TP-008,2024-04-22 23:00:00,129,675666.252000,0.000000,252132.398000,0.000000,412.137000,0.000000,-9999.999900,0.000000,-9999.999900,0.000000,-9999.999900,0.000000*1F
$PTER,TS_TRG,TP-009,2024-04-22 23:00:00,129,675704.440000,0.000000,252162.055000,0.000000,412.341000,0.000000,-9999.999900,0.000000,-9999.999900,0.000000,-9999.999900,0.000000*1A
</code></pre>
 
Top