User account code with GET and POST I'm new to Python and GAE, but years of procedural programming. I have this code, but I know that there must be some better solution to avoid retyping code.

The first and last section of code are identical in both GET and POST methods, so I suppose there must be another way to share the identical code.

# This class manage the user account
class MyAccount(webapp2.RequestHandler):
        def get(self):
                """ Shows actual user account """

                # Get actual user
                user = users.get_current_user()

                if user:
                        # Get the actual user data
                        data = Person.gql("WHERE username = :nick",
                                                            nick=user.nickname()).get()

                        # Test if we found the user
                        if data:
                                templateValues['data'] = data
                                template = jinja_environment.get_template('myAccount.htm')
                        else:
                                templateValues['custom_msg'] = "The logged user is not available."
                                template = jinja_environment.get_template('customMessage.htm')

                else:
                        templateValues['custom_msg'] = "You are not logged in. Please, login to access to your account"
                        template = jinja_environment.get_template('customMessage.htm')

                # Render the page
                self.response.out.write(template.render(templateValues))

        def post(self):
                """ Saves user updated data """

                # Get actual user
                user = users.get_current_user()

                if user:
                        # Get the actual user data
                        person = Person.gql("WHERE username = :nick",
                                                            nick=user.nickname()).get()

                        # Test if we found the user
                        if person:
                                person.username = self.request.get('username')
                                person.password = self.request.get('password')
                                if person.email: self.request.get('email')
                                person.name = self.request.get('name')
                                person.lastname = self.request.get('lastname')
                                person.idnumber = self.request.get('idnumber')

                                templateValues['data'] = person
                                template = jinja_environment.get_template('myAccount.htm')

                        else:
                                templateValues['custom_msg'] = "The logged user is not available."
                                template = jinja_environment.get_template('customMessage.htm')

                else:
                        templateValues['custom_msg'] = "You are not logged in. Please, login to access to your account."
                        template = jinja_environment.get_template('customMessage.htm')

                # Render the page
                self.response.out.write(template.render(templateValues))
This line looks suspicious:

            if person.email: self.request.get('email')


I assume it should be:

            person.email = self.request.get('email')


Your code seem to be using excessive indentation. The python standard is four space per level. 

You are using templateValues but don't seem to define it anywhere.

To refactor this, I'd start by removing the section of that that's different between the two methods into their own function:

# This class manage the user account
class MyAccount(webapp2.RequestHandler):

    def show_user(self, user):
        # Get the actual user data
        data = Person.gql("WHERE username = :nick",
                                            nick=user.nickname()).get()
        # Test if we found the user
        if data:
            templateValues['data'] = data
            template = jinja_environment.get_template('myAccount.htm')
        else:
            templateValues['custom_msg'] = "The logged user is not available."
            template = jinja_environment.get_template('customMessage.htm')

        return template, templateValues



    def get(self):
        """ Shows actual user account """

        # Get actual user
        user = users.get_current_user()
        if user:
            template, templateValues = self.show_user(user)
        else:
            templateValues['custom_msg'] = "You are not logged in. Please, login to access to your account"
            template = jinja_environment.get_template('customMessage.htm')

        # Render the page
        self.response.out.write(template.render(templateValues))

    def update_user(self, user):
        # Get the actual user data
        person = Person.gql("WHERE username = :nick",
                nick=user.nickname()).get()
        # Test if we found the user
        if person:
            person.username = self.request.get('username')
            person.password = self.request.get('password')
            person.email = self.request.get('email')
            person.name = self.request.get('name')
            person.lastname = self.request.get('lastname')
            person.idnumber = self.request.get('idnumber')

            templateValues['data'] = person
            template = jinja_environment.get_template('myAccount.htm')
        else:
            templateValues['custom_msg'] = "The logged user is not available."
            template = jinja_environment.get_template('customMessage.htm')

        return template, templateValues


    def post(self):
        """ Saves user updated data """
        # Get actual user
        user = users.get_current_user()
        if user:
            template, templateValues = self.update_user(user)
        else:
            templateValues['custom_msg'] = "You are not logged in. Please, login to access to your account."
            template = jinja_environment.get_template('customMessage.htm')
        # Render the page
        self.response.out.write(template.render(templateValues))


The only thing different between post and get is the function they call. We'll just pass the function as a parameter to a new function.

    def request(self, handler):
        """ Shows actual user account """

        # Get actual user
        user = users.get_current_user()
        if user:
            template, templateValues = handler(user)
        else:
            templateValues['custom_msg'] = "You are not logged in. Please, login to access to your account"
            template = jinja_environment.get_template('customMessage.htm')

        # Render the page
        self.response.out.write(template.render(templateValues))

    def get(self):
        return self.request(self.show_user)

    def post(self):
        return self.request(self.update_user)


The current code repeats calls to get_template a lot. So we'll refactor it so that it only happens once. We'll just store the name of the template in a variable and load it just before we render it.

def show_user(self, user):
    # Get the actual user data
    data = Person.gql("WHERE username = :nick",
                                        nick=user.nickname()).get()
    # Test if we found the user
    if data:
        templateValues['data'] = data
        template = 'myAccount.htm'
    else:
        templateValues['custom_msg'] = "The logged user is not available."
        template = 'customMessage.htm'

    return template, templateValues



def request(self, handler):
    """ Shows actual user account """

    # Get actual user
    user = users.get_current_user()
    if user:
        template, templateValues = handler(user)
    else:
        templateValues['custom_msg'] = "You are not logged in. Please, login to access to your account"
        template = 'customMessage.htm'

    # Render the page
    template = jinja_environment.get_template(template)
    self.response.out.write(template.render(templateValues))


But we can clean up our actual work functions a little more:

def show_user(self, user):
    # Get the actual user data
    data = Person.gql("WHERE username = :nick",
                                        nick=user.nickname()).get()
    # Test if we found the user
    if data:
        return 'myAccount.htm', {
                'data' : data
        }
    else:
        return 'custom_msg.htm', {
                'custom_msg' : "The logged user is not available."
        }