views:

47

answers:

1

Hi,

I had read the docs for Appengine to know how to retrieve data from Models. But i´m missing something..

My models are user and student, where student is a reference property from user.

Users login, fill form with some values and save the data with put(). If you login with [email protected] you get your data or if you login with another email you get the data corresponding to your information sent to db.

Everythin is fine till here. I´m trying to get a PDF and want to get the data from the user loged in.

Example:

class SavePDF(webapp.RequestHandler):
    def post(self):
        user = users.get_current_user()
        if user:
            student= models.Student.all().get()

        p = canvas.Canvas(self.response.out)

        p.drawImage('ipca.jpg', 40, 700)

        p.drawString(50,640, 'Student Info:'  + '%s'  % student.name)   
        p.drawString(50,620, 'Adress:'                 + '%s'  % student.adress)


        p.drawString(50,300, 'PDF updated:%s'  %          str(datetime.date.today()))

        p.showPage()
        self.response.headers['Content-Type'] = 'application/pdf'
        self.response.headers['Content-Disposition'] = 'filename=mypdf.pdf'
        p.save()

What i get here is a user that is not current user. Shows Info about other user. I´ve tried different things but it throws errors. If i try to iterate gives error.

I´m missing something.

+2  A: 

If your Student model looks like this:

class Student(db.Model):
    user = db.UserProperty()
    name = db.StringProperty()
    address = db.StringProperty()

Then you probably want something like this:

user = users.get_current_user() 
if user: 
    student = models.Student.all().filter('user =', user).get() 
Saxon Druce
hi, get this error:Traceback (most recent call last): File "C:\Program Files\Google\google_appengine\google\appengine\ext\webapp\__init__.py", line 513, in __call__ handler.post(*groups) File "C:\Users\Celia\workspace\myApp\src\main.py", line 337, in post p.drawString(50,640, 'Student Info:' + '%s' % student.name)AttributeError: 'NoneType' object has no attribute 'name'
Martin
Sorry, user is defined in class User( db.Model):
Martin
@Martin: This happens because student is None. From your code, that might happen if there is no logged in google user (ie users.get_current_user()) returned None, or it might happen if there is no student with the specified user (ie the query returns None). Your PDF code assumes a valid student exists - possibly you need to return something else when there is no valid student?
Saxon Druce
Hi Saxon, tahnks for your quick reply. What is strange to me is that if i make student= models.Student.all().get() doesn´t give error. It returns data from a user that is not current user. I´m testing locally but also tested at http://ipca-fest.appspot.com/. Maybe i have to tweak my models, but don´t know how to fix this.
Martin
class User(db.Model): user=db.UserProperty('user', required=False) foto = db.BlobProperty('foto')class Student(db.Model): photo=db.BlobProperty('photo') name= db.StringProperty() unidade = db.StringProperty(choices = CHOICES_UNIDADE ) categoria = db.StringProperty(choices = CHOICES_CATEGORIA) regime = db.StringProperty(choices = CHOICES_REGIME) ano = db.IntegerProperty() modificado = db.DateTimeProperty(auto_now = True) user=db.ReferenceProperty(User, verbose_name=u'utilizador', required=False, collection_name='utilizadores')
Martin
Martin, if you do `student= models.Student.all().get()' you are asking the datastore for the first Student entity that it finds. If you want the one that matched the *current user* you need to use the filter clause that Saxon included.
Adam Crossland
Thanks adam. could you give more specific help to resolve this error. What changes should I do in my Model or some Handler. I don´t get the error. Since now many thanks.
Martin
Martin: as written, if there's no logged in user you'll get a NameError as student will be undefined. You should use 2 conditionals here; if there's no logged in user, go to a login prompt. If the logged in user doesn't have a corresponding Student in the datastore (Saxon's filter above results in None), print an error message or redirect to a page to input data about the Student.
Wooble