Пример #1
0
 def before_post(self):
     self.Print('%s fired' % inspect.stack()[0][3], Colors.GRAY)
     try:
         col_users = db()['users']
         result = col_users.find_one_and_update(
             {
                 'mobile': self.params['mobile'],
                 # 'pure_password': self.params['old_password'],
                 'activation_code': self.params['activation_code']
             },
             {
                 '$set': {
                     'password': create_md5(self.params['new_password']),
                     'password_pure': self.params['new_password']
                 }
             })
         if result is None:
             self.set_output('user', 'password_reset_failed')
         else:
             self.set_output('public_operations', 'successful')
     except:
         PrintException()
         self.set_output('public_operations', 'failed')
         return False
     self.allow_action = False
     return True
Пример #2
0
    def before_post(self):
        try:
            self.method = 'users'
            col_users = db()['users']
            if col_users.count({'mobile': self.params['mobile']}) > 0:
                self.set_output('user', 'mobile_exists')
                return False
            elif self.params.get('email') is not None:
                if col_users.count({'email': self.params['email']}) > 0:
                    self.set_output('user', 'email_exists')
                    return False
            # if col_users.count({'device_info.mac_address': self.params['device_info']['mac_address']}) > 0:
            #     self.set_output('user', 'multiple_registration')
            #     return False

            self.params['activation_code'] = random_digits()
            self.params['confirmed'] = False
            self.params['role'] = 'user'
            # self.params['tasks_figure'] = 'line' if 'tasks_figure' not in self.params else self.params['tasks_figure']
            self.params['password_pure'] = self.params['password']
            self.params['password'] = create_md5(self.params['password'])
            # .encode('utf-8')
        except:
            PrintException()
            return False
        return True
Пример #3
0
    def before_post(self):
        self.Print('%s fired' % inspect.stack()[0][3], Colors.GRAY)
        try:
            col_users = db()['users']
            col_user_logins = db()['user_logins']
            user_info = col_users.find_one({
                'mobile':
                self.params['mobile'],
                'password':
                create_md5(self.params['password'])
            })
            if user_info is None:
                self.set_output('user', 'login_failed')
            else:
                self.user_role = user_info['role']
                user_info = self.after_get_one(user_info)
                self.user_id = user_info['id']
                if user_info['confirmed']:
                    self.user_id = user_info['id']
                    self.set_output('public_operations', 'successful')
                    self.token = encode_token({
                        'user_id': self.user_id,
                        'role': self.user_role
                    }).decode('ascii')
                    self.output['token'] = self.token
                    last_login = col_user_logins.find({
                        'mobile':
                        self.params['mobile']
                    }).sort('date', -1).limit(1)
                    if last_login.count() > 0:
                        user_info['last_login'] = str(last_login[0]['date'])
                    else:
                        user_info['last_login'] = ''
                        user_info['first_login'] = str(datetime.now())
                    if 'last_update' in user_info: del user_info['last_update']
                    if 'password_pure' in user_info:
                        del user_info['password_pure']
                    if 'password' in user_info: del user_info['password']
                    if '_id' in user_info: del user_info['_id']
                    self.output['data']['item'] = user_info
                else:
                    self.set_output('user', 'inactive')
        except:
            PrintException()
            self.set_output('public_operations', 'failed')

        try:
            col_user_logins = db()['user_logins']
            col_user_logins.insert({
                'user_id': self.user_id,
                'mobile': self.params.get('mobile'),
                'status': self.status,
                'date': datetime.now(),
                'notes': self.note_id
            })
        except:
            PrintException()
        self.allow_action = False
