Как я могу кэшировать имя пользователя и логин для отображения в классе LoginSuccess? - PullRequest
0 голосов
/ 19 июня 2020

Я попытался импортировать объект кэша kivy, добавить ключ и объект имени пользователя и получить его в классе LoginSuccess, но, похоже, он не добавляет объект, когда я добавляю его в класс LoginScreen. Когда он не добавлен в класс, он работает нормально. Как мне go решить эту проблему?

    """Logic of the program resides. """
from kivy.uix.label import Label
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.cache import Cache
from datetime import datetime
import json
import hashlib
import glob
from pathlib import Path
import random

# Build the kivy design file
Builder.load_file('design.kv')
# register a new Cache
Cache.register('design')


# create an object + id
# key = 'objectid'
# instance = Label(text=text)
# Cache.append('mycache', key, instance)

# retrieve the cached object
# instance = Cache.get('mycache', key)
# Every rule in the kivy file has to be represented as a class in the main python file
class LoginScreen(Screen):
    def sign_up(self):
        # access the sign up screen by the name
        self.manager.current = "sign_up_screen"
        print("sign up button pressed...")
    def login(self,username,password): 
        # open up json file 
        key = 'username'
        use = Label(text='jaja')
        Cache.append('design', key, use)
        with open('users.json') as file:
            users = json.load(file)
            # check for username present, if not present show login failed
            if username in users:
                global key_username
                key_username = username
                p_s= str.encode(password)
                # hash password
                p_s_hash = hashlib.sha256(p_s)
                hex_dig = p_s_hash.hexdigest()
                if hex_dig == users[username]["password"]:
                    # create an object + id
                    key = 'username'
                    use = Label(text=username)
                    Cache.append('design', key, use)

                    self.manager.current = "login_success"

                else:
                    self.ids.wrong_login.text = "Incorrect username or password please try again"
            else:
                self.ids.wrong_login.text = "Incorrect username or password please try again"
        # compare decrypted password with password passed in if matches switch to login success page
        # create an object + id
        key = 'username'
        use = Label(text=username)
        Cache.append('design', key, use)
        # else show login failed

class LoginSuccessScreen(Screen):
    def __init__(self,**kwargs):
        super(Screen, self).__init__(**kwargs)
        # retrieve from cache
        username = Cache.get('design', 'username',default="username")
        print(username)
        # if username is None:
        #     print("No username present")
        # else:
        #     welcome_text = Label(pos=(0,250), text="Welcome "+username+ "!")
        #     self.add_widget(welcome_text)
    def log_out(self):
        self.manager.transition.direction = "right"
        self.manager.current = "login_screen"
    def get_quotes(self, feel):
        feel = feel.lower()
        available_feelings = glob.glob("quotes/*.txt")
        # acquire available feelings from quote directory
        print(available_feelings)
        available_feelings = [Path(filename).stem for filename in available_feelings]

        # check if feeling is in available_feelings
        if feel in available_feelings:
            with open(f"quotes/{feel}.txt") as file:
                quotes = file.readlines()
                quote = random.choice(quotes)
                self.ids.feeling_result.text = quote

        else:
            self.ids.feeling_result.text = "Not found :("


class RootWidget(ScreenManager):
    pass

class SignUpScreen(Screen):
    # add username 
    def add_user(self, username, password):
        print("username",username,"password",password)
        with open('users.json') as file:
            users = json.load(file)
            password_bytes = hashlib.sha256(str.encode(password))
            hex_dig = password_bytes.hexdigest()
            users[username] = {"username": username, "password": hex_dig, "created": datetime.now().strftime("%Y-%m-%d %H:%M:%S")}
            print(users)

            #overwrite json file
            with open('users.json','w') as file:
                json.dump(users,file)

            # switch to success screen 
            self.manager.current = "sign_up_success"
class SignUpSuccessScreen(Screen):
    def login(self):
        self.manager.current = "login_screen"

class MainApp(App):
    def build(self):
        return RootWidget()

if __name__ =="__main__":
    MainApp().run()

Ниже мой файл дизайна kivy:

<LoginScreen>:
    GridLayout:
        cols: 1
        GridLayout:
            cols: 1

            Label: 
                text: "User Login"
            TextInput: 
                id: username
                hint_text: "Username"
            TextInput: 
                id: password
                hint_text: "Password"
                password: True
            Button:
                text: "Login"
                on_press: root.login(root.ids.username.text,root.ids.password.text)
            Label:
                id: wrong_login
                text: ""
        GridLayout:
            cols: 2

            Button:
                text: "Sign Up"
                on_press: root.sign_up()
            Button: 
                text: "Forgot Password?"
<SignUpScreen>:
    GridLayout:
        cols: 1
        Label:
            text: "Sign up for a free account"
        TextInput:
            id: username
            hint_text: "Username"
        TextInput:
            id: password
            hint_text: "Password"
        Button:
            text: "Submit"
            on_press: root.add_user(root.ids.username.text, root.ids.password.text)
<SignUpSuccessScreen>:
    GridLayout:
        cols: 1
        Label:
            text: "Sign Up Successful!"
        Button:
            text: "Login"
            on_press: root.login()
<LoginSuccessScreen>:
    GridLayout:
        cols: 1
        Label:
            id: welcome

            text: ""
        Button:
            text: "Logout"
            on_press: root.log_out()
        Label:
            text: "How are you feeling?"
        TextInput:
            id: feeling
            hint_text: "Things to try: sad, unloved, happy"
        Button:
            text: "Enlighten me"
            on_press: root.get_quotes(root.ids.feeling.text)
        Label:
            id: feeling_result
            text: ""
            multiline: True
<RootWidget>:
    LoginScreen:
        name: "login_screen" 
    SignUpScreen:
        name: "sign_up_screen"
    SignUpSuccessScreen:
        name: "sign_up_success"
    LoginSuccessScreen:
        name: "login_success"

1 Ответ

0 голосов
/ 20 июня 2020

Cache работает правильно, но код:

    # retrieve from cache
    username = Cache.get('design', 'username',default="username")
    print(username)

выполняется в методе __init__() из LoginSuccessScreen, который запускается, когда

return RootWidget()

выполнено. Это задолго до того, как кто-либо сможет войти в систему. Вам просто нужно сделать Cache.get() в нужное время. Поэтому удалите извлечение из кеш-кода из __init__() и переместите его в метод on_enter() из LoginSuccessScreen:

class LoginSuccessScreen(Screen):
    def on_enter(self, *args):
        # retrieve from cache
        username = Cache.get('design', 'username',default="username")
        print('\tusername:', username.text)
...