zpage / model / user_mail.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-
from _db import Model, McCache
from model.zsite import Zsite
from model.cid import CID_USER


class UserMail(Model):

mc_mail_by_user_id = McCache('MailByUserId.%s')
#mc_mail_by_user_id_if_login = McCache('MailByUserIdIfLogin.%s')

#def mail_by_user_id_if_login(user_id):
#    c = UserMail.raw_sql('select mail from user_mail where user_id=%s and state=%s', user_id, MAIL_LOGIN).fetchone()
#    if c:
#        return c[0]
#    return ''

def mail_by_user_id(user_id):
    c = UserMail.raw_sql('select mail from user_mail where user_id=%s order by state desc limit 1', user_id).fetchone()
    if c:
        return c[0]
    return ''

mc_user_id_by_mail = McCache('UserIdByMail:%s')

def _user_id_by_mail(mail):
    c = UserMail.raw_sql('select user_id from user_mail where mail=%s ', mail).fetchone()
    if c:
        return c[0]
    return 0

def user_by_mail(mail):
    user_id = user_id_by_mail(mail)
    return Zsite.mc_get(user_id)

def user_id_by_mail(mail):
    if mail:
        mail = mail.strip().lower()
        return _user_id_by_mail(mail)

def user_mail_new(user_id, mail, state=MAIL_UNVERIFY):
    mail = mail.strip().lower()
    id = user_id_by_mail(mail)
    if  id and id != user_id:
        return False

    if state == MAIL_UNVERIFY:
        UserMail.where(user_id=user_id, state=MAIL_UNVERIFY).delete()

    u = UserMail(mail=mail, user_id=user_id, state=state)
    mc_mail_by_user_id.set(user_id, mail)
    mc_user_id_by_mail.set(mail, user_id)
    return user_id

def user_mail_active_by_user(user, mail=None):
    user_id = user.id
    for u in UserMail.where(user_id=user_id, state=MAIL_LOGIN):
        u.state = MAIL_VERIFIED

    um = None
    if not mail:
        um = UserMail.get(user_id=user_id, state=MAIL_UNVERIFY)
        mail = um.mail
        um = UserMail.get(user_id=user_id, mail=mail)

    if um is not None:
        um.state = MAIL_LOGIN
        mc_mail_by_user_id.set(user_id, mail)
        mc_user_id_by_mail.set(mail, user_id)

    if user.cid == CID_USER and mail:
        from model.autocomplete_user import autocomplete_user_mail_new
        autocomplete_user_mail_new(user, mail)

    return um

def user_mail_by_state(user_id, state):
    return UserMail.where(user_id=user_id).where('state>=%s', state).col_list(col='mail')

if __name__ == '__main__':
    #from zsite import Zsite, CID_USER, STATE_ACTIVE
    #for i in Zsite.where(cid=CID_USER).where("state>=%s"%STATE_ACTIVE):
    #    print i.id
#    print mail_by_user_id(10128296)
    mail = 'kilnt.huo@gmail.com'
    print UserMail.raw_sql('select count(1) from user_mail where mail=%s', mail).fetchone()
    print UserMail.raw_sql("select user_id from user_mail where mail=%s", mail).fetchone()
    from model.vps_sell import VpsOne
    print user_by_mail(mail).id
    c = UserMail.raw_sql('update user_mail set user_id = %s where mail=%s',10077177, mail)
#    print c 
    vps = VpsOne.get(377)
    vps.user_id = 10077177

Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.