Пример #4
0
    def put(self, *args, **kwargs):
        try:
            self.method = 'put'
            self.module = 'users'
            if self.pre_put():

                for item in self.params:
                    if item not in self.inputs['put']:
                        self.set_output('tasks', 'wrong_params')
                        return False

                need_consistency_update = any(
                    x in self.params for x in ['pic', 'name', 'family'])
                if 'password' in self.params:
                    self.params['password'] = create_md5(
                        self.params['password'])
                    self.params['password_pure'] = self.params['password']
                col_users = db()['users']
                col_users.update({'_id': ObjectId(self.user_id)},
                                 {'$set': self.params})
                if need_consistency_update:
                    col_people = db()['people']
                    col_tasks = db()['tasks']
                    doc = {}
                    if 'name' in self.params:
                        doc['people.name'] = self.params['name']
                    if 'family' in self.params:
                        doc['people.family'] = self.params['family']
                    if 'pic' in self.params:
                        doc['people.pic'] = self.params['pic']
                    col_tasks.update({'people.id': self.user_id},
                                     {'$set': doc},
                                     multi=True)
                    changes = {}

                    if 'name' in self.params and 'pic' not in self.params:
                        changes['$set'] = {'name': self.params['name']}
                    elif 'pic' in self.params and 'name' not in self.params:
                        changes['$set'] = {'pic': self.params['pic']}
                    elif 'pic' in self.params and 'name' in self.params:
                        changes['$set'] = {
                            'name': self.params['name'],
                            'pic': self.params['pic']
                        }

                    col_people.update({'user_id': self.user_id},
                                      changes,
                                      multi=True)
                self.params['last_update'] = datetime.now()
            self.set_output('public_operations', 'successful')
        except:
            PrintException()
            self.set_output('public_operations', 'failed')
        if consts.LOG_ACTIVE:
            self.log_status(self.output)
        self.after_put()
        self.kmwrite()
Пример #5
0
def insert_users():
    col_users = db()['users']
    # col_users.drop()
    if col_users.count({'username': '******'}) == 0:
        col_users.insert_one({
            "_id": ObjectId("5e25ab698c90582c5785d291"),
            'name': 'shahab',
            'family': 'qazavi',
            'mobile': 'admin',
            'password': create_md5('1'),
            'role': 'admin',
        })
Пример #6
0
 def before_delete(self):
     try:
         self.method = 'users'
         print(self.params['mobile'])
         print(self.user_id)
         col_users = db()['users']
         col_users.delete_one({
             '_id':
             ObjectId(self.params['id']),
             'mobile':
             self.params['mobile'],
             'password':
             create_md5(self.params['password'])
         })
         self.set_output('public_operations', 'successful')
     except:
         PrintException()
     self.allow_action = False
Пример #7
0
 def post(self, *args, **kwargs):
     data = deepcopy(output)
     try:
         file_contents = self.request.files['image'][0]['body']
         file_name = self.request.files['image'][0]['filename']
         type = self.get_argument('type', '')
         file_ext = '.' + file_name.split('.')[-1]
         if not os.path.exists(consts.PDP_ROOT):
             os.mkdir(consts.PDP_ROOT)
         if not os.path.exists(consts.PDP_IMAGES):
             os.mkdir(consts.PDP_IMAGES)
         if type != '':
             if not os.path.exists(consts.PDP_IMAGES + type):
                 os.mkdir(consts.PDP_IMAGES + type)
             if not os.path.exists(consts.PDP_IMAGES + type + '/' +
                                   str(datetime.today().date())):
                 os.mkdir(consts.PDP_IMAGES + type + '/' +
                          str(datetime.today().date()))
             filename = create_md5(str(datetime.now()) +
                                   file_name) + file_ext
             file = open(
                 '%s/%s/%s' % (consts.PDP_IMAGES + type,
                               datetime.today().date(), filename), 'wb')
             file.write(file_contents)
             file.close()
             self.set_output('public_operations', 'successful')
             data['data']['item'] = {
                 'link':
                 '%s/%s/%s' % (consts.ODP_IMAGES + type,
                               datetime.today().date(), filename)
             }
             print(data['data']['item'])
         else:
             self.set_output('field_error', 'file_type')
     except Exception:
         self.set_output('public_operations', 'failed')
         PrintException()
     self.write(data)