Error loging in after authentication changes

46 views
Skip to first unread message

Maurice Waka

unread,
Apr 6, 2018, 4:57:15 PM4/6/18
to web2py-users
My app has been ok with these code;

In controller:

@auth.requires_login()
def notes():
    dbhealth.health.id.readable = False
    g = SQLFORM.grid(dbhealth.health, searchable=True, csv=False, user_signature=True, )
    return dict(form = g)........code
question = db(db.answers.author == auth.user.id).select(orderby=~db.answers.created_on,limitby=(2,0))
return dict(form=form, question=question)


In view:

{{for q in question:}}
{{q=XML(q.quest.replace('\n','<br>').replace('(','{').replace(')','}'), sanitize=True)}}
{{pass}}

when I changed the login credentials to membership requirement as follows:
@auth.requires_membership('managers')
def notes():

I got this error on trying to use the app:

<type 'exceptions.NameError'> name 'q' is not defined


what could be the issue? and how can i solve it?
I hope i have given all the details needed
Kind regards

Anthony

unread,
Apr 6, 2018, 5:01:15 PM4/6/18
to web2py-users
@auth.requires_login()
def notes():
    dbhealth.health.id.readable = False
    g = SQLFORM.grid(dbhealth.health, searchable=True, csv=False, user_signature=True, )
    return dict(form = g)........code
question = db(db.answers.author == auth.user.id).select(orderby=~db.answers.created_on,limitby=(2,0))
return dict(form=form, question=question)

The above is not valid Python code. Please show the real code.

Anthony

Maurice Waka

unread,
Apr 6, 2018, 5:41:02 PM4/6/18
to web...@googlegroups.com
Model:
dbhealth = DAL('sqlite://wellness.db',folder='/home/maurice/web2py')
dbhealth.define_table( "health",
                              Field('name', 'string'),
                              Field('definition', 'text', length= 1000000, default="We'll update soon.", notnull=True),
                              .....

in controller:

@auth.requires_login()#membership('managers')->this gives an errors bout q not being defined in view_searches
defnotes():
    dbhealth.health.id.readable = False
    g = SQLFORM.grid(dbhealth.health, searchable=True, csv=False, user_signature=True, )
    return dict(form = g)
@auth.requires_login()
def upd_healthinsure():
    id,column = request.post_vars.id.split('.')
    value = request.post_vars.value
    dbhealth(dbwellness.health.id == id).update(**{column:value})
    return value

    ....more code

name3 = 'testing string 123'
db.answers.insert(quest=name3)
    replies = db(db.answers.author == auth.user.id).select(db.answers.ALL)[-5:-1]
    question = db(db.answers.author == auth.user.id).select(orderby=~db.answers.created_on,limitby=(2,0))
    return dict(form=form, question=question, replies=replies)

This is how I have it. I hope its clear.
regards

--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to a topic in the Google Groups "web2py-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/web2py/YQhEantsews/unsubscribe.
To unsubscribe from this group and all its topics, send an email to web2py+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Anthony

unread,
Apr 6, 2018, 6:42:27 PM4/6/18
to web2py-users
@auth.requires_login()#membership('managers')->this gives an errors bout q not being defined in view_searches
defnotes():
    dbhealth.health.id.readable = False
    g = SQLFORM.grid(dbhealth.health, searchable=True, csv=False, user_signature=True, )
    return dict(form = g)
@auth.requires_login()
def upd_healthinsure():
    id,column = request.post_vars.id.split('.')
    value = request.post_vars.value
    dbhealth(dbwellness.health.id == id).update(**{column:value})
    return value

    ....more code

name3 = 'testing string 123'
db.answers.insert(quest=name3)
    replies = db(db.answers.author == auth.user.id).select(db.answers.ALL)[-5:-1]
    question = db(db.answers.author == auth.user.id).select(orderby=~db.answers.created_on,limitby=(2,0))
    return dict(form=form, question=question, replies=replies)

This still isn't the relevant code. The decorated function is notes(), but some other function (which you have not shown the name of) returns the "question" variable.

Please show the action (i.e., function) that is resulting in the error along with the view of that action and the full traceback.

Also, please use the code formatting feature provided by Google Groups.

Anthony

Maurice Waka

unread,
Apr 6, 2018, 10:08:34 PM4/6/18
to web...@googlegroups.com
Sorry , I was taking a shortcut to avoid posting much code.
Here it is, a different app still with the same issue..

def index():
    return locals()
@auth.requires_membership('managers')#->this gives an erros bout q not being defined in view_searches
def my_notes():
    dbwellnesshealth.health.id.readable = False
    g = SQLFORM.grid(dbwellnesshealth.health, searchable=True, csv=False, user_signature=True, )
    return dict(form = g)
@auth.requires_login()
def upd_healthinsure():
    id,column = request.post_vars.id.split('.')
    value = request.post_vars.value
    dbwellnesshealth(dbwellness.health.id == id).update(**{column:value})
    return value
@auth.requires_login()
def about():
    return dict(message=T('Welcome to Home!'))
@auth.requires_login()
def view_searches():
    if db(db.post).isempty():
        db.post.insert(message="Hi, it's" +' ' +auth.user.first_name+' '+ 'loging in...')# Some times after deleting the questions we get an error about list index outof range when starting afresh. Hence this.
    form = SQLFORM(Post, formstyle='table3cols',)
    if form.process().accepted:
        pass
    name3 = 'testing this app 123'
    def searches_process():
        for item in [name3][0]:
            if '123' in item:
                return name3
            else:
                return "Ooops! no answer!"
    report = searches_process()
    db.answers.insert(quest=name3, message=report)
    replies = db(db.answers.author == auth.user.id).select(db.answers.ALL)[-5:-1]
    question = db(db.answers.author == auth.user.id).select(orderby=~db.answers.created_on,limitby=(2,0))
    answer = db(db.answers.author == auth.user.id).select(orderby=~db.answers.created_on, limitby=(2,0))
    return dict(form=form, question=question, answer=answer, replies=replies)

Traceback

1.
2.
3.
4.
5.
6.
7.
Traceback (most recent call last):
File "/home/maurice/web2py/gluon/restricted.py", line 219, in restricted
exec(ccode, environment)
File "/home/maurice/web2py/applications/Hestque_Wellness/views/default/view_searches.html", line 103, in <module>
display: inline-block;
NameError: name 'q' is not defined


As for the view:

{{extend "layout.html"}}

{{for q in question:}}
{{q=XML(q.quest.replace('\n','<br>').replace('(','{').replace(')','}'), sanitize=True)}}
{{pass}}
{{for a in answer:}}
{{a=XML(a.message.replace('\n','<br>').replace('(','{').replace(')','}'), sanitize=True)}}
{{pass}}
<html lang="en">
<meta charset="utf-8"/>

<style type="text/css">
    * {
        box-sizing: border-box;
        }
    body {
        background-color: #edeff2;
        font-family: 'Noto Serif', serif;
        font-size: 0.3em;
        font-weight: lighter;
        overflow-y:hidden;
        }
    .chat_window {
        position: fixed;
        width: calc(100% - 20px);
        max-width: 90%px;
        height: 440px;
        border-radius: 10px;
        background-color: #fff;
        left: 50%;
        top: 50%;
        transform: translateX(-50%) translateY(-50%);
        box-shadow: 0 10px 20px rgba(0, 0, 0, 0.15);
        background-color: #f8f8f8;
        overflow: hidden;
        bottom:0;
        }
    .messages {
        position: relative;
        list-style: none;
        padding: 20px 10px 0 10px;
        margin: 0;
        height: 390px;
        overflow-y: scroll;
        }
    .messages::-webkit-scrollbar {
        display: none;
    }
    .messages .message {
        clear: both;
        overflow: hidden;
        margin-bottom: 20px;
        transition: all 0.5s linear;
        opacity: 0;
        font-size: 0.5em;
        }
    .messages .message.left .text_wrapper {
        background-color: #8C9EFF;
        margin-left: 2px;
        }
    .messages .message.left .text_wrapper::after, .messages .message.left .text_wrapper::before {
        right: 100%;
        border-right-color: #8C9EFF;
        }
    .messages .message.left .text {
        color: white;
        }
    .messages .message.right .text_wrapper {
        background-color: #E8EAF6;
        margin-right: 2px;
        float: right;
        max-width: 89%;
        width: auto;
        position: relative;
        word-wrap: break-word;
        word-spacing: normal;
        text-align:justify;
        max-height: 150px;
        height:auto;
        display: inline-block;
        padding: 6px;
        border-radius: 7px;
        }
    .messages .message.right .text_wrapper::-webkit-scrollbar{
        display:none;
    }
    .messages .message.right .text_wrapper::after, .messages .message.right .text_wrapper::before {
        left: 100%;
        border-left-color: #E8EAF6;
        }
    .messages .message.right .text {
        color: black;
        font-size: 1em;
        }
    .messages .message.appeared {
        opacity: 1;
        }
    .messages .message .message_data_time  {
        margin-right:0;
        margin-top:0;
        position: relative;
        right:5px;
        display: inline-block;
        }
    .messages .message .text_wrapper {
        display: inline-block;
        max-height: 150px;
        border-radius: 6px;
        width:auto;
        overflow-y: auto;
        text-align:justify;
        position: relative;
        word-wrap: break-word;
        word-spacing: normal;
        padding:4px;
        line-height: 16px;
        }
    .messages .message .text_wrapper::-webkit-scrollbar{
        display:none;
    }
    .messages .message .text_wrapper::after, .messages .message .text_wrapper:before {
        top: 18px;
        border: solid transparent;
        height: 0;
        width: 0;
        position: absolute;
        pointer-events: none;
        }
    .messages .message .text_wrapper::after {
        border-width: 13px;
        margin-top: 0px;
        }
    .messages .message .text_wrapper::before {
        border-width: 15px;
        margin-top: 2px;
        }
    .messages .message .text_wrapper .text {
        font-size: 18px;
        font-weight: 300;
        }
    .bottom_wrapper {
        position: relative;
        width: 100%;
        background-color: #fff;
        padding: 10px 10px;
        position: absolute;
        bottom: 0;
        }
    .message_input_wrapper textarea {
        border: 1px solid #bcbdc0;
        padding: 10px 10px;
        font: 16px/22px "Lato", Arial, sans-serif;
        margin-bottom: 2px;
        border-radius: 5px;
        height: 45px;
        box-sizing: border-box;
        width: 82%;
        max-width: 82%;
        position: relative;
        outline-width: 0;
        color: black;
        top:5px;
        bottom: 10px;
        }
    .message_input_wrapper button {
        border: none;
        padding: 8px 2px;
        float: right;
        border-radius: 5px;
        height: 45px;
        box-sizing: border-box;
        width: 15.5%;
        max-width: 15.5%;
        position: relative;
        outline-width: 0;
        color: white;
        font-size: 16px;
        cursor: pointer;
        background: #8C9EFF;
        top:5px;
        bottom: 10px;
        }
    .bottom_wrapper .message_input_wrapper .message_input {
        border: none;
        height: 100%
        box-sizing: border-box;
        width: calc(100% - 40px);
        position: absolute;
        outline-width: 0;
        color: gray;
        }
    .bottom_wrapper .send_message {
        width: 140px;
        height: 40px;
        display: inline-block;
        border-radius: 50px;
        background-color: #a3d063;
        border: 2px solid #a3d063;
        color: #fff;
        cursor: pointer;
        transition: all 0.2s linear;
        text-align: center;
        float: right;
        }
    .bottom_wrapper .send_message:hover {
        color: #a3d063;
        background-color: #fff;
        }
    .bottom_wrapper .send_message .text {
        font-size: 18px;
        font-weight: 300;
        display: inline-block;
        line-height: 48px;
        }
    .message_template {
        display: none;
        }
</style>
    <script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
    <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.0/js/bootstrap.min.js"></script>
</head>


On Sat, Apr 7, 2018 at 12:01 AM, Anthony <abas...@gmail.com> wrote:

--

Maurice Waka

unread,
Apr 6, 2018, 10:15:32 PM4/6/18
to web...@googlegroups.com
view.html

<body>
    <div class="chat_window">
          <ul class="messages">
              {{for reply in replies:}}
              <li class="message left appeared">
                  <div>
                      {{=prettydate(reply.modified_on)}}
                  </div>
                  <div class="text_wrapper">
                      <div class="text">{{=XML(reply.quest.replace('\n','<br>').replace('(','{').replace(')','}'), sanitize=True)}}</div>
                  </div>
              </li>

              <li class="message right appeared">
                  <div>
                      {{=prettydate(reply.modified_on)}}
                  </div>
                  <div class="text_wrapper">
                      <div class="text">{{=XML(reply.message.replace('\n','<br>').replace('(','{').replace(')','}'), sanitize=True)}}</div>
                  </div>
              </li>
              {{pass}}
        </ul>
        <div class="bottom_wrapper clearfix">
            <div class="message_input_wrapper">
                <div class="message_input_wrapper">
                {{=form.custom.begin}}
                <textarea name="message" id="message_input" placeholder="Type your message here..."></textarea>
                <button>send</button>
                {{=form.custom.end}}
            </div>
            </div>
        </div>
    </div>
    <div class="message_template">
        <li class="message">
            <div class="message-data-time" ></div>
            <div class="text_wrapper">
                <div class="text"></div>
            </div>
        </li>
    </div>
    <script type="text/javascript">
        (function () {
        var Message;
        Message = function (arg) {
            this.text = arg.text, this.message_side = arg.message_side;
            this.draw = function (_this) {
                return function () {
                    var $message;
                    $message = $($('.message_template').clone().html());
                    $message.addClass(_this.message_side).find('.text').html(_this.text);
                    $('.messages').append($message);
                    return setTimeout(function () {
                        return $message.addClass('appeared');
                    }, 0);
                };
            }(this);
            return this;
        };
        $(function () {
            var getMessageText, message_side, sendMessage;
            message_side = 'right';
            getMessageText = function () {
                var $message_input;
                $message_input = $('.message_input');
                return $message_input.val();
            };
            sendMessage = function (text) {
                var $messages, message;
                if (text.trim() === '') {
                    return;
                }
                $('.message_input').val('');
                $messages = $('.messages');
                message_side = message_side === 'left' ? 'right' : 'left';
                message = new Message({
                    text: text,
                    message_side: message_side
                });
                message.draw();
                return $messages.animate({ scrollTop: $messages.prop('scrollHeight') }, 10);
            };
            $('.send_message').click(function (e) {
                return sendMessage(getMessageText());
            });
            $('.message_input').keyup(function (e) {
                if (e.which === 13) {
                    return sendMessage(getMessageText());
                }
            });
            sendMessage('{{=q}}');
            setTimeout(function () {
                return sendMessage('{{=a}}');
            }, 1500);
        });
    }.call(this));
</script>
    </body>
</html>

Anthony

unread,
Apr 7, 2018, 10:34:28 AM4/7/18
to web2py-users
{{for q in question:}}
{{q=XML(q.quest.replace('\n','<br>').replace('(','{').replace(')','}'), sanitize=True)}}
{{pass}}
...
        $(function () {
           ... 
            sendMessage('{{=q}}');

When "question" is empty (i.e., there are no answers from the current user), the variable "q" will not be defined in the for loop, and therefore will result in an exception later where you have {{=q}}.

This doesn't have anything to do with @auth.has_membership('managers'), as the error is not even occurring in the function with that decorator. If you are expecting "question" to always include some records, you have to figure out why you aren't getting any in this case. Otherwise, adjust your code to account for the possibility of an empty "question".

Anthony

Anthony

unread,
Apr 7, 2018, 10:36:42 AM4/7/18
to web2py-users
Also, there is not point to either of the for loops at the top of the view -- each loop is simply assigning a new value to the same variable, so the final value of the variable is just that of the final element in the iteration. You might as well just make that final assignment without bothering with iteration over all the previous items.

Anthony

Maurice Waka

unread,
Apr 7, 2018, 10:40:12 AM4/7/18
to web...@googlegroups.com
Thanks for the information. Let me work on it. 

--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to a topic in the Google Groups "web2py-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/web2py/YQhEantsews/unsubscribe.
To unsubscribe from this group and all its topics, send an email to web2py+un...@googlegroups.com.

Maurice Waka

unread,
Apr 9, 2018, 12:15:14 AM4/9/18
to web...@googlegroups.com
I moved the iteration to the controller side and it worked.
Regards

--
Reply all
Reply to author
Forward
0 new messages