You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

152 lines
3.4 KiB

# coding=utf-8
import os
import django
import sys
from django.core.files import File
sys.path.append("/var/www/projects/lms/")
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "lms.settings")
django.setup()
import openpyxl
from finance.models import Price
from access.models import User
EXEL_PATH = 'data.xlsx'
USERS = {'point': 'id'}
USERS_P = {}
USERS_E = {}
SERVICES = {'point': 'id'}
BOOK = openpyxl.load_workbook(filename=EXEL_PATH)
SHEET = BOOK.active
def check_exists_row(index):
columns = ['A', 'B', 'C', 'D', 'E', 'F', 'J', 'H', 'I', 'J']
for i in columns:
if SHEET['%s%i' % (i, index)].value:
return True
return False
def get_user_name(id):
result = u''
try:
user = User.objects.get(id=id)
except User.DoesNotExist:
return u'Пользователь не найден'
except ValueError:
return u'Студент'
else:
result = user.get_full_name()
return result
def get_user_email(id):
result = u''
try:
user = User.objects.get(id=id)
except User.DoesNotExist:
return u'Пользователь не найден'
except ValueError:
return u'Студент'
else:
result = user.email
return result
def get_user_phone(id):
result = u''
try:
user = User.objects.get(id=id)
except User.DoesNotExist:
return u'Пользователь не найден'
except ValueError:
return u'Студент'
else:
result = user.get_phone()
return result
def get_service_data(id):
result = u''
try:
service = Price.objects.get(id=id)
except Price.DoesNotExist:
return u'Пользователь не найден'
except ValueError:
return u'Услуга'
else:
result = service.get_name()
return result
#######
def read_users():
i = 1
while check_exists_row(i):
box = 'I%i' % i
USERS[box] = SHEET[box].value
USERS_P['J%s' % i] = USERS[box]
USERS_E['K%s' % i] = USERS[box]
print u'Читаю'
print box
print USERS[box]
i += 1
def read_services():
i = 1
while check_exists_row(i):
box = 'H%i' % i
SERVICES[box] = SHEET[box].value
print u'Читаю'
print box
print SERVICES[box]
i += 1
def write_users():
for box, value in USERS.items():
data = get_user_name(value)
try:
SHEET[box] = data
except openpyxl.utils.exceptions.CellCoordinatesException:
print u'Ошибка'
print u'Пишу'
print box
print data
for box, value in USERS_P.items():
data = get_user_phone(value)
try:
SHEET[box] = data
except openpyxl.utils.exceptions.CellCoordinatesException:
print u'Ошибка'
for box, value in USERS_E.items():
data = get_user_email(value)
try:
SHEET[box] = data
except openpyxl.utils.exceptions.CellCoordinatesException:
print u'Ошибка'
def write_services():
for box, value in SERVICES.items():
data = get_service_data(value)
try:
SHEET[box] = data
except openpyxl.utils.exceptions.CellCoordinatesException:
print u'Ошибка'
print u'Пишу'
print box
print data
read_users()
write_users()
read_services()
write_services()
BOOK.save(EXEL_PATH)