def update(self): """ Update user values in the database """ self.__request_account() self.__request_profile() self.__request_password() if 'file' in request.files and self.__upload_avatar(): self.user['image'] = self.list_images if self.message is None: if len(request.form['password_new']): password_new = create_password(request.form['password_new']) self.user['password'] = password_new # If is changed the username it's important # the old avatars directory in the new position if self.changed_username and len(self.user['image']): src = os.path.join(UP_AVATARS_FOLDER,self.changed_username[0]) dst = os.path.join(UP_AVATARS_FOLDER,self.changed_username[1]) shutil.move(src,dst) try: g.db.users.update({ '_id' : ObjectId(self.user['_id']) }, self.user) self.status = 'msg msg-success' self.message = g.users_msg('success_update_user') except PyMongoError: self.message = g.users_msg('error_mongo_update')
def __request_password(self, new_user=False): """ Get from request.form the password values and check it """ if not self.message is None: return False # Check that the password_new field is not empty if not len(request.form['password_new']) and new_user: self.message = g.users_msg('error_password_0') if len(request.form['password_new']): self.user['password_new'] = request.form['password_new'] self.user['password_check'] = request.form['password_check'] # Check that the new password has between 6 and 30 characters. if not check.length(self.user['password_new'], 6, 30): self.message = g.users_msg('error_password_1') # Check that both passwords are the same elif self.user['password_new'] != self.user['password_check']: self.message = g.users_msg('error_password_2') if 'password' in request.form: # Verify that the old password matches the one entered. old_password = create_password(request.form['password']) if self.user['password'] != old_password: self.message = g.users_msg('error_password_3')
def update(self): """ Update user values in the database """ self.__request_account() self.__request_profile() self.__request_password() if 'file' in request.files and self.__upload_avatar(): self.user['image'] = self.list_images if self.message is None: if len(request.form['password_new']): password_new = create_password(request.form['password_new']) self.user['password'] = password_new # If is changed the username it's important # the old avatars directory in the new position if self.changed_username and len(self.user['image']): src = os.path.join(UP_AVATARS_FOLDER, self.changed_username[0]) dst = os.path.join(UP_AVATARS_FOLDER, self.changed_username[1]) shutil.move(src, dst) try: g.db.users.update({'_id': ObjectId(self.user['_id'])}, self.user) self.status = 'msg msg-success' self.message = g.users_msg('success_update_user') except PyMongoError: self.message = g.users_msg('error_mongo_update')
def __request_password(self, new_user=False): """ Get from request.form the password values and check it """ if not self.message is None: return False # Check that the password_new field is not empty if not len(request.form['password_new']) and new_user: self.message = g.users_msg('error_password_0') if len(request.form['password_new']): self.user['password_new'] = request.form['password_new'] self.user['password_check'] = request.form['password_check'] # Check that the new password has between 6 and 30 characters. if not check.length(self.user['password_new'], 6, 30): self.message = g.users_msg('error_password_1') # Check that both passwords are the same elif self.user['password_new'] != self.user['password_check']: self.message = g.users_msg('error_password_2') if 'password' in request.form: # Verify that the old password matches the one entered. old_password = create_password(request.form['password']) if self.user['password'] != old_password: self.message = g.users_msg('error_password_3')
def update_password(self): """ Update user values in the database """ self.__request_password() if self.message is None: password_new = create_password(request.form['password_new']) self.user['password'] = password_new try: g.db.users.update({ '_id' : ObjectId(self.user['_id']) }, self.user) self.status = 'msg msg-success' self.message = g.users_msg('success_update_password') except PyMongoError: self.message = g.users_msg('account_error_1')
def update_password(self): """ Update user values in the database """ self.__request_password() if self.message is None: password_new = create_password(request.form['password_new']) self.user['password'] = password_new try: g.db.users.update({'_id': ObjectId(self.user['_id'])}, self.user) self.status = 'msg msg-success' self.message = g.users_msg('success_update_password') except PyMongoError: self.message = g.users_msg('account_error_1')
def new(self): """ Insert new user in the database """ self.__request_account() self.__request_profile() self.__request_password(True) if self.message is None: password_new = create_password(request.form['password_new']) self.user['image'] = '' self.user['password'] = password_new try: g.db.users.insert(self.user) return True except PyMongoError: self.message = g.users_msg('error_mongo_new') return False
def new(self): """ Insert new user in the database """ self.__request_account() self.__request_profile() self.__request_password(True) if self.message is None: password_new = create_password(request.form['password_new']) self.user['image'] = '' self.user['password'] = password_new try: g.db.users.insert(self.user) return True except PyMongoError: self.message = g.users_msg('error_mongo_new') return False
def login_page(): """ """ if request.method == 'POST': username = request.form['username'].lower() password = request.form['password'] user = g.db.users.find_one({'username' : username}) if not username and not password: g.status = 'mes-red' g.message = dict_login['error_1'] elif user is None or user['password'] != create_password(password): g.status = 'mes-red' g.message = dict_login['error_2'] else: session['user_id'] = user['_id'] return redirect(url_for('admin')) return render_template('admin/login.html')
def login_page(): """ """ if request.method == 'POST': username = request.form['username'].lower() password = request.form['password'] user = g.db.users.find_one({'username': username}) if not username and not password: g.status = 'mes-red' g.message = dict_login['error_1'] elif user is None or user['password'] != create_password(password): g.status = 'mes-red' g.message = dict_login['error_2'] else: session['user_id'] = user['_id'] return redirect(url_for('admin')) return render_template('admin/login.html')
def profile_page(): """ """ if request.method == 'POST': # get request ot_name username = request.form['username'] password = request.form['password'] password_check = request.form['password_check'] regx = re.compile('^'+username+'$', re.IGNORECASE) result = g.db.users.find_one({"username" : regx }) old_username = g.my['username'] if len(password) < 6 and len(password) > 0: g.message = setting_message['password_error_1'] g.status = 'mes-red' elif password != password_check and len(password) > 0: g.message = setting_message['password_error_2'] g.status = 'mes-red' # control several things: # - username wrote # - username's length is greater than 2 # - username is available and it is not the same as # - the format of username is incorrect elif not len(username): g.message = setting_message['account_error_1'] g.status = 'mes-red' elif len(username) < 2: g.message = setting_message['account_error_2'] g.status = 'mes-red' elif result is not None and username != old_username: g.message = setting_message['account_error_4'] g.status = 'mes-red' elif not re.match(r'^[a-zA-Z0-9_]+$', username): g.message = setting_message['account_error_7'] g.status = 'mes-red' else: g.my['username'] = username g.my['password'] = create_password(password) g.db.users.update({"_id": g.my['_id']}, g.my) g.message = setting_message['account_ok'] g.status = 'mes-green' return render_template('admin/profile.html')
def profile_page(): """ """ if request.method == 'POST': # get request ot_name username = request.form['username'] password = request.form['password'] password_check = request.form['password_check'] regx = re.compile('^' + username + '$', re.IGNORECASE) result = g.db.users.find_one({"username": regx}) old_username = g.my['username'] if len(password) < 6 and len(password) > 0: g.message = setting_message['password_error_1'] g.status = 'mes-red' elif password != password_check and len(password) > 0: g.message = setting_message['password_error_2'] g.status = 'mes-red' # control several things: # - username wrote # - username's length is greater than 2 # - username is available and it is not the same as # - the format of username is incorrect elif not len(username): g.message = setting_message['account_error_1'] g.status = 'mes-red' elif len(username) < 2: g.message = setting_message['account_error_2'] g.status = 'mes-red' elif result is not None and username != old_username: g.message = setting_message['account_error_4'] g.status = 'mes-red' elif not re.match(r'^[a-zA-Z0-9_]+$', username): g.message = setting_message['account_error_7'] g.status = 'mes-red' else: g.my['username'] = username g.my['password'] = create_password(password) g.db.users.update({"_id": g.my['_id']}, g.my) g.message = setting_message['account_ok'] g.status = 'mes-green' return render_template('admin/profile.html')
def login_page(): """ """ if request.method == 'POST': username = request.form['username'] password = request.form['password'] regx = re.compile('^' + username + '$', re.IGNORECASE) user = g.db.users.find_one({'username': regx}) status = 'mes_red' # If the field isn't complete if not username and not password: message = g.login['error_1'] # If there are wrong Username/Email and password combination. elif user is None or user['password'] != create_password(password): message = g.login['error_2'] else: session['user_id'] = user['_id'] return redirect(url_for('home.index')) return render_template(MODULE_DIR + '/login.html', **locals())
def login_page(): """ """ if request.method == 'POST': username = request.form['username'] password = request.form['password'] regx = re.compile('^'+username+'$', re.IGNORECASE) user = g.db.users.find_one({'username' : regx}) status = 'mes_red' # If the field isn't complete if not username and not password: message = g.login['error_1'] # If there are wrong Username/Email and password combination. elif user is None or user['password'] != create_password(password): message = g.login['error_2'] else: session['user_id'] = user['_id'] return redirect(url_for('home.index')) return render_template(MODULE_DIR+'/login.html', **locals())
from pages import pages from rank import rank from settings import settings from users import users USERNAME = '******' PASSWORD = '******' # Save connectio test database connection = Connection() db = connection.app_test db.users.remove({'username': USERNAME}) data = { "username": USERNAME, "email": "", "password": create_password(PASSWORD), "name": "", "description": "", "rank": 10, "lan": "en", "language": "English", "time_zone": "Europe/London", "image": "", "location": "", "web": "" } db.users.insert(data) class CheckAdminPages(unittest.TestCase):
from login import login from hash_table import hash_table from home import home from pages import pages from rank import rank from settings import settings from users import users USERNAME = '******' PASSWORD = '******' # Save connectio test database connection = Connection() db = connection.app_test db.users.remove({'username' : USERNAME}) data = { "username" : USERNAME, "email" : "", "password" : create_password(PASSWORD), "name" : "", "description" : "", "rank" : 10, "lan" : "en", "language" : "English", "time_zone" : "Europe/London", "image" : "", "location" : "", "web" : "" } db.users.insert(data) class CheckAdminPages(unittest.TestCase): user_data = { "username" : "Leonardo", "email" : "", "password_new" : "", "password_check" : "", "name" : "", "description" : "", "rank" : "12",