Related
I'm somewhere between beginner and intermediate level using Dart/Flutter combination.
I'm building a Marine logbook whereby I need to be able to log a duty that is made up a a variable number of activities. See the code below. In this example I have two duties that each comprise 1 activity.
List<Map<String, dynamic>> duties = [
{
'date': '12122021',
'dutyStart': '00:00',
'dutyFinish': '12:00',
'skipper': 'Mark',
'crew': 'My Crew',
'assets': 'G1',
'activity': [
{
'activityType': 'Training',
'trainingActivityDetail': 'Navigation',
'wapolJobNumber': null,
'rapNumber': null,
'activityStart': '02:00',
'activityFinish': '03:00',
'startFuel': 100,
'endFuel': 300,
'fuelUsed': 200,
}
],
},
{
'date': '01092021',
'dutyStart': '18:00',
'dutyFinish': '23:00',
'skipper': 'Neil',
'crew': 'My Crew',
'assets': 'G2',
'activity': [
{
'activityType': 'WAMSAR',
'trainingActivityDetail': null,
'wapolJobNumber': '123',
'rapNumber': '456',
'activityStart': '19:00',
'activityFinish': '20:00',
'startFuel': 1000,
'endFuel': 3000,
'fuelUsed': 2000,
}
],
}
];
I've worked out how to add a duty with the following code:
//add a duty
duties.add(
{
'date': '111111',
'dutyStart': '11:11',
'dutyFinish': '22:22',
'skipper': 'Tom',
'crew': 'My Crew',
'assets': 'Ranger',
'activity': [
{
'activityType': 'WAMSAR',
'trainingActivityDetail': null,
'wapolJobNumber': '000',
'rapNumber': '999',
'activityStart': '19:00',
'activityFinish': '20:00',
'startFuel': 1000,
'endFuel': 3000,
'fuelUsed': 2000,
}
],
},
);
But I can't work out how to add an activity to a duty.
For example, how would I add the activity below to the last duty so that the last duty now has 2 activities instead of 1?:
{
'activityType': 'XXX',
'trainingActivityDetail': null,
'wapolJobNumber': '000',
'rapNumber': '999',
'activityStart': '19:00',
'activityFinish': '20:00',
'startFuel': 1000,
'endFuel': 3000,
'fuelUsed': 2000,
}
For now I'm just using the data in a listview builder that isn't being saved anywhere. It will end up being saved in firestore cloud. Just wondering as well how I might save an activity in firestore? I'm happy with saving a duty in firestore but haven't taken the leap into saving lists in lists yet.
Apologies if this has been covered previously. I did check prior to posting but didn't see anything of help.
you can try this. Here we insert the activity element in the last duty as follows:
duties.last["activity"].add(
{
'activityType': 'WAMSAR',
'trainingActivityDetail': null,
'wapolJobNumber': '000',
'rapNumber': '999',
'activityStart': '19:00',
'activityFinish': '20:00',
'startFuel': 1000,
'endFuel': 3000,
'fuelUsed': 200023,
}
);
print(duties);
Output
[{date: 12122021, dutyStart: 00:00, dutyFinish: 12:00, skipper: Mark, crew: My Crew, assets: G1, activity: [{activityType: Training, trainingActivityDetail: Navigation, wapolJobNumber: null, rapNumber: null, activityStart: 02:00, activityFinish: 03:00, startFuel: 100, endFuel: 300, fuelUsed: 200}]}, {date: 01092021, dutyStart: 18:00, dutyFinish: 23:00, skipper: Neil, crew: My Crew, assets: G2, activity: [{activityType: WAMSAR, trainingActivityDetail: null, wapolJobNumber: 123, rapNumber: 456, activityStart: 19:00, activityFinish: 20:00, startFuel: 1000, endFuel: 3000, fuelUsed: 2000}, {activityType: WAMSAR, trainingActivityDetail: null, wapolJobNumber: 000, rapNumber: 999, activityStart: 19:00, activityFinish: 20:00, startFuel: 1000, endFuel: 3000, fuelUsed: 200023}]}]
How can I upate the following List of Map:
List<Map<String, dynamic>> entries = [
{
'name1': 'John',
'age1': 21
},
{
'name2': 'Paul',
'age2': 18
}
]
I tried to use
entries.update({'name2': 'Frank', 'age2': 28});
but didn't work.
if you know the index.
Then Try This:
List<Map<String, dynamic>> entries = [
{'name1': 'John', 'age1': 21},
{'name2': 'Paul', 'age2': 18}
];
void updateMap() {
print(entries[1]['name2']);
entries[1] = {'name2': 'Usman', 'age2': 88};
print(entries[1]['name2']);
}
This might work
List<Map<String,dynamic>> entries =[];
entries[index] ={'name2': 'Frank', 'age2': 28};
Try this way
entries[1] = {'name2': 'Frank', 'age2': 28};
I am retrieving data from multiple tables in Django.
my current response is :
{
"status": 0,
"message": "Client details retrived successfully...!!!",
"results": [
{
"id": 11,
"client_id": "CL15657917080578748000",
"client_name": "Pruthvi Katkar",
"client_pan_no": "RGBB004A11",
"client_adhar_no": "12312312313",
"legal_entity_name": "ABC",
"credit_period": "6 months",
"client_tin_no": 4564565,
"client_email_id": "abc#gmail.com",
"head_office_name": "ABC",
"office_name": "asd234",
"office_email_id": "zxc#gmail.com",
"office_contact": "022-27547119",
"gst_number": "CGST786876876",
"office_country": "India",
"office_state": "gujrat",
"office_district": "vadodara",
"office_taluka": "kachh",
"office_city": "vadodara",
"office_street": "New rode 21",
"office_pincode": 2344445,
"contact_person_name": "prasad",
"contact_person_designation": "DM",
"contact_person_number": "456754655",
"contact_person_email": "asd#gmail.com",
"contact_person_mobile": "5675545654",
"created_at": "2019-08-14T14:08:28.057Z",
"created_by": "Prathamseh",
"updated_at": "2019-08-14T14:08:28.057Z",
"updated_by": "prasad",
"is_deleted": false
},
{
"id": 11,
"user_id": "CL15657917080578748000",
"bank_details_id": "BL15657917080778611000",
"bank_name": "Pruthvi",
"branch": "vashi",
"ifsc_code": "BOI786988",
"account_number": 56756765765765,
"account_name": "Pruthvi",
"is_deleted": false
},
{
"id": 10,
"document_details_id": "DL15657917080808598000",
"user_id": "CL15657917080578748000",
"document_type": "Pruthvi ID",
"document": "www.sendgrid.com/pan",
"is_deleted": false
}
]
}
Expected Response :
I am getting the queryset form db in models.py and i am sending it to the views.py and i am iterating over the dict but not getting the expected response.
views.py
#csrf_exempt
def get_client_details(request):
try:
# Initialising lists for storing results
result = []
temp_array = []
# Getting data from request body
client_master_dict = json.loads(request.body)
# Response from get client data
records = ClientDetails.get_client_data(client_master_dict)
# Create response object
# Iterating over the records object for getting data
for i in range(len(records)):
# Converting the querysets objects to json array format
record_result_list = list(records[i].values())
# If multiple records are present
if(len(record_result_list) > 1):
for j in range(len(record_result_list)):
user_info = record_result_list[j]
temp_array.append(user_info)
result.append(temp_array)
temp_array=[]
# For single record
else:
result.append(record_result_list[0])
# Success
returnObject = {
"status" : messages.SUCCESS,
"message" : messages.CLIENT_RETRIVE_SUCCESS,
"results" : result
}
return JsonResponse(returnObject,safe=False)
I think the issue might be in my inner for loop, can anyone help me out with this, is there any way to iterate over the nested JSON object.
Models.py
#classmethod
def get_client_data(cls, client_master_dict):
try:
response_list = []
client_id = client_master_dict['client_id']
client_details = cls.objects.filter(client_id = client_id,is_deleted = False)
bank_details = BankDetails.objects.filter(user_id = client_id,is_deleted = False)
document_details = DocumentDetails.objects.filter(user_id = client_id,is_deleted = False)
response_list.append(client_details)
response_list.append(bank_details)
response_list.append(document_details)
return response_list
except(Exception) as error:
print("Error in get_client_data",error)
return False
Here i'm fetching data from 3 tables and adding it into list.
After printing the data on console i am getting :
[{'id': 11, 'client_id': 'CL15657917080578748000', 'client_name': 'Pruthvi Katkar', 'client_pan_no': 'RGBB004A11', 'client_adhar_no': '12312312313', 'legal_entity_name': 'ABC', 'credit_period': '6 months', 'client_tin_no': 4564565, 'client_email_id': 'abc#gmail.com', 'head_office_name': 'ABC', 'office_name': 'asd234', 'office_email_id': 'zxc#gmail.com', 'office_contact': '022-27547119', 'gst_number': 'CGST786876876', 'office_country': 'India', 'office_state': 'gujrat', 'office_district': 'vadodara', 'office_taluka': 'kachh', 'office_city': 'vadodara', 'office_street': 'New rode 21', 'office_pincode': 2344445, 'contact_person_name': 'prasad', 'contact_person_designation': 'DM', 'contact_person_number': '456754655', 'contact_person_email': 'asd#gmail.com', 'contact_person_mobile': '5675545654', 'created_at': datetime.datetime(2019, 8, 14, 14, 8, 28, 57874, tzinfo=<UTC>), 'created_by': 'Prathamseh', 'updated_at': datetime.datetime(2019, 8, 14, 14, 8, 28, 57874, tzinfo=<UTC>), 'updated_by': 'prasad', 'is_deleted': False}]
[{'id': 11, 'user_id': 'CL15657917080578748000', 'bank_details_id': 'BL15657917080778611000', 'bank_name': 'Pruthvi', 'branch': 'vashi', 'ifsc_code': 'BOI786988', 'account_number': 56756765765765, 'account_name': 'Pruthvi', 'is_deleted': False}]
[{'id': 10, 'document_details_id': 'DL15657917080808598000', 'user_id': 'CL15657917080578748000', 'document_type': 'Pruthvi ID', 'document': 'www.sendgrid.com/pan', 'is_deleted': False}]
Did you check the output of record_result_list? You can outright tell their if it's recovering the data in the format you requested. Try the printing to screen method to debug.
As far as I cam see, the expected output and the hierarchy of results for bank details are not matching. I don't know how you are handling the hierarchy. Are you directly taking it from JSON as the hierarchy? Or are you just taking the data and creating hierarchy in the expected output?
I am trying to test elastic search with the following instruction:
http://docs.aws.amazon.com/elasticsearch-service/latest/developerguide/es-gsg.html
When I try the above and upload the following text which is also in the instruction:
{
"index": {
"_index": "movies",
"_type": "listings",
"_id": "2"
}
} {
"director": "Frankenheimer, John",
"genre": ["Drama", "Mystery", "Thriller"],
"year": 1962,
"actor": ["Lansbury, Angela", "Sinatra, Frank", "Leigh, Janet", "Harvey, Laurence", "Silva, Henry", "Frees, Paul", "Gregory, James", "Bissell, Whit", "McGiver, John", "Parrish, Leslie", "Edwards, James", "Flowers, Bess", "Dhiegh, Khigh", "Payne, Julie", "Kleeb, Helen", "Gray, Joe", "Nalder, Reggie", "Stevens, Bert", "Masters, Michael", "Lowell, Tom"],
"title": "The Manchurian Candidate"
} {
"index": {
"_index": "movies",
"_type": "listings",
"_id": "3"
}
} {
"director": "Baird, Stuart",
"genre": ["Action", "Crime", "Thriller"],
"year": 1998,
"actor": ["Downey Jr., Robert", "Jones, Tommy Lee", "Snipes, Wesley", "Pantoliano, Joe", "Jacob, Ir\u00e8ne", "Nelligan, Kate", "Roebuck, Daniel", "Malahide, Patrick", "Richardson, LaTanya", "Wood, Tom", "Kosik, Thomas", "Stellate, Nick", "Minkoff, Robert", "Brown, Spitfire", "Foster, Reese", "Spielbauer, Bruce", "Mukherji, Kevin", "Cray, Ed", "Fordham, David", "Jett, Charlie"],
"title": "U.S. Marshals"
} {
"index": {
"_index": "movies",
"_type": "listings",
"_id": "4"
}
} {
"director": "Ray, Nicholas",
"genre": ["Drama", "Romance"],
"year": 1955,
"actor": ["Hopper, Dennis", "Wood, Natalie", "Dean, James", "Mineo, Sal", "Backus, Jim", "Platt, Edward", "Ray, Nicholas", "Hopper, William", "Allen, Corey", "Birch, Paul", "Hudson, Rochelle", "Doran, Ann", "Hicks, Chuck", "Leigh, Nelson", "Williams, Robert", "Wessel, Dick", "Bryar, Paul", "Sessions, Almira", "McMahon, David", "Peters Jr., House"],
"title": "Rebel Without a Cause"
}
After when I use this command :
curl -XGET 'search-movies-4f3nw7eiia2xiynjr55a2nao2y.us-west-1.es.amazonaws.com/movies/_search?q=Frankenheimer'
I never get anything back. always returns 0. Here is what I get back:
I am not if I am doing it right but I think I should be able to see at least something because I have Frankenheimer in the director key of the json data uploaded. Can anyone shed light on it?
Try searching the whole index with curl -XGET search-movies-4f3nw7eiia2xiynjr55a2nao2y.us-west-1.es.amazonaws.com/movies/_search which will return the first 10 results by default. If it returns less than 10, the data is likely not in the index.
You can verify that by changing the query to:
curl -XGET search-movies-4f3nw7eiia2xiynjr55a2nao2y.us-west-1.es.amazonaws.com/movies/_search?q=director:Burton
Check out the Elasticsearch URI Search for the complete list of the parameters.
I'm quite new to Python and programming in general, so apologies if this is quite basic or has been asked and answered before. Here is a sample of the data I'm working with:
{
"homeTeam": {
"formation": [
"4",
"4",
"2"
],
"lineupsSorted": [
{
"player": {
"name": "Scott P. Brown",
"slug": "scott-p-brown",
"shortName": "S. P. Brown",
"id": 19889,
"hasImage": true
},
"position": 1,
"shirtNumber": 1,
"substitute": false,
"positionName": "Goalkeeper",
"positionNameshort": "G",
"captain": false
},
{
"player": {
"name": "Carl Winchester",
"slug": "carl-winchester",
"shortName": "C. Winchester",
"id": 110785,
"hasImage": true
},
"position": 2,
"shirtNumber": 27,
"substitute": false,
"positionName": "Midfielder",
"positionNameshort": "M",
"captain": false
},
I am looking to automate populating defined names as I have done manually here:
hometeamPositions =['Goalkeeper','Midfielder','Defender','Defender','Defender','Midfielder','Midfielder','Midfielder','Midfielder','Forward','Forward','Goalkeeper','Defender','Defender','Midfielder','Midfielder','Forward','Forward']
hometeamPlayers = ['S. P. Brown','C. Winchester','M. Onariase','W.
Boyle','J. Cranston','H. Pell','J. Rowe','K. Storer','B. Waters','D.
Wright','D. Holman','R. Lovett','J. Barthram','T. Plavotic','J.
Munns','L. Davis','K. Wootton','J. Dayton']
As I will be repeating this process many hundreds of times with different data (same structure) I was wondering if anyone could give me some tips on automatically building these ranges?
Thanks,
Peter
I'm not sure I understood what is the problem you are trying to solve but I'll try to help.
Assuming you have a dictionary team_dict and you want to create 2 list: hometeamPositions and hometeamPlayers you can use the following code:
hometeamPlayers = []
hometeamPositions = []
for player_dict in teams_dict['homeTeam']['lineupsSorted']:
hometeamPlayers.append(player_dict['player']['shortName'])
hometeamPositions.append(player_dict['positionName'])
The output on your example will be:
hometeamPlayers = ['S. P. Brown', 'C. Winchester']
hometeamPositions = ['Goalkeeper', 'Midfielder']