Thomas Robert Horn
Thomas Robert Horn

Reputation: 133

Adding methods to GAE database class

I am messing around with GAE. I want to place my database object in one file and call it from another. Here is the DB object:

import webapp2
import os
import jinja2
import json
import logging
import main 



from google.appengine.ext import db


class User(db.Model):
    user_name = db.StringProperty(required = True)
    hashed_password = db.StringProperty(required = True)
    email = db.EmailProperty(required = True)
    created_dttm = db.DateTimeProperty(auto_now_add = True)
    last_modified = db.DateTimeProperty(auto_now = True)
    coords = db.GeoPtProperty(required = False)


    # def as_dict(self):
        # time_fmt = '%c'
        # d = { 
                # 'subject':self.subject,
                # 'content':self.content,
                # 'created':self.created_dttm.strftime(time_fmt),
                # 'last_modified': self.last_modified.strftime(time_fmt)
            # }
        # return d  

    def isValueUnique(self,column,value):
        result = None
        q = User.all()
        q.filter(column, value)
        result = q.get()
        return result

I cannot instantiate the DB because it thinks I'm trying to store data.

I want to call the isValueUnique method from another file like so:

import webapp2
import os
import jinja2
import json
import logging
import main 
import database
import validation

from google.appengine.ext import db



class SignUp(main.Handler):
    def post(self):
        user_username = self.request.get("username")
        user_email = self.request.get("email")
        user_pass = self.request.get("password")
        user_verify = self.request.get("verify")
        valid = validation.Valid()
        error1=""
        error2=""
        error3=""
        error4=""


        q = database.User.all()
        q.filter("username =", user_username)
        result = q.get()
        if result:
            error1="Username already taken"
        if (not valid.valid_user(user_username)) and (not error1):
            error1 = "Enter a valid username"
        if not valid.valid_password(user_pass):
            error2 = "Enter a valid password"
        if not valid.valid_pass_match(user_pass,user_verify):
            error3 = "Passwords must match"

        # Email Validation
        email=valid.valid_email(user_email)
        if not email:
            error4 = "Invalid email"
            email=""
        elif not database.User.isValueUnique("email",email):
            error4 = "Email already in use, please sign in"
            email=""

I get this error: elif not database.User.isValueUnique("email",email):

TypeError: unbound method isValueUnique() must be called with User instance as first argument (got str instance instead)

I can't instantiate User like I already said. What is the work around here?

Upvotes: 1

Views: 31

Answers (1)

Dave W. Smith
Dave W. Smith

Reputation: 24966

database.User.isValueUnique("email",email)

This is attempting to call a method on the database.User class, but isValueUnique is an instance method.

If you decorate isValueUnique with @staticmethod you'll get farther.

Where are you trying to instantiate a User?

Upvotes: 1

Related Questions