This function is used to update the users.
(uid, data)
| 702 | |
| 703 | |
| 704 | def update_user(uid, data): |
| 705 | """ |
| 706 | This function is used to update the users. |
| 707 | """ |
| 708 | |
| 709 | usr = User.query.get(uid) |
| 710 | if not usr: |
| 711 | return False, _("Unable to update user '{0}'").format(uid) |
| 712 | |
| 713 | # Username and email can not be changed for internal users |
| 714 | if usr.auth_source == INTERNAL: |
| 715 | non_editable_params = ('username', 'email') |
| 716 | else: |
| 717 | non_editable_params = ('username',) |
| 718 | |
| 719 | for f in non_editable_params: |
| 720 | if f in data: |
| 721 | return False, _("'{0}' cannot be modified.").format(f) |
| 722 | |
| 723 | try: |
| 724 | new_data = validate_user(data) |
| 725 | if 'roles' in new_data: |
| 726 | new_data['roles'] = [Role.query.get(new_data['roles'])] |
| 727 | except Exception as e: |
| 728 | return False, str(e) |
| 729 | |
| 730 | try: |
| 731 | for k, v in new_data.items(): |
| 732 | setattr(usr, k, v) |
| 733 | |
| 734 | db.session.commit() |
| 735 | except Exception as e: |
| 736 | return False, str(e) |
| 737 | |
| 738 | return True, '' |
| 739 | |
| 740 | |
| 741 | def delete_user(uid): |
no test coverage detected