Sindbad~EG File Manager
from core import app
from flask import url_for,render_template_string
from datetime import datetime, date, time, timedelta
import random
import math,random
import time
from num2words import num2words
from core.library.auth import Auth
import base64
import requests
import pyqrcode
import io
import png
import barcode
from barcode import Code128
from barcode.writer import ImageWriter
from PIL import Image, ImageOps, ImageChops
from bs4 import BeautifulSoup
from collections import defaultdict
#settime working as global variable but commented for easy identification of this variable using class name
# global settime
# settime = 5
class Helper:
# PaymentGatewayAppMasterID = 7 # IAGES23 razorpay
# PaymentGatewayISTest = 1 # testmode = 1
DefaultCountryID = 101 # for india country code
AppType = "BADGE"
merge_pdf = 1 # this static variable value 1 - then one bye one or limit pdf file generated and merge all pdf files into single pdf
merge_pdf_per_limit = 50 # this static variable when merge_pdf is 1 then per pdf generate count
horizontal_view = 1
is_offline = 0
# badge_condition = [
# {"con_name": "Photo","column_name": "del_img_filename", "condition": "is_not_null"},
# {"con_name": "Photo","column_name": "badge_role", "condition": "is_null"}
# ]
badge_condition = {"Photo":"del_img_filename","Role": "badge_role"}
galley_sheet_header = [
{"order_no": 1, "header_name": "Del.No", "column_name": "an_delegate_no","is_show":1,"g_width":0},
{"order_no": 2, "header_name": "Name", "column_name": "full_name","is_show":1,"g_width":0},
{"order_no": 3, "header_name": "Name / Membership No", "column_name": "full_name_and_memberhsip_no","is_show":1,"g_width":0},
{"order_no": 4, "header_name": "Role", "column_name": "role","is_show":1,"g_width":0},
{"order_no": 5, "header_name": "Email/Mobile", "column_name": "email_and_mobile","is_show":1,"g_width":0},
{"order_no": 6, "header_name": "MC No / MC State", "column_name": "mc_number_and_mc_state","is_show":1,"g_width":0},
# {"order_no": 7, "header_name": "Signature", "column_name": "Del.No"}
]
def align_style(vertical_aline,horizontal_aline):
if vertical_aline == 'center' and horizontal_aline == 'center':
return 'top: 50%;left: 50%;transform: translate(-50%, -50%);-webkit-transform: translate(-50%,-50%);text-align:center'
elif vertical_aline == 'start' and horizontal_aline == 'center':
return 'top: 50%;left: 0;transform: translate(0,-50%);-webkit-transform: translate(0,-50%);'
elif vertical_aline == 'end' and horizontal_aline == 'center':
return 'top: 50%;left:0;-webkit-transform: translate(0,-50%);text-align: right;'
elif vertical_aline == 'start' and horizontal_aline == 'start':
return 'top: 0;left:0;-webkit-transform: translate(0,0);text-align: left;'
elif vertical_aline == 'center' and horizontal_aline == 'start':
return 'top: 0;left:0;-webkit-transform: translate(0,0);text-align: center;'
elif vertical_aline == 'end' and horizontal_aline == 'start':
return 'top: 0;left:0;-webkit-transform: translate(0,0);text-align: right;'
elif vertical_aline == 'end' and horizontal_aline == 'end':
return 'top: 100%;left:0;-webkit-transform: translate(0,-100%);text-align: right;'
elif vertical_aline == 'center' and horizontal_aline == 'end':
return 'top: 100%;left:0;-webkit-transform: translate(0,-100%);text-align: center;'
elif vertical_aline == 'start' and horizontal_aline == 'end':
return 'top: 100%;left:0;-webkit-transform: translate(0,-100%);text-align: left;'
else:
return ''
def test_login(data):
return "Virtual Conference - 1"
def footer_text():
return "Virtual Conference - 1"
def date_diff(d1, d2):
# fmt = '%H:%M'
d1 = date(d1,'%Y, %m, %d')
d2 = date(d2,'%Y, %m, %d')
diff = str(d1) - str(d2)
# print(diff)
return diff
def set_list(roles):
roles = roles.split(',')
return roles
def random4digt():
digits = "123456789"
otp_random = ""
for i in range(4) :
random.seed(time.process_time())
otp_random += digits[math.floor(random.random() * 9)]
return otp_random
def amountTowords(amount):
words_in_amount = num2words(amount, lang='en_IN')
return words_in_amount
def amountTodecimal(amount):
num = int(amount)
rs = f"{num:,}"
return rs
def email_mask(email):
if email:
lo = email.find('@')
if lo>0:
mask_email = email[0]+"*****"+email[lo-1:]
return mask_email
return email
def mobile_mask(mobile):
if mobile:
lo = len(mobile)
if lo>0:
mask = mobile[:2]+"******"+mobile[lo-2:]
return mask
return mobile
def formatINR(number):
s, *d = str(number).partition(".")
r = ",".join([s[x-2:x] for x in range(-3, -len(s), -2)][::-1] + [s[-3:]])
return "".join([r] + d)
def convert_html(data):
temp = app.jinja_env.from_string("{{login_content|safe}}").render(**data)
return render_template_string(temp,**data)
def random_otp():
digits = "123456789"
otp_random = ""
for i in range(4) :
random.seed(time.process_time())
otp_random += digits[math.floor(random.random() * 9)]
return otp_random
# Dec 23 ,2022 Siva To convert jinja tag to html
def convert_jinja_template(self,data1,data):
temp = app.jinja_env.from_string(data1).render(data=data)
return render_template_string(temp)
def base64encode(value):
basevalue = value.encode("ascii")
base64_bytes = base64.b64encode(basevalue)
return base64_bytes.decode("ascii")
def convert_b64(encode_txt):
sample_string = encode_txt
sample_string_bytes = sample_string.encode("ascii")
base64_bytes = base64.b64encode(sample_string_bytes)
base64_string = base64_bytes.decode("ascii")
return base64_string
def split_delegate_ids(delegate_ids):
if delegate_ids:
delegate_id = delegate_ids.split(',')
delegate_ids = len(delegate_id)
else:
delegate_ids = 0
return delegate_ids
def merge_tags(text,values):
for k, v in values.items():
text = text.replace('*|'+k+'|*', v)
return text
def convert_html_for_mail_screen(data):
temp = app.jinja_env.from_string("{{mail_content|safe}}").render(**data)
return render_template_string(temp,**data)
def leftrightstrip(text):
if text :
ltext = text.lstrip()
rtext = ltext.rstrip()
striped_text = rtext
return striped_text
else :
pass
def getcurrenttimestamp():
ct = datetime.now()
ts = ct.timestamp()
return ts
def ImageDecodeBase64(url):
if url:
bytes_string = base64.b64encode(requests.get(url).content)
aa = str(bytes_string, 'utf-8')
return aa
else :
return " "
def getQRCode(delegate_no):
c = pyqrcode.create(delegate_no)
s = io.BytesIO()
c.png(s,scale=6)
encoded = base64.b64encode(s.getvalue()).decode("ascii")
return encoded
def getQRCodeWithBg_old(delegate_no,color_code):
c = pyqrcode.create(delegate_no)
s = io.BytesIO()
# c.png(s,scale=6) dcf2fb a2b2da
if color_code:
c.png(s,scale=6, background=color_code)
else:
c.png(s,scale=6)
encoded = base64.b64encode(s.getvalue()).decode("ascii")
return encoded
def getQRCodeWithBg(delegate_no,color_code):
c = pyqrcode.create(delegate_no)
s = io.BytesIO()
# c.png(s,scale=6) dcf2fb a2b2da
if color_code:
c.png(s,scale=6, background=color_code)
else:
c.png(s,scale=6)
# Customize options for the barcode
options = {
'write_text': False, # Hide the text below the barcode
'module_height': 8, # Set the height of each module (barcode line)
'module_width': 0.3, # Set the width of each module (barcode line)
}
# Open the generated barcode image from BytesIO using Pillow
barcode_image = Image.open(s)
# Convert the image to RGBA mode (if not already in RGBA)
barcode_image = barcode_image.convert("RGBA")
# Create a mask for the white background
background = Image.new("RGBA", barcode_image.size, (0, 0, 0, 0))
diff = ImageChops.difference(barcode_image, background)
diff = ImageOps.invert(diff.convert('L'))
# Make the white background transparent
barcode_image.putalpha(diff)
# Save the barcode image to BytesIO
output = io.BytesIO()
barcode_image.save(output, format='PNG')
# Encode the image as base64
encoded_image = base64.b64encode(output.getvalue()).decode("ascii")
return encoded_image
def getQRCodeWithBgV1(delegate_no,bg_color,qr_color):
c = pyqrcode.create(delegate_no)
s = io.BytesIO()
qr_color = qr_color if qr_color else "#000000"
# c.png(s,scale=6) dcf2fb a2b2da
if bg_color:
c.png(s,scale=6, background=bg_color,module_color=qr_color)
encoded = base64.b64encode(s.getvalue()).decode("ascii")
return encoded
else:
c.png(s,scale=6,module_color=qr_color)
# Customize options for the barcode
options = {
'write_text': False, # Hide the text below the barcode
'module_height': 8, # Set the height of each module (barcode line)
'module_width': 0.3, # Set the width of each module (barcode line)
}
# Open the generated barcode image from BytesIO using Pillow
barcode_image = Image.open(s)
# Convert the image to RGBA mode (if not already in RGBA)
barcode_image = barcode_image.convert("RGBA")
# Create a mask for the white background
background = Image.new("RGBA", barcode_image.size, (0, 0, 0, 0))
diff = ImageChops.difference(barcode_image, background)
diff = ImageOps.invert(diff.convert('L'))
# Make the white background transparent
barcode_image.putalpha(diff)
# Save the barcode image to BytesIO
output = io.BytesIO()
barcode_image.save(output, format='PNG')
# Encode the image as base64
encoded_image = base64.b64encode(output.getvalue()).decode("ascii")
return encoded_image
# encoded = base64.b64encode(s.getvalue()).decode("ascii")
# return encoded
# def getBARCode(delegate_no):
# s = io.BytesIO()
# options = {
# 'write_text': False, # This option hides the text below the barcode
# 'module_height': 8, # Set the height of each module (barcode line)
# 'module_width': 0.3,
# # 'background': 'transparent' # Set background color to transparent
# }
# Code128(f"delegate_no:04", writer=ImageWriter()).write(s, options)
# encoded = base64.b64encode(s.getvalue()).decode("ascii")
# return encoded
def getBARCode(delegate_no):
s = io.BytesIO()
# Customize options for the barcode
options = {
'write_text': False, # Hide the text below the barcode
'module_height': 8, # Set the height of each module (barcode line)
'module_width': 0.3, # Set the width of each module (barcode line)
}
# Generate the barcode and write it to the BytesIO stream
Code128(str(delegate_no).zfill(4), writer=ImageWriter()).write(s, options)
# Open the generated barcode image from BytesIO using Pillow
barcode_image = Image.open(s)
# Convert the image to RGBA mode (if not already in RGBA)
barcode_image = barcode_image.convert("RGBA")
# Create a mask for the white background
background = Image.new("RGBA", barcode_image.size, (0, 0, 0, 0))
diff = ImageChops.difference(barcode_image, background)
diff = ImageOps.invert(diff.convert('L'))
# Make the white background transparent
barcode_image.putalpha(diff)
# Save the barcode image to BytesIO
output = io.BytesIO()
barcode_image.save(output, format='PNG')
# Encode the image as base64
encoded_image = base64.b64encode(output.getvalue()).decode("ascii")
return encoded_image
def get_url_base64(url):
return "data:image/jpg;base64," + base64.b64encode(requests.get(url).content).decode('ascii')
# return base64.b64encode(urlopen(url).read())
def getcurrenttimestamp():
ct = datetime.now()
ts = ct.timestamp()
return ts
def convert_html_for_badge_screen(badge_content,data):
# print("badge_content -1 ",badge_content)
temp = app.jinja_env.from_string(badge_content).render(**data)
return render_template_string(temp,**data)
def convert_html_for_badge_screen_2(badge_content,data):
# print("badge_content - 2",badge_content)
temp = app.jinja_env.from_string(badge_content).render(**data)
return render_template_string(temp,**data)
def conv_dele_no(value):
delegate_nos = None
from_del_no = None
to_del_no = None
if value:
if ',' in value or len(value) == 1 :
delegate_nos = value
elif '-' in value:
result = value.split('-')
if result and len(result)>0:
from_del_no = result[0] if result[0] else None
to_del_no = result[1] if result[1] else None
else:
delegate_nos = value
return delegate_nos,from_del_no,to_del_no
def update_sess(data,primary_id):
students = session.get('students', [])
# students = session['students']
for student in students:
if student[primary_id] == 3:
student.update(data)
return "success"
def getQRCodeForBadge(scan_code_size,length_scale,qr_val,is_preview):
demo_url = '/static/images/DEMO_QR_CODE.png'
result = ''
img_src = None
if is_preview == 1:
img_src = demo_url
if qr_val:
img_src = f"data:image/png;base64,{Helper.getQRCodeWithBg(qr_val,'#ffffff')}"
# img_src = f"data:image/png;base64,{Helper.getQRCode(qr_val)}"
if img_src:
result =f'<img style="width:{scan_code_size}{length_scale};height:{ scan_code_size}{length_scale};" src="{img_src}" >'
return result
def getQRCodeForBadgeV1(scan_code_size,length_scale,qr_val,is_preview,bg_color,qr_color):
demo_url = '/static/images/DEMO_QR_CODE.png'
result = ''
img_src = None
if is_preview == 1:
img_src = demo_url
if qr_val:
img_src = f"data:image/png;base64,{Helper.getQRCodeWithBgV1(qr_val,bg_color,qr_color)}"
# img_src = f"data:image/png;base64,{Helper.getQRCode(qr_val)}"
if img_src:
result =f'<img style="width:{scan_code_size}{length_scale};height:{ scan_code_size}{length_scale};" src="{img_src}" >'
return result
def getBarCodeForBadge(scan_code_size,length_scale,bar_val,is_preview):
demo_url = '/static/images/DEMO_QR_CODE.png'
result = ''
img_src = None
if is_preview == 1:
img_src = demo_url
if bar_val:
img_src = f"data:image/png;base64,{Helper.getBARCode(bar_val)}"
# img_src = f"data:image/png;base64,{Helper.getQRCode(qr_val)}"
if img_src:
result =f'<img style="height:{ scan_code_size}{length_scale};" src="{img_src}" >'
return result
def getPhotoForBadge(photo_width,photo_height,length_scale,photo_radius,photo_val,is_preview):
print("------",photo_width,"-",photo_height,"-",length_scale,"-",photo_radius,"-",photo_val,"-",is_preview)
demo_url = '/static/images/empty_photo.png'
result = ''
img_src = None
if is_preview == 1:
img_src = demo_url
if photo_val:
img_src = photo_val
if img_src:
# result =f'<img style="width:{photo_width}{length_scale};height:{ photo_height}{length_scale}; /* border: 2px solid #000 !important;*/-webkit-border-radius: 10% !important;border-radius: 10% !important; -webkit-box-shadow: 0px 0px 0px 1px #000; " src="{img_src}" >'
# result =f'<img style="width:{photo_width}{length_scale};height:{ photo_height}{length_scale}; /* border: 2px solid #000 !important; /* -webkit-border-radius: 10% !important;border-radius: 10% !important; -webkit-box-shadow: 0px 0px 0px 1px #000; " src="{img_src}" >'
# tancon
# last use
# result =f'<img style="width:{photo_width}{length_scale};height:{ photo_height}{length_scale}; /* border: 2px solid #000 !important;*/-webkit-border-radius: 14% !important;border-radius: 14% !important; /*-webkit-box-shadow: 0px 0px 0px 1px #000;*/ " src="{img_src}" >'
result =f'<img style="width:{photo_width}{length_scale};height:{ photo_height}{length_scale}; /* border: 2px solid #000 !important;*/-webkit-border-radius: {photo_radius}{length_scale} !important;border-radius: {photo_radius}{length_scale} !important; /*-webkit-box-shadow: 0px 0px 0px 1px #000;*/ " src="{img_src}" >'
# normal
# result =f'<img style="width:{photo_width}{length_scale};height:{ photo_height}{length_scale}; /* border: 2px solid #000 !important;*/-webkit-border-radius: 10% !important;border-radius: 10% !important; -webkit-box-shadow: 0px 0px 0px 1px #000; " src="{img_src}" >'
# result =f"<div style=\"width: {photo_width}{length_scale};position: relative;margin-bottom: 2px;height: { photo_height}{length_scale};\"><div style=\"position: absolute;background-image: url('{img_src}');background-repeat: no-repeat;text-align: center;background-size: cover;width: {photo_width}{length_scale};height: {photo_height}{length_scale};\"></div></div>"
# result =f"<div style=\"background-image: url('{img_src}');background-repeat: no-repeat;text-align: center;background-size: cover;width: {photo_width}{length_scale};height: {photo_height}{length_scale};\"></div>"
return result
def getQRCodeForCustom(qr_val):
result = ''
img_src = None
if qr_val:
qr_val = "tel:"+qr_val
result = f"data:image/png;base64,{Helper.getQRCodeWithBg(qr_val,'#ffffff')}"
return result
def addDictItems(dict_var,items):
for key, value in items.items():
dict_var[key] = value
return dict_var
# def extract_div_by_class_and_id(html_content, class_name, div_id):
# soup = BeautifulSoup(html_content, 'html.parser')
# # Find the outer <div> with specified class and id
# target_div = soup.find('div', {'class': class_name, 'id': div_id})
# if target_div:
# # Remove all child elements
# for child in target_div.find_all(recursive=False):
# child.extract()
# return str(target_div)
# return ''
# def extract_div_by_class_and_id(html_content, class_name, div_id):
# soup = BeautifulSoup(html_content, 'html.parser')
# # Find all divs with the specified class and id (if there are multiple)
# target_divs = soup.find_all('div', {'class': class_name , 'id': div_id})
# result = []
# for target_div in target_divs:
# # Remove all child elements
# for child in target_div.find_all(recursive=False):
# child.extract()
# # Append the cleaned <div> as a string to the result list
# result.append(str(target_div))
# # Join the result list to return the multiple divs as a single string
# return ''.join(result)
def extract_div_by_class_and_id(html_content, class_name, div_id, horizontal_view):
soup = BeautifulSoup(html_content, 'html.parser')
# Find all divs with the specified class and id (if there are multiple)
target_divs = soup.find_all('div', {'class': class_name, 'id': div_id})
# Clean the target divs by removing their child elements
for target_div in target_divs:
for child in target_div.find_all(recursive=False):
child.extract()
# If horizontal_view == 1, include the main-page wrapper with the cleaned badge_frame divs
if horizontal_view == 1:
main_page_div = soup.find('div', {'class': 'main-page'})
if main_page_div:
# Replace the content of main-page with cleaned badge_frame divs
for div in target_divs:
main_page_div.append(div) # Append the cleaned divs back inside main-page
return str(main_page_div)
else:
return '' # Return an empty string if no main-page wrapper exists
else:
# If horizontal_view != 1, return just the cleaned badge_frame divs
return ''.join([str(div) for div in target_divs])
def sorted_counter_list(data):
counter_list = sorted({int(item["counter"]) for item in data if item["counter"] is not None})
counter_list = None if counter_list == [] else counter_list
return counter_list
def sorted_batch_list(data):
counter_dict = {}
for item in data:
counter_value = item.get("counter")
if counter_value is not None:
# Convert to integer if it's a string number
counter_value = int(counter_value) if isinstance(counter_value, str) and counter_value.isdigit() else counter_value
counter_dict.setdefault(counter_value, set()).add(item["batch"])
return {k: sorted(list(v)) for k, v in counter_dict.items()}
def group_by_counter_and_batch(data):
# Initialize a defaultdict to hold lists of people grouped by counter and batch
grouped_data = defaultdict(list)
for item in data:
counter_value = item.get("counter")
batch_value = item.get("batch")
if counter_value and batch_value:
# Create a key of the form 'counter-batch' and append the item to the list
grouped_data[f"{counter_value}-{batch_value}"].append(item)
return dict(grouped_data) # Convert defaultdict to a regular dictionary for final output
app.jinja_env.globals.update(Helper=Helper)
Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists