How to access user uid from anywhere in the code? - python

I'm trying to access the currently logged in user eslewhere in my code (i.e in other classes). I've managed to do this to some extent with global (frowned upon I know but didn't find any other way), as shown below:
class Login(Screen):
def login(self, email, password):
try:
global user
user = auth.sign_in_with_email_and_password(email, password)
print(user['localId']) #This is the users uid
class SelectChat(Screen):
def __init__(self, **kwargs):
super(SelectChat, self).__init__(**kwargs)
grid = GridLayout(cols=1)
self.add_widget(grid)
docs = db.collection(u'users').where(u'value', u'==', True).stream()
for doc in docs: # cycle through database and create Buttons for each value found
dict = doc.to_dict()
btn = Button(text="{} {}".format(dict['first_name'], dict['last_name']), id=doc.id)
btn.bind(on_release=self.move_to_chat(doc.id))
grid.add_widget(btn)
def move_to_chat(self, *args):
group_id = str(user['localId']) + str(doc.id) # This is where I'm trying to access the user variable but it's getting called on startup due to the above ```btn.bind()``` call.
print(group_id)
MDApp.get_running_app().sm.current = "chat"
The problem I'm having now is that I want to access the user['localId'] value in the __init__ method of another class. As this is run when as soon as the app has loaded the login method hasn't started and therefore there's no user variable.
Error message: NameError: name 'user' is not defined

Related

How to pass username into function inside a form in django?

I have a form which has a variable that calls a function to get a list of names. I need to pass the current logged in user as a dynamic parameter variable into this function.
I have spent about 2 days on this trying to work any and every solution I can. Cannot find anything that works. I have tried to initialize a request object but cannot get that to work.
class ManagerForm(forms.Form):
names = get_employee_names(<<dynamic username goes here>>)
manager = forms.ChoiceField(choices=names, widget=forms.RadioSelect)
The expected result is to pass the username as a string into the function as a parameter.
Forms by itself doesn't have access to request object and therefore can't identify which user is currently logged. Your view should pass current user username instead:
views.py:
def index(request):
# ...
form = ManagerForm(request.POST or None, current_user_username=request.user.username)
# ...
forms.py:
def get_employee_names(username):
# assuming it constructs correct choices tuples, like:
# choices = ((username, username), ('noname', 'noname'))
return choices
class ManagerForm(forms.Form):
manager = forms.ChoiceField(choices=[], widget=forms.RadioSelect)
def __init__(self, *args, **kwargs):
username = kwargs.pop('current_user_username')
super().__init__(*args, **kwargs)
self.fields['manager'].choices = get_employee_names(username)
This is description of what django expect choices to be.

Python: Call string from another class

Im writing a GUI with Tkinter in python 2.7 where i want a username variable to get used in another class. This is my first time doing OOP, so i'm not good at all. All help is appreciated.
Here is a link of the full code.
class LoginWindow(Frame):
def logged(self):
username = self.entry_username.get()
password = self.entry_password.get()
class MainWindow(Frame):
def saveValues(self):
# I WANT THE USERNAME VARIABLE HERE #
You can define username and password as global variable outside all the classes and then use them directly in whatever class you want.
Now for your code, you can try this too:
class LoginWindow():
def __init__(self):
self.username = self.entry_username.get()
self.password = self.entry_password.get()
class MainWindow():
def saveValues(self):
obj = LoginWindow()
# Access the object's variables here
print(obj.username, obj.password)
return "Done..."
print(MainWindow().saveValues())
This will print
("whatever_username", "password")
Done...

Google Cloud Datastore 'NoneType' object has no attribute 'email'

When I try getting a kind from my datastore it returns NoneType, as if the query is empty. I know the datastore is working properly while saving, but pulling a kind from the a query is not.
Also using the GQL Query in the Google cloud Console website and using SELECT * FROM User does return all the kinds. User kind has no parents, it is at the root. I made sure all the properties are indexed as well.
I am not sure what I am doing wrong on GET.
MyApp.py
import webapp2
from google.appengine.ext import ndb
from google.appengine.ext.db import GqlQuery
class MainHandler(webapp2.RequestHandler):
def post(self):
message = self.request.body
message = message.splitlines()
if message[0] == "register":
user = User.create_user(message[1], message[2], message[3])
user_key = User.save_user(user)
if user_key is not None:
self.response.write(user_key)
else:
user = User.get_by_id(User.email == message[0])
if User.token == message[1]:
self.response.write("CURRENT")
else:
User.token = message[1]
User.save_user(user)
self.response.write("UPDATED")
def get(self):
self.response.write("CONNECTED")
user= User.query().get()
self.response.write("\n" + query.email)
class User(ndb.Model):
email = ndb.StringProperty()
token = ndb.StringProperty()
name = ndb.StringProperty()
#classmethod
def create_user(cls, email, token, name):
user = User(email=email, token=token, name=name, id=email)
return user
#classmethod
def save_user(cls, user):
user_key = user.put()
return user_key
#classmethod
def get_user(cls, email):
return User.get_by_id(User.email == email)
app = webapp2.WSGIApplication([
('/', MainHandler)
], debug=True)
You seem to be confusing .get_by_id() with a query.
The get_by_id method is actually mapped to ndb.Model._get_by_id which invokes ndb.Model._get_by_id_async, which requires an entity key identifier to determine the entity's key used to do a direct entity lookup (not a query!). From appengine.ext.ndb.model.py:
#classmethod
#utils.positional(3)
def _get_by_id_async(cls, id, parent=None, app=None, namespace=None,
**ctx_options):
"""Returns an instance of Model class by ID (and app, namespace).
This is the asynchronous version of Model._get_by_id().
"""
key = Key(cls._get_kind(), id, parent=parent, app=app, namespace=namespace)
return key.get_async(**ctx_options)
But in your code you're passing as id a bool: User.email == message[0], which most likely won't match any existing entity key identifiers, hence the None result causing the error you see.
Since the info you have available is the value of an entity's property (the email value) you probably want to perform a query, something along these lines:
results = User.query(User.email == message[0]).fetch(limit=1)
if results:
user = results[0]
So I figured out what was wrong. So there seems to be an issue with the way Google Cloud SDK is set up on my computer. When running the same code on the google servers rather than on my network everything seems to work properly.

Python global name not defined error?

I am calling a function to validate form in python, but I am getting error :
user = valid_username(username)
NameError: global name 'valid_username' is not defined.
code:
class MainPage(Handler):
def valid_username(self,username):
print USER_RE.match(username)
return USER_RE.match(username)
def render_front(self,username="",password="",error=""):
logins = db.GqlQuery("SELECT * FROM logindb")
self.render("login.html",username=username,password=password,error=error,logins=logins)
def get(self):
self.render_front()
def post(self):
username = self.request.get("userID")
password = self.request.get("pass")
user = valid_username(username)
# pas = isvalid_password(password)
if user and password:
a = logindb(username = username,password=password)
a.put()
self.redirect("/")
else:
error = "we need both username and password"
self.render_front(username,password,error)
valid_username is a class method which means that its name is in the class namespace. But python doesn't look variables up in the class namespace unless you explicitly tell it to do so. When python sees:
user = valid_username(username)
it looks for "valid_username" in the local method scope and then the module scope (confusingly called "global"). Since its not in either, you get the error.
When you define a class method like:
def valid_username(self,username):
the self variable is in the local function scope and refers to the class instance data. You use self to find the method:
user = self.valid_username(username)
Now, python looks up self in the local function scope, realizes its a class instance reference, and continues the lookup by first checking if the name is in the instance variables and then falling back to the class namespace (finally!) where your method is defined.

Modify static class variable from a different Class/module

couldnt find concrete answer to my question in other posts so here it goes :
I need to modify a static class variable from a different class/module. I got my LoginPage class with static variable admin
class LoginPage(BasePage):
#Users
admin = ["testemail1#gmail.com","Password1234!"]
basicUser = ["testemail1#gmail.com", "Password1234!"]
and i got my profile test class which has uses changepassword method/test :
class MyProfileTests(BaseTestCase, LoginPage):
def test_change_password(self):
try:
self.loginAs(self.basicUser)
MyProfilePage(self.driver).changePassword('Password007$$')
and finally changepassword method
def changePassword(self, new_password):
self.driver.find_element_by_xpath(self._change_pw).click()
password_fields = self.driver.find_elements_by_xpath(".//*[#class='form-group']/*/input")
password_fields[1].send_keys(new_password)
self.submit_button.click()
#Changing password for the LoginPage object
self._passwordChange(new_password)
def _passwordChange(self, password):
try:
self.driver.find_element(by=By.XPATH, value = '//*[contains(text(), "Password successfully changed.")]')
LoginPage.admin[1] = password
except NoSuchElementException:
pass
The problem is that it doesn't seem to change actual object which is LoginPage.admin. The idea here is to whenever i use changePassword method that i dont need to manually update the password for the admin/basicuser if i need to login for a differnt test
It seems what i wanted to do is to dynamically modify a line of code in LoginPage.py, which does not work with my code and will probably need some advanced knowledge. I decided to keep all of the logins in a separate .txt file which solved the problem

Categories