3

I'm new to Python and KivyMD. Also to working with Databases. I want to check if the data provided by the user using the KivyMD App is already in the Firebase Realtime Database. These are the data in the firebase.

Data in Firebase

The Code

def send_data(self, email):
    from firebase import firebase

    firebase = firebase.FirebaseApplication("https://infinity-mode-default-rtdb.firebaseio.com/", None)

    data = {
        'Email' : email
    }

    if email.split() == []:
        cancel_btn_checkpoint_dialogue = MDFlatButton(text='Retry', on_release=self.close_checkpoint_dialogue)
        self.checkpoint_dialog = MDDialog(title='Access Denied', text="Invalid Username"),
                                          buttons=[cancel_btn_checkpoint_dialogue])

        self.checkpoint_dialog.open()

    else:
        firebase.post('Users', data)

If the user enters an existing value in the database, that value should not be saved in the database. Also a Dialog box should be shown that the email is already in use. If the value provided by the user is not in the database it should be saved. Please help me to do this.

Kusal Darshana
  • 159
  • 2
  • 18

3 Answers3

2

I'm guessing you use python-firebase which so far has very little documentation. So I'll try to do my best based on the package source...

You have to use firebase.get method to retrieve a dictionary with the current data stored for a given user, then check if that data contains the information you are going to add (untested, since firebase doesn't even import on my machine):

record = firebase.get('/Users', 'id')
if not record.get('Email'):
    firebase.post('/Users/'+'id', data)
Nux ツ
  • 146
  • 1
  • 7
  • @KusalDarshana What error does the code give? – Nux ツ Sep 13 '21 at 11:03
  • 1
    @KusalDarshana Not that I know of. You could also try with a different module, like https://pypi.org/project/firebase/ – Nux ツ Sep 13 '21 at 11:24
  • Sir, The value given by the user is saved again and again even if it has it. Are there any other ways? – Kusal Darshana Sep 13 '21 at 11:54
  • Sir, I couldn't understand anything. can you explain me a little more about this. – Kusal Darshana Sep 13 '21 at 13:48
  • @KusalDarshana I don't think I can Please have a look at this site to have a review of the Python language in general: https://www.learnpython.org/ I think that will help you a lot more than any of my answers. – Nux ツ Sep 14 '21 at 00:11
  • sir, I tried with the firebase documentation. I installed the package `Crypto` several times because I hadn't it. But there was an error occurred again and again, `NoModuleFoundError : no mudule found Crypto`. What should I do? – Kusal Darshana Sep 14 '21 at 02:06
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/237074/discussion-between-kusal-darshana-and-nux-). – Kusal Darshana Sep 14 '21 at 02:56
2

Did you try with firebase_admin?

I check my data with like this:

import firebase_admin
from firebase_admin import credentials
from firebase_admin import firestore

cred = credentials.Certificate(
        "firestore-cred.json"
    )
firebase_admin.initialize_app(cred)
db = firestore.client()
data = {
    "Email" : email
}

query_email = db.collection(u'Users') \
                .where(u"Email",u"==",data["Email"]) \
                .get()
if query_email:  
    # exists
    ...
else:
    # does not exist
    ...

If user email does not exist, query_email is going to be empty list. Do not forget that query_email is not json data. You can convert it json with to_dict():

email_query_result_as_json = query_email[0].to_dict()
Baris Senyerli
  • 642
  • 7
  • 13
  • Sir, What is that `db` in `db.collection(u'Users')`? is that `db = firebase.Database` or `from firebase_admin import db`? Or any other thing? – Kusal Darshana Sep 15 '21 at 05:26
  • 1
    @KusalDarshana it is `db = firestore.client()`. I added firestore but forgot intance of `db`. All is gonna well. Hope it helps. – Baris Senyerli Sep 15 '21 at 06:51
  • Thank you sir, It's working! However, an error is appeared (`ValueError: The default Firebase app already exists. This means you called initialize_app() more than once without providing an app name as the second argument. In most cases you only need to call initialize_app() once. But if you do want to initialize multiple apps, pass a second argument to initialize_app() to give each app a unique name.`) when the user enters an existing email address and enter another email as it is not valid. What can I do to prevent it? – Kusal Darshana Sep 15 '21 at 15:21
  • @KusalDarshana You need to use only 1 firebase lybrary. Choose one of them. `firebase_admin` or your old framework `firebase`. If it is work please check the answer and please open new question about it and share your code. It is not concern of this answer. – Baris Senyerli Sep 16 '21 at 07:04
  • 1
    Thank you very much sir for your kindness help... – Kusal Darshana Sep 16 '21 at 07:18
0

Try using the dataSnapshot.exist() method or the snapshot.hasChild method, they work in python, I'm not 100% sure whether they work in python but it is worth a go.

ChrisGPT was on strike
  • 127,765
  • 105
  • 273
  • 257