api_manager.py 1.31 KB
Newer Older
wanli's avatar
wanli committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#!/usr/bin/env python
# -*- coding: utf_8 -*-

import time
import json
import logging
import traceback
import uuid
from datetime import datetime

from pony.orm import *
from model import fullStackDB
from model.user import User
from utils import md5_salt
wanli's avatar
wanli committed
15
from utils.ccode import convert_string
wanli's avatar
wanli committed
16 17 18 19 20 21 22

logger = logging.getLogger("ApiManager")

class ApiManager(object):
    def __init__(self):
        super(ApiManager, self).__init__()

wanli's avatar
wanli committed
23
    def update_user_password(self, user, data):
wanli's avatar
wanli committed
24
        with db_session:
wanli's avatar
wanli committed
25
            editor = User.get(id=user)
wanli's avatar
wanli committed
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
            if not editor:
                return False, "current user is not exists"

            result = User.get(uuid=data.get("uuid"), password=md5_salt(data['password']))

            if not result:
                return None, "user does not exists"

            data.pop("uuid")
            data['password'] = md5_salt(data['newPassword'])
            data.pop('newPassword')
            data.update({
                "create_by": editor.id,
                "create_at": datetime.now(),
                "update_by": editor.id,
                "update_at": datetime.now()
            })

            result = result.set(**data)
            commit()

            return True, "success"

wanli's avatar
wanli committed
49 50 51
    def get_escape_text(self, data):
        return convert_string(data['string'])

wanli's avatar
wanli committed
52
apiManager = ApiManager()