Access Firebase Realtime Database using Pyrebase - python

I have recently started with pyrebase and I am having trouble in storing data depending on user and receiving it. After completing the authentication part I create a user ID as user['idToken']
And then pushed data by using
archer = {"name": "Sterling Archer", "agency": "Figgis Agency"}
db.child("agents").push(archer, user['idToken'])
I assume that each user has different ID token which remains same even if we logout and relogin. but when I am using
all_agents = db.child("agents").get(user['idToken']).val()
print(all_agentes)
It's printing all data stored in realtime database. Even which were stored by other users.
I tried reading all it's documentation, but I was unable to understand how to handle it.
What's wrong that I am doing in here and How can I correct it?

Use user[localId] instead of user[idToken] as it will create database for every different user separately

Related

Can users access data you don't want on firebase?

I want to make an app that stores user data using firestore from firebase. My question is: Is there any possible way for a hacker to somehow access the database? I still want to be able to read/write/delete/edit from inside the app.(I'm using Python)

Why python posting one data will appear 6 times on Firebase database

I just use the simple code to post a data to firebase, but I don't know why it appears 6 times on firebase realtime database.
from firebase import firebase
url = "https://xxx.firebaseio.com/"
fb = firebase.FirebaseApplication(url, None)
fb.post("/posts", {'ID':123})
I run "python fb.py" one time only.
However the result is:
I am very confused.
Are you trying to update this field or create a new entry with a unique ID when you run your code?
Maybe try using fb.put() instead.
fb.post() is the equivalent of .push() in the JavaScript API, so it creates a unique ID for you. fb.put() is equivalent to .set() and will just set the data.

django continuous api data and saving into database

Suppose I am getting a list of user from an api in json format and I want to save it in my User model in django. Saving the user might not be a problem but I want that data continuously.
I am getting an api from a system which sends me the list of user who sent emails. These users go on increasing.
Now I am getting a list of users. I want to save these users in my database. But the questioning part is, suppose 10 user has sent messages I am getting list of 10 users from api then I will save it say like
usr = User()
usr.username = data["username"]
usr.save()
Now what when 1 more user sends the email. Now I will be receiving 11 user.
Here I want continuously add the updated user in my database. How to do this ?
I dont know if I have made it clear or not but need help on this
I guess your problem is how to trigger the retrieving of the emails.
A very easy way to do this is to use a timed autoreload - you can do this easily in html. See this answer. Then your django view can check for new emails and respond with the new data.
as per my understanding, you want to check for the new data entries periodically and save the new records into db. Right?
So for periodic tasks Celery is the best utility.
once you get data periodically then get db data into list_db and API data into API_list. Now start comparing both the lists data and store the new one into db.
I hope this will help.

I can't send Emails using Django non-rel on GAE

Im trying to send a simple email to do the password recover of a user, the input is just a email to send the new password..
But i can't... i get this error
SMTPServerDisconnected: please run connect() first
I already tried a few examples, like, https://bitbucket.org/andialbrecht/appengine_emailbackends/overview, but i get the same error
I really need this, maybe someone can tell me how to use an alternative to code in my view to send an email...Also i changed the backend to
EMAIL_BACKEND = 'djangoappengine.mail.EmailBackend'
but nothing,i don't know how to use this backend anyway :(
Plz Help :(
maybe someone can tell me how to use an alternative to code in my view to send an email...
I can help with this, seeing as it seems that perhaps this repository you're trying to use is based on an earlier version of App Engine and is throwing the error due to a required code change somewhere in the library - either that or the fact that you changed the string from what the library recommends (your version: 'djangoappengine.mail.EmailBackend') to a string that seems to not be correct, as it's different to what the author of the repository directed you to use (their version: 'appengine_emailbackend.EmailBackend'), and this is causing trouble.
Whenever possible, I'd recommend seeing if there is an "app-engine-y" way to do something, before going to a third-party library or deploying a module somebody else wrote to hack in third-party capabilities, or looking for an advanced/experimental feature (for example, use Datastore first, rather than remotely connecting to a MySQL VM, unless you need MySQL). If you absolutely need that library, this is a different story, but if you just want to send emails, the Mail API is what you need. It's a convenient way to send emails on App Engine.
I'm going to assume in the following that you are storing your user's usernames and hashed passwords in custom-defined User-kind entities in your Datastore. If you have your users using simple OAuth to sign into your site, there is never any reason to "reset/recover password":
Create the <form action="/some/route" action="POST"> element on
the page where the user requests password recovery.
Put the code responsible for handling this form submission (they will input their email, or whatever account info they need for your code to find their User entity in the Datastore in a handler that will respond on that route.
In the handler, generate a unique token and store it in the Datastore. Send the token in the email that you generate and send using the Mail API (see the example code in the link to the docs I provided). This will allow your user to return to your site, authenticate with the token from the email, and then fill out a form to create a new password. You will then hash this password (with a salt) and store it in their User entity in your Datastore.
I'm skipping over the details of how to implement a "password recovery form", given what I said about OAuth and that you are probably really only concerned with how to send mail. In the email you send, for example, you can insert a hyperlink to your site with the token already inserted as a query param, so that the user doesn't have to copy and paste, etc.

How do I create a session variable in Python?

I use Python 3 as a serverside scripting language, and I want a way to keep users logged into my site. I don't use any framework, since I prefer to hand code pages, so how do I create session variables like in PHP in Python 3?
The logic of a session is storing a unique session id inside the user cookie ( uuid package will do a perfect job for that ). And you store the sessions data inside a file, database or other semi-permanent datastore.
The idea is matching the sessionid that you receive from your user cookie, to some data stored somewhere on your server.
I assume that you know how to add the right header to set a cookie via the response header.
Otherwise there is more information here : http://en.wikipedia.org/wiki/List_of_HTTP_header_fields#Responses

Categories