如何阻止Pyrebase set()覆盖现有数据?

时间:2020-07-31 00:05:50

标签: python firebase flask firebase-realtime-database pyrebase

我正在用Flask&Pyrebase创建一个简单的目录;我已经成功创建了登录名和注册表格。登录并注册后,用户将被重定向到其个人资料页面。

我遇到的问题是,当我尝试添加新条目时,现有数据将被覆盖。当我创建一个新的数据库条目时,我使用set()而不是push(),这是为什么呢?

我已经尽可能地缩短了代码

创建帐户和注册业务

@app.route('/register', methods=['GET', 'POST'])
def register():
unsuccessful = 'Please check your credentials'
successful = 'Registraion successful'
    if request.method == 'POST':
        email = request.form.get('email')
        confirmEmail = request.form('confirmEmail')
        password = request.form.get('pass')
        userName = request.form.get('inputName')
        businessName = request.form.get('businessName')
        startYear = request.form.get('startYear')
        selectCategory = request.form.get('selectCategory')
        businessDescription = request.form.get('businessDescription')
        businessAddress = request.form.get('businessAddress')
        businessTown = request.form.get('businessTown')
        businessCity = request.form.get('businessCity')
        businessPostcode = request.form.get('businessPostcode')
        businessNumber = request.form.get('businessNumber')
        businessEmail = request.form.get('businessEmail')
        bameRegister = dict(
            userName = userName,
            confirmationEmail = confirmEmail,
            businessName = businessName,
            businessStartYear = startYear,
            businessCategory = selectCategory,
            businessDescription = businessDescription,
            businessAddress = [businessAddress, businessTown, businessCity, businessPostcode],
            businessNumber = businessNumber,
            businessEmail = businessEmail,
        )
        if selectCategory == "arts":
            try:
                user = auth.create_user_with_email_and_password(email, password)
                auth.send_email_verification(user['idToken'])
                db.child("Bame_Buisness").child("business").child("arts").set(bameRegister, user['idToken'])
                return render_template('homepage.html', x=successful)
            except:
                return render_template('homepage.html', y=unsuccessful)
        elif selectCategory == "food":
            try:
                user = auth.create_user_with_email_and_password(email, password)
                auth.send_email_verification(user['idToken'])
                db.child("Bame_Business").child("business").child("food").set(bameRegister, user['idToken'])
                return render_template('homepage.html', x=successful)
            except:
                return render_template('homepage.html', y=unsuccessful)
       elif selectCategory == "health":
           try:
               user = auth.create_user_with_email_and_password(email, password)
               auth.send_email_verification(user['idToken'])
               db.child("Bame_Business").child("business").child("health").set(bameRegister, user['idToken'])
               return render_template('homepage.html', x=successful)
            except:
               return render_template('homepage.html', y=unsuccessful)
    return render_template('homepage.html')

创建登录名:

@app.route('/login', methods=['GET', 'POST'])
def login():
    unsuccessful = 'Please check your credentials'
    if request.method == 'POST':
        try:
            user = auth.sign_in_with_email_and_password(email, password)
            signed_in_user = auth.get_account_info(user['idToken'])
            artCategory = db.child("Bame_Business").child("business").child("arts").get(user['idToken']).val()
            foodCategory = db.child("Bame_Business").child("business").child("food").get(user['idToken']).val()
            healthCategory = db.child("Bame_Business").child("business").child("health").get(user['idToken']).val()
            if signed_in_user['users'][0]['email'] == artCategory['confirmationEmail']:
                return render_template('profile.html', artCategory=artCategory)
            elif signed_in_user['users'][0]['email'] == foodCategory['confirmationEmail']:
                return render_template('profile.html', foodCategory=foodCategory)
            elif signed_in_user['users'][0]['email'] == healthCategory['confirmationEmail']:
                return render_template('profile.html', healthCategory=heathCategory)
        except:
            return render_template('homepage.html')
    return render_template('homepage.html')

这是登录后创建新业务的途径:

