Sindbad~EG File Manager

Current Path : /home/numerotech/mcq.numerotech.com/MCQ_APP/core_old/library/
Upload File :
Current File : //home/numerotech/mcq.numerotech.com/MCQ_APP/core_old/library/Xlsexport.py

from flask import Response
from os import listdir
import io 
from io import StringIO
from io import BytesIO
import xlsxwriter
from xlsxwriter import Workbook
import xlwt
import flask_excel as excel
from collections import defaultdict

class XLSEXPORT:
    # **** Sample input data *****
    # @app.route('/sample_single_xls_export')
    # def dwonload_sample_single_xl():
        # header = {'full_name':'Name','email':'Email Id','mobile':'Mobile Number'}
        # rows = [
        #         {
        #         "full_name":"Mukesh Kumar",
        #         "email":"mukesh@numerotec.com",
        #         "Phone":"6379348963",
        #         },
        #         {
        #         "full_name":"Sridhar r",
        #         "email":"sridhar.r@numerotec.com",
        #         "Phone":"9988776655",
        #         },
        #     ]
        # file_name = "Sample_single"
        # data = XLSEXPORT().export_singlesheet_xls(rows,file_name,header)
        # return data

    # **** Sample input data *****
    # @app.route('/sample_multiple_xls_export')
    # def dwonload_sample_multiple_xl():
        # header = {'full_name':'Name','email':'Email Id','mobile':'Mobile Number'}
        # sheet_name = ['abstract','delegate']
        # rows = {'abstract':[
        #         {
        #         "full_name":"Mukesh Kumar",
        #         "email":"mukesh@numerotec.com",
        #         "Phone":"6379348963",
        #         },
        #         {
        #         "full_name":"Sridhar r",
        #         "email":"sridhar.r@numerotec.com",
        #         "Phone":"9988776655",
        #         },
        #     ],'delegate':[
        #         {
        #         "full_name":"Karthick",
        #         "email":"karthick@numerotec.com",
        #         "Phone":"6444332483",
        #         },
        #         {
        #         "full_name":"Sridhar r",
        #         "email":"sridhar.r@numerotec.com",
        #         "Phone":"9988776655",
        #         },
        #     ]}
        # file_name = "Sample_multiple"
        # data = XLSEXPORT().export_multiplesheet_xls(sheet_name,rows,file_name,header)
        # return data

    def export_singlesheet_xls(self,row_data,file_name,header=None):
        output   = io.BytesIO()
        Workbook = xlwt.Workbook()
        sh = Workbook.add_sheet("sheet1")
        idx      = 0
        a        = 0
        if row_data:
            if header:
                row_keys = list(header.keys())      
                header   = list(header.values())      
            else:
                header   = list(row_data[0].keys())
                row_keys = header
            for row in header:
                sh.write(0,a,row)
                a+=1
            b=0
            for row in row_data:
                for i in row_keys:
                    sh.write(idx+1, b, row[i])
                    b+=1
                b=0
                idx+=1
            Workbook.save(output)
            output.seek(0)
            return Response(output, mimetype="application/ms-excel", headers={"Content-Disposition":"attachment;filename="+str(file_name)+".xls"})
        return None
        
    def export_multiplesheet_xls(self,sheet_names,row_data,file_name,header=None):
        output   = io.BytesIO()
        Workbook = xlwt.Workbook()
        style_1=xlwt.easyxf('alignment: wrap True;''pattern: pattern solid, fore_colour yellow;''font: colour black, bold True, name Calibri, height 200; align: vert centre, horiz centre;border: left thin,right thin,top thin,bottom thin')
        if sheet_names and row_data:
            for d in sheet_names:
                sheet_name =  d[:27] + '..' * (len(d) > 27)
                sh = Workbook.add_sheet(sheet_name)
                idx      = 0
                a = 0
                print(header)
                if header:
                    row_keys = list(header.keys())      
                    header1   = list(header.values())      
                else:
                    header1   = list(row_data[d][0].keys())
                    row_keys = header1
                for row in header1:
                    sh.write(0,a,row,style=style_1)
                    a+=1
                b=0
                for row in row_data[d]:
                    for i in row_keys:
                        sh.write(idx+1, b, row[i])
                        b+=1
                    b=0
                    idx+=1
            Workbook.save(output)
            output.seek(0)
            return Response(output, mimetype="application/ms-excel", headers={"Content-Disposition":"attachment;filename="+str(file_name)+".xls"})
        return None

    def evaluation_summary_xls(self,abstract_data,marks_data,evaluator,abs_type,file_name):
        abstracts    =   defaultdict(list) # sub list with key 
        evaluator_list    =   defaultdict(list) # sub list with key 
        marks_list       =   defaultdict(list)
        categories   =   []  
        for r in abstract_data:
            abstracts[r["cat_name"]].append(r)
            if r["cat_name"] not in categories:
                categories.append(r["cat_name"])
        for r in marks_data:
            marks_list[r["cat_name"]].append(r)
        for r in evaluator:
            evaluator_list[r["cat_name"]].append(r)
        output = io.BytesIO()
        Workbook =xlwt.Workbook()
        for cat in categories:
            full_name=[]
            for r in evaluator_list[cat]:
                f_name=r['full_name']
                full_name.append(f_name)
            if abs_type == "IC":
                eva_length = len(full_name)+11
                col_name       = ["Abs_no","Title","Synopsis","Chief instrutor","Date of Birth","Age","Membership No","Email","Mobile","Presentation type","Co-Instructor"]+full_name + ["Assigned Evaluators","Completed Evaluators","Total","Average"]
                export_columns = ["abs_no","title","synopsis","presenting_author","dob","age","presenting_membershipno","presenting_email","presenting_mobile","presenting_type","co_author"] + full_name + ["total_eva","evaluated","total_marks","percentage"]
            else:
                eva_length = len(full_name)+11
                col_name       = ["Abs_no",  "Title","Synopsis","Chief author","presenting author","Age","Membership No","Email","Mobile","Presentation type","Co-Author"]+full_name + ["Assigned Evaluators","Completed Evaluators","Total","Average"]
                export_columns = ["abs_no","title","synopsis","chief_author","presenting_author","age","presenting_membershipno","presenting_email","presenting_mobile","presenting_type","co_author"] + full_name + ["total_eva","evaluated","total_marks","percentage"]
            marks_dict    =   defaultdict(list) # sub list with key 
            for r in marks_list[cat]:
                marks_dict[r["e_full_name"]].append(r)
            sheet_name = cat.replace("/","").strip()
            sheet_name = sheet_name[:25] + '..' * (len(sheet_name) > 25)
            sh = Workbook.add_sheet(sheet_name)
            idx      = 0
            a = 0
            for row in col_name:
                sh.write(0,a,row)
                sno_val = 0
                a+=1
            total_eva = len(full_name)+11
            col_len = len(export_columns)
            style=xlwt.easyxf('pattern: pattern solid, fore_colour yellow;' 'font: colour black, bold True, name Calibri, height 180; align: vert centre, horiz centre;border: left thin,right thin,top thin,bottom thin')
            for data in abstracts[cat]:
                b=0
                c=0
                d=0
                for i in export_columns:
                    if int(col_len) >= int(c):
                        if c > 10 :
                            if int(c) < int(eva_length):
                                for j in marks_dict[i]:
                                    if data['abs_id'] == j["abs_id"]:
                                        if data["pa_user_id"] == j["eva_user_id"]:
                                            sh.write(idx+1, b, 'N/A',style=style)
                                        else:
                                            sh.write(idx+1, b, j["marks_total"])
                                        d+=1
                            else:
                                sh.write(idx+1, b, data[i])
                        else:
                            sh.write(idx+1, b, data[i])
                        b+=1
                        if int(total_eva) == d:
                            c = 0
                            d = 0
                        else:
                            c+=1
                    else:
                        c = 0
                        d = 0
                b=0
                idx+=1
        Workbook.save(output)
        output.seek(0)
        return Response(output, mimetype="application/ms-excel", headers={"Content-Disposition":"attachment;filename="+file_name+".xls"})

Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists