Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

0

How to restrict user login based date on a custom module..Need Help..Here I attached code sample from a refernce

By
Odoo
on 6/23/14, 8:20 AM 739 views

Hi,

Please check this code and ,need explain when this code will excecuted for a user login restriction....

init.py----------------------------

import login_info_check

openerp.py--------------------------

 

{
    "name" : "Check User Login",
    "version" : "1.0",
    "author" : "",
    "category" : "Generic Modules for Customer ",
    "description": """
    Module For Managing customer Login:
 
    """,
    "depends" : ['base'],
    "init_xml" : [],
    'images': [],

     "update_xml" : [
                    'user_login_check.xml'           
         ],
                   
    'images': [],
    "installable": True,
    "auto_install":False
}

-----------------------------

login_info_check-----------------------

from functools import partial
import logging
from lxml import etree
from lxml.builder import E
from datetime import datetime

import openerp
from openerp import SUPERUSER_ID
from openerp import pooler, tools
import openerp.exceptions
from openerp.osv import fields,osv
from openerp.osv.orm import browse_record
from openerp.tools.translate import _

_logger = logging.getLogger(__name__)


class res_users(osv.osv):
    
    _inherit = 'res.users'
 
    def login(self, db, login, password):
        try:
            current_date="%Y-%m-%d"
            users_obj = self.pool.get('res.users')
            print "users_obj################################",users_obj
            cr = pooler.get_db(db).cursor()
            user_id = super(res_users, self).login(db, login, password)
            login_date = None
            try:            
                if user_id:
                    val = users_obj.browse(cr, SUPERUSER_ID, user_id)
                    print "@@@@@@@@val################################",val
                    login_date = val.login_date
                    print "@@@@@@@@login_date################################",login_date
                    current_date = datetime.date.today().strftime('%Y-%m-%d')
                    print "@@@@@@@@current_date################################",current_date
            except:
                pass
            if login_date == current_date:
               print "@@@@@@@@login_date################################INVALID CONDITION",login_date
               raise osv.except_osv(_('Message!'), _("Restrict Login Only one in a day"))
               _logger.info("Login failed for db:%s login:%s", db, login)
               user_id = False                
        except openerp.exceptions.AccessDenied:
            _logger.info("Login failed for db:%s login:%s", db, login)
            user_id = False
        return user_id
    
res_users()

 

.xml-----------------

 

<?xml version="1.0" encoding="utf-8"?>
<openerp>
    <data>

 
</data>
</openerp>

 

 

 

 

Second time its shows Error msg login into admin and Logout and try to login its shows error msg

Prakash
on 6/23/14, 8:32 AM

No Praksh its not working.. I tried as per your answer But in your code I copied and paste and try to run then one error occured---------------------- UnboundLocalError: local variable 'current_date' referenced before assignment

Odoo
on 6/23/14, 8:39 AM

So I create one variable like, herer def login(self, db, login, password): try: current_date="%Y-%m-%d"

Odoo
on 6/23/14, 8:40 AM

First I login as admin,then logout.Again I login as admin,,then no error message or warning is shows............could you help

Odoo
on 6/23/14, 8:42 AM

Hi Prakash, can u help me ..this code is not working as per your code...,So Pls guide me how to run this code

Odoo
on 6/24/14, 12:03 AM

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

2 follower(s)

Stats

Asked: 6/23/14, 8:20 AM
Seen: 739 times
Last updated: 3/16/15, 8:10 AM