@app.route('newBusiness', methods=['GET', 'POST'])
def newBusiness():
    if request.method == 'POST':
        email = request.form.get('email')
        confirmEmail = request.form.get('confirmEmail')
        password = request.form.get('pass')
        userName = request.form.get('userName')
        businessName = request.form.get('businessName')
        startYear = request.form.get('startYear')
        selectCategory = request.form.get('selectCategory')
        businessDescription = request.form.get('businessDescription')
        businessAddress = request.form.get('businessAddress')
        businessTown = request.form.get('businessTown')
        businessCity = request.form.get('businessCity')
        businessPostcode = request.get('businessPostcode')
        businessNumber = request.form.get('businessNumber')
        businessEmail = request.form.get('businessEmail')
        bameRegister = dict(
            userName = userName,
            confirmationEmail = confirmEmail,
            businessName = businessName,
            businessStartYear = startYear,
            businessCategory = businessCategory,
            businessDescription = businessDescription,
            businessAddress = [businessAddress, businessTown, businessCity, businessPostcode],
            businessNumber = businessNumber,
            businessEmail = businessEmail,
        )
        if selectCategory == "arts":
            try:
                user = auth.sign_in_with_email_and_password(email, password)
                user = auth.refresh(user['refreshToken'])
                db.child("Bame_Business").child("business").child("arts").set(bameRegister, user['idToken'])
                signed_in_user = auth.get_account_info(user['idToken'])
                artCategory = db.child("Bame_Business").child("business").child("arts").get(user['idToken']).val()
                if signed_in_user['users'][0]['email'] == artCategory['confirmationEmail']:
                   return render_template('profile.html', artCategory=artCategory)
            except:
                return render_template('homepage.html')
        elif selectCategory == "food":
            try:
                user = auth.sign_in_with_email_and_password(email, password)
                user = auth.refresh(user['refreshToken'])
                db.child("Bame_Business").child("business").child("food").set(bameRegister, user['idToken'])
                signed_in_user = auth.get_account_info(user['idToken'])
                foodCategory = db.child("Bame_Business").child("business").child("food").get(user['idToken']).val()
                if signed_in_user['users'][0]['email'] == foodCategory['confirmationEmail']:
                    return render_template('profile.html', foodCategory=foodCategory)
            except:
                return render_template('homepage.html')
        elif selectCategory == "health":
            try:
                user = auth.sign_in_with_emal_and_password(email, password)
                user = auth.refresh(user['refreshToken'])
                db.child("Bame_Business").child("business").child("health").set(bameRegister, user['idToken'])
                signed_in_user = auth.get_account_info(user['idToken'])
                healthCategory = db.child("Bame_Business").child("business").child("health").get(user['idToken']).val()
                if signed_in_user['users'][0]['email'] == healthCategory['confirmationEmail']:
                    return render_template('profile.html', healthCategory=healthCategory)
            except:
                return render_template('homepage.html')
    return render_template('homepage.html')

用户登录后,可以说他们在艺术类别中注册了第一项业务,当他们在艺术类别中注册了第二项不同的业务时,原始集合在Firebase中被覆盖。即使选择其他类别,也会发生这种情况。这绝对不理想,因为有人可能会覆盖他人的数据-从而导致各种问题!

我了解您可以使用push&set在pyrebase中创建新条目;我选择set的原因是,当我使用push时,它会创建一个唯一的标签,我发现很难从用户帐户中获得单独的访问权限。使用set方法可以很容易地用artCategory['businessName']artCategory['businessDescription']artCategory['confirmationEmail']等调用我想要的字典部分

如果有人可以帮助我提出一个非常值得赞赏的解决方案,我只需在登录后创建一个新的商业注册而不覆盖任何现有数据即可。

亲切的问候

1 个答案:

答案 0 :(得分:0)

library(plotly) fig <- plot_ly(ds, x = ~Date) fig <- fig %>% add_lines(y = ~AAPL.Adjusted, name = "Apple") fig <- fig %>% add_lines(y = ~MSFT.Adjusted, name = "Microsoft") rangeselector = list( buttons = list( list( count = 3, label = "3 mo", step = "month", stepmode = "backward"), list( count = 6, label = "6 mo", step = "month", stepmode = "backward"), list( count = 1, label = "1 yr", step = "year", stepmode = "backward"), list( count = 1, label = "YTD", step = "year", stepmode = "todate"))) xaxis = list(rangeselector = rangeselector, rangeslider = list(type = "date")) fig <- fig %>% layout( title = "Stock Prices", updatemenus = list( list( active = -1, type = 'buttons', buttons = list( list( label = '2018', method = "relayout", args = list(list(xaxis = list(range = as.POSIXct(c("2018-01-01","2018-12-31"), origin= "1970-1-1"), rangeselector = rangeselector, rangeslider = list(type = "date"))))), list( label = '2019', method = "relayout", args = list(list(xaxis = list(range = as.POSIXct(c("2019-01-01","2019-12-31"), origin= "1970-1-1"), rangeselector = rangeselector, rangeslider = list(type = "date"))))), list( label = 'All', method = "relayout", args = list(list(xaxis = list(range = as.POSIXct(c("2018-01-01","2019-12-31"), origin= "1970-1-1"), rangeselector = rangeselector, rangeslider = list(type = "date"))))) ) ) ), xaxis = xaxis, yaxis = list(title = "Price")) fig 覆盖。 .set()仅更新已更改的字段。