Advertisement
Guest User

Untitled

a guest
Aug 19th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.69 KB | None | 0 0
  1. @bp.route('/imports/<import_id>/citizens/<citizen_id>', methods=['PATCH'])
  2. def update_user(import_id, citizen_id):
  3. citizen = db.session.query(Citizen).filter_by(import_id=import_id, citizen_id=citizen_id).first()
  4. if (citizen == None):
  5. return ('', 400)
  6. updated_info = request.json
  7. citizen_address = citizen.get_address()
  8. print(citizen_address)
  9. for (key,val) in updated_info.items():
  10. if key == 'relatives':
  11. upd_relatives = True
  12. elif key == 'name':
  13. citizen.set_name(val)
  14. elif key == 'birth_date':
  15. citizen.set_birth_date(val)
  16. elif key == 'gender':
  17. citizen.set_gender(val)
  18. elif key in citizen_address.keys():
  19. citizen_address[key]=val
  20. upd_address = True
  21. else:
  22. return ('', 400)
  23.  
  24. print(citizen_address)
  25. if upd_address:
  26. new_address = "{0};{1};{2};{3}".format(citizen_address['town'], citizen_address['street'], citizen_address['building'], citizen_address['apartment'])
  27. citizen.set_address(new_address)
  28.  
  29. if upd_relatives:
  30. print("Upd relatives")
  31. current_relatives = []
  32. new_relatives = []
  33. added_relatives = []
  34. deled_relatives = []
  35. if citizen.relatives != '':
  36. current_relatives = list(map(int, citizen.relatives.split(';')))
  37. current_relatives.sort()
  38. new_relatives = updated_info['relatives']
  39. new_relatives.sort()
  40. else:
  41. new_relatives = updated_info['relatives']
  42.  
  43. new_relatives_ptr = 0
  44.  
  45. for current_relative in current_relatives:
  46. while (new_relatives_ptr < len(new_relatives) and
  47. new_relatives[new_relatives_ptr] < current_relative):
  48. added_relatives.append(new_relatives[new_relatives_ptr])
  49. new_relatives_ptr += 1
  50. if (new_relatives_ptr >= len(new_relatives) or
  51. new_relatives[new_relatives_ptr] != current_relative):
  52. deled_relatives.append(current_relative)
  53. else:
  54. new_relatives_ptr += 1
  55.  
  56. while new_relatives_ptr < len(new_relatives):
  57. added_relatives.append(new_relatives[new_relatives_ptr])
  58. new_relatives_ptr += 1
  59.  
  60. for deled_relative in deled_relatives:
  61. citizen_to_clear = db.session.query(Citizen).filter_by(import_id=import_id, citizen_id=deled_relative).first()
  62. if citizen_to_clear is None:
  63. return '', 400
  64. gd = citizen_to_clear.relatives
  65. gd = gd.replace(str(citizen_id) + ';', '')
  66. gd = gd.replace(str(citizen_id), '')
  67. citizen_to_clear.relatives = gd
  68.  
  69. for added_relative in added_relatives:
  70. citizen_to_add = db.session.query(Citizen).filter_by(import_id=import_id, citizen_id=added_relative).first()
  71. if citizen_to_add is None:
  72. return '', 400
  73. gd = citizen_to_add.relatives
  74. gd = gd.replace(str(citizen_id) + ';', '')
  75. gd = gd.replace(str(citizen_id), '')
  76. if gd == '':
  77. gd = str(citizen_id)
  78. else:
  79. gd = str(citizen_id) + ';' + gd
  80. citizen_to_add.relatives = gd
  81.  
  82. citizen.relatives = ';'.join(list(map(str, new_relatives)))
  83. print(new_relatives, current_relatives)
  84. print(added_relatives, deled_relatives)
  85.  
  86. return_answer = citizen.json()
  87. print(citizen.__dict__)
  88.  
  89. if citizen.error:
  90. return '', 400
  91.  
  92. db.session.commit()
  93. return jsonify({'data':return_answer}), 200
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement