Example #1
0
def main():
    """Get results, parse and add to database"""
    sites = results(sys.argv[1])
    for site in sites:
        print site
    Db(sys.argv[1])
    db = Db()
    db.add(sites, sys.argv[1])
Example #2
0
def download_resource(url):
    try:
        # logger.debug("Downloading :" + url)
        exist_filename = fs.get_exist_file_name(url)
        if (exist_filename):
            logger.warn("file is existed: " + exist_filename)
            return exist_filename
        content = http.get_blob(url)
        out_path = fs.save_blob(url, content)
        logger.info('RESOURCE: %s => %s' % (url, out_path))
        Db.add_done_item(url)
        return out_path
    except Exception as err:
        logger.error('error download resource %r' % (err))
Example #3
0
 def setUp(self):
     self.db = Db(Application.connectionString)
     self.userService = UserService()
     self.languageService = LanguageService()
     self.termService = TermService()
     self.user = User()
     self.user.username = str(uuid.uuid1())
     self.user = self.userService.save(self.user)
     self.language = Language()
     self.languageService.save(self.language)
     
     Application.user = self.user
Example #4
0
def run_in_threads(data, action, thread_count):
    results = {}
    if not data or len(data) == 0:
        return results

    with concurrent.futures.ThreadPoolExecutor(
            max_workers=thread_count) as executor:
        # Start the load operations and mark each future with its URL
        future_to_url = {executor.submit(action, url): url for url in data}
        for future in concurrent.futures.as_completed(future_to_url):
            url = future_to_url[future]
            result = ''
            try:
                result = future.result()
                results[url] = result.replace(args.output, '')
            except Exception as exc:
                logger.error('%r generated an exception: %s' % (url, exc))
                Db.add_error_link(url, str(exc))
            else:
                logger.info('FECHED: %s => %s' % (url, result))
                Db.add_done_item(url)
    return results
Example #5
0
def crawl_page(url):
    global visited_links
    global downloaded_links
    global html_queue
    global resource_queue
    try:
        html = http.get_html(url)
        html_links = HtmlParser.get_links(args.url, html)
        resource_links = HtmlParser.get_resource_urls(args.url, html)
        lock.acquire()
        unique_links = list(set(html_links) - set(visited_links))
        visited_links = visited_links + unique_links
        Db.add_links(unique_links)
        for l in unique_links:
            html_queue.tasks.crawl_page(l)
        lock.release()
        lock2.acquire()
        unique_resource_links = list(
            set(resource_links) - set(downloaded_links))
        downloaded_links = downloaded_links + unique_resource_links
        lock2.release()
        if args.download_resources == True:
            resources = dict([(resource_url,
                               fs.get_filename_from_url(resource_url))
                              for resource_url in resource_links])
            html = HtmlParser.replace_resource_url(resources, html)

            for resource_link in unique_resource_links:
                resource_queue.tasks.download_resource(resource_link)

        output_path = fs.save_html(url, html)
        logger.info('HTML : %s -> %s' % (url, output_path))
        Db.add_done_item(url)
        return output_path
    except Exception as err:
        logger.error(err)
Example #6
0
 def __init__(self,
              school_name,
              city_name,
              teachers=None,
              courses=None,
              students=None,
              banjis=None):
     '''
     定义学校属性
     :param school_name: 学校名,字符类型
     :param city_name: 城市名,字符类型
     :param teachers: 讲师,字典类型,如{"teachers": []}
     :param students: 学员, 字典类型,如{"students": []}
     :param courses: 课程,字典类型,如{"courses": []}
     :param banjis: 班级,字典类型,如{"banjis": []}
     '''
     self.school_name = school_name
     self.city_name = city_name
     self.teachers = teachers
     self.courses = courses
     self.students = students
     self.banjis = banjis
     self.db = Db(settings.BASE_DATABASE)  # 数据库连接
     self.db_path = self.db.db_handler()
Example #7
0
class TaxModel(General):

    TABLE = "tax"

    def __init__(self):
        General.__init__(self)
        self.conn = self.pool.connection()
        self.cursor = self.conn.cursor(pymysql.cursors.DictCursor)
        self.db = Db(self.conn, self.cursor)

    def select(self):
        try:
            self.db.select(self.TABLE)
            return self.db.execute()
        except Exception:
            raise

    def insert(self, data):
        try:
            self.db.insert(self.TABLE, data)
            return self.db.execute()
        except Exception:
            raise
Example #8
0
class BaseDb(object):
    '''基础数据库类'''

    db = Db(settings.BASE_DATABASE)  # 数据库连接
    db_path = "%s/base.db" % db.db_handler()

    def __init__(self):
        '''
        初始基础数据课程和学校
        '''
        if os.path.exists(BaseDb.db_path):  # 初始数据库存在时,不做任何操作
            return None

        # 创建课程3门,linux、python、go课程
        go = Course("go", 8000)
        linux = Course("linux", 10800)
        python = Course("python", 8800)

        # 创建学校2所,北京老男孩、上海老男孩
        beijing_oldboy_school = School("beijing_oldboy_school", "beijing",
                                       {"teachers": []},
                                       {"courses": [linux, python]},
                                       {"students": []}, {"banjis": []})
        shanghai_oldboy_school = School("shanghai_oldboy_school", "shanghai",
                                        {"teachers": []}, {"courses": [go]},
                                        {"students": []}, {"banjis": []})
        base_data = {
            "schools": [beijing_oldboy_school, shanghai_oldboy_school],
        }
        self.save_data(base_data)

        admin_username = "******"
        admin_password = "******"
        status = 1
        authority = 8
        admin = Admin(admin_username, admin_password, status, authority)
        admin.create()

    def save_data(self, base_data):
        '''保存基础数据库
        :param base_data: 基础数据
        :return
        '''
        BaseDb.db.dump_pickle_data(BaseDb.db_path, base_data)  # 写入数据库
        return True

    def get_data(self):
        base_data = BaseDb.db.load_pickle_data(BaseDb.db_path)
        # 从数据库读取学员信息
        schools = base_data["schools"]
        attr_students = "students"
        attr_teachers = "teachers"
        for school in schools:
            students = getattr(school, attr_students)[attr_students]
            teachers = getattr(school, attr_teachers)[attr_teachers]
            for student in students:
                account_id = student.account.account_id
                student.account = student.account.get_account_data(account_id)
                user_info_dict = student.account.user_info
                student.name = user_info_dict.get("name")
                student.sex = user_info_dict.get("sex")
                student.age = user_info_dict.get("age")
            for teacher in teachers:
                account_id = teacher.account.account_id
                teacher.account = teacher.account.get_account_data(account_id)
                user_info_dict = teacher.account.user_info
                teacher.name = user_info_dict.get("name")
                teacher.sex = user_info_dict.get("sex")
                teacher.age = user_info_dict.get("age")
        return base_data
Example #9
0
 def __init__(self):
     General.__init__(self)
     self.conn = self.pool.connection()
     self.cursor = self.conn.cursor(pymysql.cursors.DictCursor)
     self.db = Db(self.conn, self.cursor)
Example #10
0
 def setUp(self):
     self.db = Db()
     self.db.execute('CREATE TABLE testtable ( id INTEGER PRIMARY KEY, name VARCHAR ) ')
     pass
Example #11
0
class TestDatabase(unittest.TestCase):
    def setUp(self):
        self.db = Db()
        self.db.execute('CREATE TABLE testtable ( id INTEGER PRIMARY KEY, name VARCHAR ) ')
        pass
    
    def test_can_create_db_object(self):
        db = Db()
        self.assertIsNotNone(db, "Database is not None")
        
    def test_can_execute_sql(self):
        self.db.execute('SELECT * FROM "main".sqlite_master')
        self.db.commit()
        
    def test_can_execute(self):
        self.db.execute("INSERT INTO testtable ( id, name ) VALUES ( null, 'name')")
        self.db.execute("INSERT INTO testtable ( id, name ) VALUES ( null, ?)", 'name')
        self.db.execute("INSERT INTO testtable ( id, name ) VALUES ( null, :name)", name='name')
        self.db.commit()
        
    def test_can_fetch_one_object_by_id(self):
        id = self.db.execute("INSERT INTO testtable ( id, name ) VALUES ( null, 'name')")
        self.db.commit()
        obj = self.db.one(TestObject, "SELECT * from testtable WHERE id=?", id)
        
    def test_can_fetch_many_objects(self):
        for x in range(1000,1010):
            self.db.execute("INSERT INTO testtable ( id, name ) VALUES ( null, :name)", name="name"+ str(x))
        
        self.db.commit()
        objs = self.db.many(TestObject, "SELECT * FROM testtable WHERE name LIKE :name", name="name1%")
        self.assertEqual(10, len(objs), '10==len(Returned)')
        
        counter = 1000
        for obj in objs:
            self.assertEqual(obj.name, 'name%s' % counter, 'Name matches')
            counter += 1
            
    def test_can_fetch_one_by_unnamed(self):
        id = self.db.execute("INSERT INTO testtable ( id, name ) VALUES ( null, 'name')")
        self.db.commit()
        fetched = self.db.one(TestObject, "SELECT * FROM testtable WHERE id=?", id)
        self.assertIsNotNone(fetched , 'fetched is not none')
        self.assertEqual(fetched.name, 'name', 'name==name')
        
    def test_can_fetch_one_by_named(self):
        id = self.db.execute("INSERT INTO testtable ( id, name ) VALUES ( null, 'name')")
        self.db.commit()
        fetched = self.db.one(TestObject, "SELECT * FROM testtable WHERE id=:id", id=id)
        self.assertIsNotNone(fetched , 'fetched is not none')
        self.assertEqual(fetched.name, 'name', 'name==name')
        
    def test_can_fetch_scalar(self):
        self.db.execute("INSERT INTO testtable ( id, name ) VALUES ( null, 'name')")
        self.db.execute("INSERT INTO testtable ( id, name ) VALUES ( null, 'name')")
        self.db.execute("INSERT INTO testtable ( id, name ) VALUES ( null, 'name')")
        self.db.commit()
        result = self.db.scalar("SELECT COUNT(*) FROM testtable")
        self.assertIsNotNone(result, "Scalar fetched")
        self.assertEqual(result, 3, "Count==3")
Example #12
0
class School(object):
    '''学校类'''
    def __init__(self,
                 school_name,
                 city_name,
                 teachers=None,
                 courses=None,
                 students=None,
                 banjis=None):
        '''
        定义学校属性
        :param school_name: 学校名,字符类型
        :param city_name: 城市名,字符类型
        :param teachers: 讲师,字典类型,如{"teachers": []}
        :param students: 学员, 字典类型,如{"students": []}
        :param courses: 课程,字典类型,如{"courses": []}
        :param banjis: 班级,字典类型,如{"banjis": []}
        '''
        self.school_name = school_name
        self.city_name = city_name
        self.teachers = teachers
        self.courses = courses
        self.students = students
        self.banjis = banjis
        self.db = Db(settings.BASE_DATABASE)  # 数据库连接
        self.db_path = self.db.db_handler()

    def add_attr_value(self, attr, list_value):
        '''学校添加属性的字典元素列表值
        :param attr: school类的属性
        :return:
        '''
        attr_value = getattr(self, attr)  # 字典类型
        attr_value_list = attr_value[attr]  # 列表类型
        if not list_value in attr_value_list:
            attr_value_list.append(list_value)
        else:
            print("{} already exist.".format(list_value.name))

        return self

    def show_info(self, attr=None):
        '''显示课程/讲师/学生/班级'''
        attr_list = ["courses", "teachers", "students", "banjis"]

        def show_attr_value(attr):
            attr_value = getattr(self, attr)  # 字典类型
            attr_value_list = attr_value[attr]  # 列表类型
            print("{} :".format(attr))
            for a in attr_value_list:  # 循环显示
                # a.show_info()
                if a.name:
                    print("\033[34;1m{}\033[0m".format(a.name))
                else:  # 显示人的帐户名
                    print("\033[34;1m{}\033[0m".format(a.account.user_name))

        if attr in attr_list:
            show_attr_value(attr)
        else:
            for attr in attr_list:
                show_attr_value(attr)

        return True

    def get_instance(self):
        '''根据名字获取对象'''
        attr_list = ["courses", "teachers", "students", "banjis"]
Example #13
0
class BaseDb(object):
    '''基础数据库类'''
    db = Db(settings.BASE_DATABASE)
    db_path = "%s/base.db" % db.db_handler()

    def __init__(self):
        '''
        初始化基础数据课程和学校
        '''
        if os.path.exists(BaseDb.db_path):  # 初始数据库存在时,不做任何操作
            return None

        # 创建课程
        java = Course("java", 8000)
        linux = Course("linux", 10300)
        python = Course("python", 12000)

        # 创建学校两所,北京An,上海An
        beijing_an_school = School("beijing_an_school", "beijing",
                                   {"teachers": []},
                                   {"courses": [java, linux]},
                                   {"students": []}, {"grades": []})
        shanghai_an_school = School("shanghai_an_school", "shanghai",
                                    {"teachers": []},
                                    {"courses": [java, python]},
                                    {"students": []}, {"grades": []})
        base_data = {"schools": [beijing_an_school, shanghai_an_school]}
        self.save_data(base_data)

        admin_username = '******'
        admin_password = '******'
        status = 1
        authority = 9
        admin = Admin(admin_username, admin_password, status, authority)
        admin.create()

    @staticmethod
    def save_data(base_data):
        '''
        保存基础数据库
        :param base_data:   基础数据
        :return:
        '''
        BaseDb.db.dump_pickle_data(BaseDb.db_path, base_data)

    @staticmethod
    def get_data():
        base_data = BaseDb.db.load_pickle_data(BaseDb.db_path)
        # 从文件中读取信息
        schools = base_data['schools']
        attr_students = 'students'
        attr_teachers = 'teachers'
        for school in schools:
            students = getattr(school, attr_students)[attr_students]
            teachers = getattr(school, attr_teachers)[attr_teachers]
            for student in students:
                account_id = student.account.account_id
                student.account = student.account.get_account_data(account_id)
                user_info_dict = student.account.user_info
                student.name = user_info_dict.get['name']
                student.sex = user_info_dict.get['sex']
                student.age = user_info_dict.get['age']
            for teacher in teachers:
                account_id = teacher.account.account_id
                teacher.account = teacher.account.get_account_data(account_id)
                user_info_dict = teacher.account.user_info
                teacher.name = user_info_dict.get['name']
                teacher.sex = user_info_dict.get['sex']
                teacher.age = user_info_dict.get['age']
        return base_data
Example #14
0
def db_data():
    """Return data from a table in the database"""
    db = Db()
    data = db.read_data(sys.argv[2])
    for returned in data:
        print returned
Example #15
0
def db_tables():
    """Return tables in database"""
    db = Db()
    tables = db.read_tables()
    for table in tables:
        print table
Example #16
0
from lib.logger import logger
from lib.db import Db
import concurrent.futures
from urllib.parse import urlparse
from worq import get_broker, TaskSpace, get_queue
from worq.pool.thread import WorkerPool
import time

ts = TaskSpace("tasks")
visited_links = []
downloaded_links = []
lock = Lock()
lock2 = Lock()
parsed = urlparse(args.url)

Db.create_db(parsed.netloc, args.force)
fs = FileStorage(args.url, args.output)


def run_in_threads(data, action, thread_count):
    results = {}
    if not data or len(data) == 0:
        return results

    with concurrent.futures.ThreadPoolExecutor(
            max_workers=thread_count) as executor:
        # Start the load operations and mark each future with its URL
        future_to_url = {executor.submit(action, url): url for url in data}
        for future in concurrent.futures.as_completed(future_to_url):
            url = future_to_url[future]
            result = ''
Example #17
0
class Account(object):
    '''
    账号信息
    '''
    db = Db(settings.ACCOUNT_DATABASE)  # 数据库连接,公共属性
    db_path = db.db_handler()

    def __init__(self,
                 user_name,
                 password,
                 status=settings.STATUS['normal'],
                 authority=settings.AUTHORITY['student'],
                 user_info={}):
        '''
        定义账号属性
        :param user_name:   用户名,字符属性
        :param password:    密码,字符属性
        :param status:      账号状态,整数类型
        :param authority:   账号权限,整数类型
        :param user_info:   用户信息,字典类型
        '''
        self.user_name = user_name
        self.password = password
        self.status = status
        self.authority = authority
        self.user_info = user_info

    def show_info(self):
        '''查看user_info属性'''
        print("account_id: \033[32;1m{}\033[0m".format(self.account_id))
        if self.user_info:
            for k in self.user_info:
                print("{}: \033[32;1m{}\033[0m".format(k, self.user_info[k]))

    def create(self):
        '''
        创建新账号
        :return: 账户实例
        '''
        # 获取自增长id
        begin_id = 10000
        auto_increment_file = "%s/increment_id" % os.path.dirname(
            Account.db_path)
        if os.path.exists(auto_increment_file):
            line = Account.db.load_pickle_data(auto_increment_file)
            auto_increment_id = int(line) + 1
        else:  # 自增长id文件不存在
            id_list = []
            files_list = os.listdir(Account.db_path)
            for f in files_list:
                exist_flag = re.match(r'^(\d{5,})', f)
                if exist_flag:
                    id_list.append(f)

            if id_list:
                id_list.sort()
                max_id = int(id_list[-1])
                auto_increment_id = max_id + 1
            else:
                auto_increment_id = begin_id

        self.account_id = auto_increment_id
        check_flag = self.check_user_name(auto_increment_id)
        if not check_flag:  # 用户名不存在,才创建用户
            user_file = "%s/%s" % (Account.db_path, auto_increment_id)
            Account.db.dump_pickle_data(auto_increment_file, auto_increment_id)
            Account.db.dump_pickle_data(user_file, self)
            return self
        else:
            print("User name [\033[31;1m%s\033[0m] has been registered!" %
                  self.user_name)
            return False

    def check_user_name(self, account_id):
        '''
        查询数据库用户名是否存在
        :param account_id:  用户名,字符类型
        :return:  True 存在,False 不存在
        '''
        user_names = []  # 初始化用户名数据库
        exist_flag = False  # 初始存在标记
        user_names_file = "%s/user_names" % Account.db_path
        if os.path.exists(user_names_file):
            user_names = Account.db.load_pickle_data(user_names_file)
            for usr in user_names:
                if usr["user_name"] == self.user_name:
                    exist_flag = usr["account_id"]
                    return exist_flag

        user_names.append({
            "account_id": account_id,
            "user_name": self.user_name
        })
        Account.db.dump_pickle_data(user_names_file, user_names)

    @staticmethod
    def get_account_data(account_id):
        '''
        获取账户数据
        :param account_id: 账户id
        :return:
        '''
        exist_flag = False
        user_file = "%s/%s" % (Account.db_path, account_id)
        if os.path.exists(user_file):
            account = Account.db.load_pickle_data(user_file)
            return account
        print("Account [\033[31;1m%s\033[0m] does not exist!" % account_id)
        return exist_flag

    @staticmethod
    def get_account_id(user_name):
        '''
        用user_name查询出account_id
        :param user_name:
        :return:
        '''
        exist_flag = False
        user_name_file = "%s/user_names" % Account.db_path
        if os.path.exists(user_name_file):
            user_names = Account.db.load_pickle_data(
                user_name_file)  # 获取用户名:id 的列表数据
            for u_n in user_names:
                if u_n['user_name'] == user_name:
                    exist_flag = u_n['account_id']
                    return exist_flag

        print("User name [\033[31;1m%s\033[0m] does not exist!" % user_name)
        return exist_flag

    def save_data(self, account_id):
        '''保存数据至数据库
        :param account_id: 帐户id
        '''
        user_file = "%s/%s" % (Account.db_path, account_id)
        Account.db.dump_pickle_data(user_file, self)  # 保存帐户数据

    def login(self):
        '''
        用户输入的是账号,通过账号获取account_id验证登录
        :return:    账号实例
        '''
        account_id = self.get_account_id(self.user_name)
        if account_id:
            account = self.get_account_data(account_id)
            if account.password == self.password:
                if self.status == settings.STATUS['normal']:
                    return account
                else:
                    print(
                        "Account locked, [\033[31;1m%s\033[0m] sign in failed!"
                        % self.user_name)
                    return False
            else:
                print("Password error, [\033[31;1m%s\033[0m] sign in failed!" %
                      self.user_name)
                return False
        else:
            print("User name [\033[31;1m%s\033[0m] does not exist!" %
                  self.user_name)
            return False
Example #18
0
class TestTermService(unittest.TestCase):
    def setUp(self):
        self.db = Db(Application.connectionString)
        self.userService = UserService()
        self.languageService = LanguageService()
        self.termService = TermService()
        self.user = User()
        self.user.username = str(uuid.uuid1())
        self.user = self.userService.save(self.user)
        self.language = Language()
        self.languageService.save(self.language)
        
        Application.user = self.user
         
    def test_can_get_term_by_id(self):
        term = Term()
        term.languageId = self.language.languageId
        original = self.termService.save(term)
        saved = self.termService.findOne(original.termId)
        self.assertEqual(original.termId, saved.termId, "Saved termId==Original termId")
        
    def test_can_update_term(self):
        term = Term()
        term.languageId = self.language.languageId
        term = self.termService.save(term)
        self.assertEqual(term.basePhrase, "", "Name is empty")
        time.sleep(0.5)
        term.basePhrase = 'Bob'
        original = self.termService.save(term)
        saved = self.termService.findOne(original.termId)
        self.assertEqual(saved.basePhrase, term.basePhrase, "Updated basePhrase")
        self.assertGreater(saved.modified, term.created, "Modified>Created")
        
    def test_can_delete_term(self):
        term = Term()
        term.languageId = self.language.languageId
        term = self.termService.save(term)
        self.assertGreater(term.termId, 0, "Term is saved")
        self.termService.delete(term.termId)
        saved = self.termService.findOne(term.termId)
        self.assertIsNone(saved, "Term is deleted")
        
    def test_can_get_term_by_phrase_and_language(self):
        term = Term()
        term.phrase = "PHRASE"
        term.languageId = self.language.languageId
        original = self.termService.save(term)
        saved = self.termService.fineOneByPhraseAndLanguage("Phrase", self.language.languageId)
        self.assertEqual(original.termId, saved.termId, "Saved termId==Original termId")
        
    def test_can_save_term(self):
        term = Term()
        term.languageId = self.language.languageId
        original = self.termService.save(term)      
        saved = self.termService.findOne(original.termId)
        
        self.assertIsNotNone(term, "term is not None")
        self.assertGreater(saved.termId, 0, "termId is >0")
        self.assertEqual(original.termId, saved.termId, "Saved PK is retrieved PK")
        self.assertEqual(saved.userId, Application.user.userId, "term has Application UserId")
        self.assertGreaterEqual(saved.created, saved.modified, "Created and modified udpated")

    def test_term_log_entry_is_added_with_create(self):
        term = Term()
        term.languageId = self.language.languageId
        term = self.termService.save(term)      
        result = self.db.scalar("SELECT COUNT(*) as count FROM termlog WHERE termId=:termId AND type=:type", termId=term.termId, type=TermType.Create)
        self.assertEqual(result, 1, "Create entry found")
        
    def test_term_log_entry_is_added_with_update(self):
        term = Term()
        term.languageId = self.language.languageId
        term = self.termService.save(term)      
        result = self.db.scalar("SELECT COUNT(*) as count FROM termlog WHERE termId=:termId AND type=:type", termId=term.termId, type=TermType.Modify)
        self.assertEqual(result, 0, "Modify entry not found")
        term = self.termService.save(term)      
        result = self.db.scalar("SELECT COUNT(*) as count FROM termlog WHERE termId=:termId AND type=:type", termId=term.termId, type=TermType.Modify)
        self.assertEqual(result, 1, "Modify entry found")      
        
    def test_term_log_entry_is_added_with_delete(self):
        term = Term()
        term.languageId = self.language.languageId
        term = self.termService.save(term)      
        result = self.db.scalar("SELECT COUNT(*) as count FROM termlog WHERE termId=:termId AND type=:type", termId=term.termId, type=TermType.Delete)
        self.assertEqual(result, 0, "Delete entry not found")
        self.termService.delete(term.termId)      
        result = self.db.scalar("SELECT COUNT(*) as count FROM termlog WHERE termId=:termId AND type=:type", termId=term.termId, type=TermType.Delete)
        self.assertEqual(result, 1, "Delete entry found")
Example #19
0
class Account(object):
    '''
    帐号类
    '''
    db = Db(settings.ACCOUNT_DATABASE)  # 数据库连接,公共属性
    db_path = db.db_handler()

    def __init__(self,
                 user_name,
                 password,
                 status=settings.STATUS['normal'],
                 authority=settings.AUTHORITY['student'],
                 user_info={}):
        '''
        定义帐号属性
        :param user_name: 用户名, 字符类型
        :param password: 密码, 字符类型
        :param status: 帐号状态和帐号权限,数字整型
        :param authority: 帐号权限, 数字整型
        :param user_info: 用户信息, 字典类型
        '''
        self.user_name = user_name
        self.password = password
        self.status = status  # 0为锁定用户,1为正常
        self.authority = authority  # 1为普通用户(学员),2为讲师,8为admin用户
        self.user_info = user_info  # 学员、讲师、学校信息

    def show_info(self):
        '''查看user_info属性'''
        print("account_id: \033[32;1m{}\033[0m".format(self.account_id))
        if self.user_info:
            for k in self.user_info:
                print("{}: \033[32;1m{}\033[0m".format(k, self.user_info[k]))

    def create(self):
        '''
        创建新帐号
        :return: 返回帐户实例(自己))
        '''
        # 获取自增长id
        begin_id = 10000
        auto_increment_file = "%s/increment_id" % os.path.dirname(
            Account.db_path)
        if os.path.exists(auto_increment_file):  # 自增长id文件存在时
            line = Account.db.load_pickle_data(auto_increment_file)
            auto_increment_id = int(line) + 1
        else:  # 自增长id文件不存在时
            id_list = []  # 帐户数据文件
            files_list = os.listdir(Account.db_path)
            for f in files_list:
                exist_flag = re.match(r'^(\d{5,})', f)
                if exist_flag:  # 有帐户存在时
                    id_list.append(f)

            if id_list:
                id_list.sort()
                max_id = int(id_list[-1])
                auto_increment_id = max_id + 1
            else:
                auto_increment_id = begin_id  # 帐户数据文件不存在

        self.account_id = auto_increment_id  # 帐户id
        check_flag = self.check_user_name(auto_increment_id)  # 检查用户名
        user_file = "%s/%s" % (Account.db_path, auto_increment_id)
        if not check_flag:  # 用户名不存在,才创建用户
            Account.db.dump_pickle_data(auto_increment_file,
                                        auto_increment_id)  # 保存自增加id
            Account.db.dump_pickle_data(user_file, self)  # 保存帐户数据
            return self
        else:
            print("User name [\033[31;1m%s\033[0m] has been registered!" %
                  self.user_name)
            return False

    def check_user_name(self, account_id):
        '''
        用account_id检查用户名是否存在于数据库
        :param user_name: 用户名,字符类型
        :return: 存在则为真,否则为假
        '''
        user_names = []  # 初始化用户名数据库
        exist_flag = False  # 初始化存在标记
        user_names_file = "%s/user_names" % Account.db_path
        if os.path.exists(user_names_file):
            user_names = Account.db.load_pickle_data(user_names_file)

            for u_n in user_names:
                if u_n["user_name"] == self.user_name:  # 存在
                    exist_flag = u_n["account_id"]
                    return exist_flag

        user_names.append({
            "account_id": account_id,
            "user_name": self.user_name
        })
        result = Account.db.dump_pickle_data(user_names_file, user_names)

        return exist_flag

    def get_account_id(self, user_name):
        '''
        用user_name查询出account_id
        :return:
        '''
        exist_flag = False  # 初始化存在标记
        user_names_file = "%s/user_names" % Account.db_path
        if os.path.exists(user_names_file):
            user_names = Account.db.load_pickle_data(user_names_file)  # 列表类型

            for u_n in user_names:
                if u_n["user_name"] == user_name:  # 存在
                    exist_flag = u_n["account_id"]
                    return exist_flag

        # print("User name [\033[31;1m%s\033[0m] does not exist!" % user_name)
        return exist_flag

    def get_account_data(self, account_id):
        '''获取帐户数据
        :param account_id: 帐户id
        '''
        exist_flag = False  # 初始化存在标记
        user_file = "%s/%s" % (Account.db_path, account_id)
        if os.path.exists(user_file):
            account = Account.db.load_pickle_data(user_file)

            account_data = {
                "account_id": account_id,
                "user_name": self.user_name,
                "password": self.password,
                "status": self.status,
                "authority": self.authority,
                "user_info": self.user_info
            }
            return account

        # print("Account [\033[31;1m%s\033[0m] does not exist!" % account_id)
        return exist_flag

    def save_data(self, account_id):
        '''保存数据至数据库
        :param account_id: 帐户id
        '''
        user_file = "%s/%s" % (Account.db_path, account_id)
        Account.db.dump_pickle_data(user_file, self)  # 保存帐户数据
        return True

    def login(self):
        '''登录
        因为用户输入的是用户名,需要转化成account_id来验证
        '''
        account_id = self.get_account_id(self.user_name)  # 获取account_id
        if account_id:
            account_data = self.get_account_data(account_id)
            user_file = "%s/%s" % (Account.db_path, account_id)
            if os.path.exists(user_file):
                account = Account.db.load_pickle_data(user_file)  # 获取用户帐户

                if account.password == self.password:  # 验证密码
                    if self.status == settings.STATUS['normal']:
                        return account  # 返回帐户实例
                    else:
                        print(
                            "Account locked,[\033[31;1m%s\033[0m] sign in failed!"
                            % self.user_name)
                else:
                    print(
                        "Password error,[\033[31;1m%s\033[0m] sign in failed!"
                        % self.user_name)
                    return False
            else:
                print("Account [\033[31;1m%s\033[0m] data exception!" %
                      account_id)
        else:
            print("User name [\033[31;1m%s\033[0m] does not exist!" %
                  self.user_name)
Example #20
0
class Account(object):
    '''
    账号类
    '''
    db = Db(settings.ACCOUNT_DATABASE)  #数据库连接,公共属性
    db_path = db.db_handler()

    def __init__(self,
                 user_name,
                 password,
                 status=settings.STATUS['normal'],
                 authority=settings.AUTHORITY['student'],
                 user_info={}):
        '''
        定义账号属性
        :param user_name: 用户名,字符类型 
        :param password: 密码,字条类型
        :param status: 账号状态和账号账号权限,数字整形
        :param authority: 账号权限,数字整型
        :param user_info: 用户信息,字典类型
        '''
        self.user_name = user_name
        self.password = password
        self.status = status  #0为锁定用户,1为正常
        self.authority = authority  #1为普通学员,2为讲师,8为admin用户
        self.user_info = user_info  #学员、讲师、学校信息

    def show_info(self):
        '''查看user_info属性'''
        print("account_id: \033\[32;1m{}\033[0m".format(self.account_id))
        if self.user_info:
            for k in self.user_info:
                print("{}: \033\[32;1m{}\033[0m".format(k, self.user_info[k]))

    def create(self):
        '''
        创建新账号
        :return: 返回账户实例(自己)
        '''
        #获取自增长ID
        begin_id = 10000
        auth_increment_file = "%s/increment_id" % os.path.dirname(
            Account.db_path)
        if os.path.exists(auth_increment_file):  #自增长id文件存在时
            line = Account.db.load_pickle_date(auth_increment_file)
            auth_increment_id = int(line) + 1
        else:  #自增长id文件不存在时
            id_list = []  #账户数据文件
            files_list = os.listdir(Account.db_path)
            for f in files_list:
                exist_flag = re.match(r'^(\d{5,})', f)
                if exist_flag:  #有账号存在时
                    id_list.append(f)

            if id_list:
                id_list.sort()
                max_id = int(id_list[-1])
                auto_increment_id = max_id + 1
            else:
                auto_increment_id = begin_id  #账号数据不存在

        self.account_id = auto_increment_id  #账号ID
        check_flag = self.check_user_name(auto_increment_id)  #检查用户名是否存在
        user_file = "%s/%s" % (Account.db_path, auto_increment_id)
        if not check_flag:  #用户名不存在,才创建用户
            Account.db.dump_pickle_data(auth_increment_file, auth_increment_id)
            Account.db.dump_pickle_data(user_file, self)  #保存账户数据,后面的self暂不是很明白
            return self
        else:
            print("User name [\033[31;1m%s\033[0m] has been registered!" %
                  self.account_id)
            return self

    def check_user_name(self, account_id):
        '''
        用account_id 检查用户名是否存在于数据库
        :param account_id:  用户名,字符类型
        :return: 存在则为真,否则为假
        '''
        user_names = []  # 初始化用户名数据库
        exist_flag = False  # 初始化存在标记
        user_names_file = "%s/user_names" % Account.db_path
        if os.path.exists(user_names_file):
            user_names = Account.db.load_pickle_date(user_names_file)

            for u_n in user_names:
                if u_n["user_name"] == self.user_name:  #存在
                    exist_flag = u_n["account_id"]
                    return exist_flag

        user_names.append({
            "account_id": account_id,
            "user_names": self.user_name
        })
        result = Account.db.dump_pickle_data(user_names_file, user_names)

        return exist_flag

    def get_account_id(self, user_name):
        '''
        用user_name 查询出account_id
        :param user_name: 
        :return: 
        '''
        exist_flag = False  #初始化存在标记
        user_names_file = "%s/user_names" % Account.db_path
        if os.path.exists(user_names_file):
            user_names = Account.db.load_pickle_date(user_names_file)

            for u_n in user_names:
                if u_n["user_name"] == user_name:  #存在
                    exist_flag = u_n["account_id"]
                    return exist_flag
        return exist_flag

    def get_account_data(self, account_id):
        '''
        获取账号数据
        :param account_id: 账户id
        :return: 
        '''
        exist_flag = False  #初始化存在标记
        user_file = "%s/%s" % (Account.db_path, account_id)
        if os.path.exists(user_file):
            account = Account.db.load_pickle_date(user_file)

            account_data = {
                "account_id": account_id,
                "user_name": self.user_name,
                "password": self.password,
                "status": self.status,
                "authority": self.authority,
                "user_info": self.user_info
            }
            return account
        return exist_flag

    def save_data(self, account_id):
        '''
        保存数据到数据库
        :param account_id: 账户id 
        :return: 
        '''
        user_file = "%s/%s" % (Account.db_path, account_id)
        Account.db.dump_pickle_data(user_file, self)  #保存账号数据
        return True

    def login(self):
        '''
        登录
        因为用户名输入的是用户名,需要转化成account_id来验证
        :return: 
        '''
        account_id = self.get_account_data(self.user_name)
        if account_id:
            account_data = self.get_account_data(account_id)
            user_file = "%s/%s" % (Account.db_path, account_id)
            if os.path.exists(user_file):
                account = Account.db.load_pickle_date(user_file)  #获取用户账号

                if account.password == self.password:  #验证密码
                    if self.status == settings.STATUS['normal']:
                        return account  #返回账号实例
                    else:
                        print(
                            "Account locked,[\033[31;1m %s \033[0m] sign in failed !"
                            % self.user_name)
                else:
                    print(
                        "Password error,[\033[31;1m %s \033[0m] sign in failed !"
                        % self.user_name)
                    return False
            else:
                print("Account [\033[31;1m %s \033[0m] data exception " %
                      account_id)
        else:
            print("User name [\033[31;1m %s \033[0m]" % self.user_name)