Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @bp.route('/imports/<import_id>/citizens/<citizen_id>', methods=['PATCH'])
- def update_user(import_id, citizen_id):
- citizen = db.session.query(Citizen).filter_by(import_id=import_id, citizen_id=citizen_id).first()
- if (citizen == None):
- return ('', 400)
- updated_info = request.json
- citizen_address = citizen.get_address()
- print(citizen_address)
- for (key,val) in updated_info.items():
- if key == 'relatives':
- upd_relatives = True
- elif key == 'name':
- citizen.set_name(val)
- elif key == 'birth_date':
- citizen.set_birth_date(val)
- elif key == 'gender':
- citizen.set_gender(val)
- elif key in citizen_address.keys():
- citizen_address[key]=val
- upd_address = True
- else:
- return ('', 400)
- print(citizen_address)
- if upd_address:
- new_address = "{0};{1};{2};{3}".format(citizen_address['town'], citizen_address['street'], citizen_address['building'], citizen_address['apartment'])
- citizen.set_address(new_address)
- if upd_relatives:
- print("Upd relatives")
- current_relatives = []
- new_relatives = []
- added_relatives = []
- deled_relatives = []
- if citizen.relatives != '':
- current_relatives = list(map(int, citizen.relatives.split(';')))
- current_relatives.sort()
- new_relatives = updated_info['relatives']
- new_relatives.sort()
- else:
- new_relatives = updated_info['relatives']
- new_relatives_ptr = 0
- for current_relative in current_relatives:
- while (new_relatives_ptr < len(new_relatives) and
- new_relatives[new_relatives_ptr] < current_relative):
- added_relatives.append(new_relatives[new_relatives_ptr])
- new_relatives_ptr += 1
- if (new_relatives_ptr >= len(new_relatives) or
- new_relatives[new_relatives_ptr] != current_relative):
- deled_relatives.append(current_relative)
- else:
- new_relatives_ptr += 1
- while new_relatives_ptr < len(new_relatives):
- added_relatives.append(new_relatives[new_relatives_ptr])
- new_relatives_ptr += 1
- for deled_relative in deled_relatives:
- citizen_to_clear = db.session.query(Citizen).filter_by(import_id=import_id, citizen_id=deled_relative).first()
- if citizen_to_clear is None:
- return '', 400
- gd = citizen_to_clear.relatives
- gd = gd.replace(str(citizen_id) + ';', '')
- gd = gd.replace(str(citizen_id), '')
- citizen_to_clear.relatives = gd
- for added_relative in added_relatives:
- citizen_to_add = db.session.query(Citizen).filter_by(import_id=import_id, citizen_id=added_relative).first()
- if citizen_to_add is None:
- return '', 400
- gd = citizen_to_add.relatives
- gd = gd.replace(str(citizen_id) + ';', '')
- gd = gd.replace(str(citizen_id), '')
- if gd == '':
- gd = str(citizen_id)
- else:
- gd = str(citizen_id) + ';' + gd
- citizen_to_add.relatives = gd
- citizen.relatives = ';'.join(list(map(str, new_relatives)))
- print(new_relatives, current_relatives)
- print(added_relatives, deled_relatives)
- return_answer = citizen.json()
- print(citizen.__dict__)
- if citizen.error:
- return '', 400
- db.session.commit()
- return jsonify({'data':return_answer}), 200
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement