mailcnf_bcc.py 8.74 KB
#!/usr/bin/python3

# -*- coding: UTF-8 -*-# enable debugging
from mysql.connector import MySQLConnection, Error
from read_dbconfig import read_db_config
import tornado.ioloop
import tornado.web
import  tornado.escape
import  tornado
import os.path
from mailsrv.mailcnf_tools import mailcnf_tools
mt=mailcnf_tools()

from ncauth.ncauth_base import ncauth_base
class mailcnf_bcc(ncauth_base):
    
    def get(self):
         self.redirect("/")
    
    @tornado.web.authenticated
    def post(self): 

        nav=all
        subnav=all
        content=all
        msglvl=""  
        msg=False
        module= self.get_argument("module", None, False)
        mainfnc= self.get_argument("mainfnc", None, False)
        id= self.get_argument("id", None, False)
        fnc= self.get_argument("fnc", None, False)
       # table= self.get_argument("table", None, False)
        domain_id= self.get_argument("domain_id", None, False)
        domaintld= self.get_argument("domaintld", None, False)
        group_id= self.get_argument("group_id", None, False)
        source= self.get_argument("source", None, False)
        destination= self.get_argument("destination", None, False)
        active= self.get_argument("active", None, False)
        #self.write("Hello, "+str(module )+" "+str(mainfnc)+" id: "+str(id)+" fnc: "+str(fnc)+" "+str(table)+"<br>")
        uid=0
        ugroup=0
        combolist=mt.getDomainNamebyGuid(ugroup)
        msg=""
        loader =tornado. template.Loader(os.path.join(os.path.dirname(__file__), "../templates"))
        navigation=loader.load("navigation.html").generate(nav=nav)
        if fnc=="delete" and id :
            msg=self.update_virtual_bccs_deleted(id);
            msglvl="alert alert-danger"
        if fnc=="undelete" and id :
            msg=self.update_virtual_bccs_undelete(id);
            msglvl="alert alert-danger"
        if fnc=="submitnewbcc"  :
                msg=self. insert_virtual_bcc(domaintld,source,destination,active,group_id)
                msg="BCC angelegt"
                msglvl="alert alert-success"
        
        if fnc=="submitedit"  :
            self.update_virtual_bccs(  id, domain_id,source,destination,active,group_id)
            msg="BCC aktualisert"
            msglvl="alert alert-danger"
                       
                 
                
        if  fnc=="edit" and id :  
            
            subnavigation=loader.load("subnav_"+module+"_"+mainfnc+".html").generate(subnav=subnav)
            lists=self.edit_virtual_bccs_by_id(id, uid)
            print(lists)
            maincontent=loader.load("content_"+module+"_"+mainfnc+"_"+fnc+".html").generate(content=content, lists=lists, uid=uid, ugroup=ugroup, domain_id=domain_id)
            self.render('base.html', navigation=navigation, subnavigation=subnavigation, maincontent=maincontent, msg=msg, msglvl=msglvl)

        elif  fnc=="new": 
            subnavigation=loader.load("subnav_"+module+"_"+mainfnc+".html").generate(subnav=subnav)
            maincontent=loader.load("content_"+module+"_"+mainfnc+"_"+fnc+".html").generate(content=content,uid=uid, ugroup=ugroup, domain_id=domain_id, combolist=combolist)
            self.render('base.html', navigation=navigation, subnavigation=subnavigation, maincontent=maincontent, msg=msg, msglvl=msglvl)
        else:
            subnavigation=loader.load("subnav_"+module+"_"+mainfnc+".html").generate(subnav=subnav)
            lists=self.print_virtual_domain_id('','',uid)
            maincontent=loader.load("content_"+module+"_"+mainfnc+".html").generate(content=content, lists=lists, combolist=combolist)
            self.render('base.html', navigation=navigation, subnavigation=subnavigation, maincontent=maincontent, msg=msg, msglvl=msglvl)
   
    def getUsers(self):
        self.write("Hello, ")

    def update_virtual_bccs_undelete(self, id):
        query = "UPDATE virtual_recipients_bcc SET deleted='0', active='1' WHERE id='"+str(id)+"'"
        try:
            db_config = read_db_config()
            conn = MySQLConnection(**db_config)
            cursor = conn.cursor()
            cursor.execute(query)
            conn.commit()
            return "ID "+str(id)+" wiederhergestellt"
        except Error as error:
            print(error)
        finally:
            cursor.close()
            conn.close()

    def update_virtual_bccs_deleted(self, id):
        query = "UPDATE virtual_recipients_bcc SET deleted='1', active='0'  WHERE id='"+str(id)+"'"
        try:
            db_config = read_db_config()
            conn = MySQLConnection(**db_config)
            cursor = conn.cursor()
            cursor.execute(query)
            conn.commit()
            return "ID "+str(id)+" deleted"
        except Error as error:
            print(error)
        finally:
            cursor.close()
            conn.close()
    
    
    def update_virtual_bccs(self,id,   domain_id,source,destination,active,group_id):
        query = "UPDATE virtual_recipients_bcc SET domain_id='"+str(domain_id)+"' ,source='"+str(source)+"' ,destination='"+str(destination)+"' ,active='"+str(active)+"' ,group_id='"+str(group_id)+"'  WHERE id="+str(id)+" "
        try:
            #self.write(query)
            db_config = read_db_config()
            conn = MySQLConnection(**db_config)
            cursor = conn.cursor()
            cursor.execute(query)
            conn.commit()
            #self.write(query)
        except Error as error:
            print(error)
        finally:
            cursor.close()
            conn.close()



    def insert_virtual_bcc(self,domaintld,source,destination,active,group_id=0):
        domain_id= str(mt.get_domain_id_by_name(domaintld))
        source= source+'@'+domaintld
        query = "INSERT INTO virtual_recipients_bcc(domain_id,source,destination,active,group_id) VALUE('"+str(domain_id)+"','"+str(source)+"','"+str(destination)+"','"+str(active)+"','"+str(group_id)+"');"
        #self.write(query)
       # args = (domain_id,password,email,sent_mail,active)
        insert_id = ''
        try:
            db_config = read_db_config()
            conn = MySQLConnection(**db_config)
            cursor = conn.cursor()
           
            cursor.execute(query)
            if cursor.lastrowid:
                insert_id=str(cursor.lastrowid)
            else:
                print('mailcnf_domain')
            conn.commit()
        except Error as error:
            print(error)
        finally:
            cursor.close()
            conn.close()
        return insert_id

    def print_virtual_domain_id(self, id='', gid='', uid='0'):
        query = ''
        delstring = ' '
        dictdata =[]
        if str(uid) !='0':
            delstring = " and deleted='0' "
        if id!='' and id != None:
            query = " SELECT 	id,domain_id,source,destination,modified,active,deleted,group_id FROM `virtual_recipients_bcc` WHERE `id`="+id+" "+delstring 
        elif str(gid) !='':
                query = " SELECT 	id,domain_id,source,destination,modified,active,deleted,group_id FROM `virtual_recipients_bcc` WHERE  `group_id` in ("+gid+")   "+delstring

        else:
            if str(uid) !='0':
                query = " SELECT 	id,domain_id,source,destination,modified,active,deleted,group_id FROM `virtual_recipients_bcc` WHERE deleted='0'"
            else:
                query = " SELECT 	id,domain_id,source,destination,modified,active,deleted,group_id FROM `virtual_recipients_bcc`"
        try:
            print(query)
            db_config = read_db_config()
            conn = MySQLConnection(**db_config)
            cursor = conn.cursor()
            cursor.execute(query)
            fields=cursor.fetchall()
            counter = 0
            for field in fields:
                counter = counter + 1
                field=field+(counter, )
                dictdata.append(field)                
        except Error as error:
            print(error)
        finally:
            conn.commit()
            cursor.close()
            conn.close()
            return dictdata

    




    
    def edit_virtual_bccs_by_id(self, id,uid):
        query = " SELECT 	id,domain_id,source,destination,modified,active,deleted,group_id FROM `virtual_recipients_bcc` WHERE `id`='"+str(id)+"'"
        dictdata =[]
        #self.write(query)
        counter=0
        try:
            db_config = read_db_config()
            conn = MySQLConnection(**db_config)
            cursor = conn.cursor()
            cursor.execute(query)
            fields=cursor.fetchall()
            for field in fields:
                    counter = counter + 1
                    field=field+(counter, )
                    dictdata.append(field)   
            conn.commit()
        except Error as error:
            print(error)
        finally:
            cursor.close()
            conn.close()
            return dictdata