-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexcelHandler.py
More file actions
126 lines (98 loc) · 4.59 KB
/
excelHandler.py
File metadata and controls
126 lines (98 loc) · 4.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import json
from sqlite3 import IntegrityError
import openpyxl as xl
def handlingFields(list: list, *args):
"""
if each value of *args is not equal 'none', and,
list dosen't contain the list of args, then
append to the list the list of args.
"""
# for item in args:
# if item == 'none':
# return
if args not in list:
list.append(args)
def proccess_workbook(filename, sheetname):
wb = xl.load_workbook(filename)
sheet = wb[sheetname]
list_items = []
for row in range(2, sheet.max_row + 1):
item = {}
for col in range(1, sheet.max_column):
# create key value bears
key = sheet.cell(1, col).value.lower().strip().replace(' ', '_')
value = str(sheet.cell(row, col).value).lower().strip()
item[key] = value
# append rows
list_items.append(item)
for item in list_items:
# remove unwanted keys
del item['us']
del item['serial_number']
del item['المكان']
models = []
cpus = []
rams = []
hdds = []
items = []
for item in list_items:
handlingFields(models, 'labtop', item['manufacturer'], item['model'])
if item['cpu'] != 'none':
if item['cpu'].__contains__('intel'):
val = item['cpu'].split('-')
handlingFields(cpus, val[0], val[1])
else:
val = item['cpu'].split(' ')
handlingFields(cpus, val[0], val[1])
handlingFields(rams, item['ram'], item['ram-type'])
handlingFields(hdds, item['hdd'], item['hdd1_type'])
handlingFields(items, 'labtop', item['manufacturer'], item['model'], item['cpu'],
item['ram'], item['ram-type'], item['hdd'], item['hdd1_type'],
item['gpu'], item['touch_screen'], item['rotation_360deg'],
item['illuminated_keyboard'], item['original_windows'], item['screen_resolution'],
item['screen_size'], item['sound_type'], item['السعر'], item['خصم'], item['note'], True)
return models, cpus, rams, hdds, items, list_items
from items.models import *
models, cpus, rams, hdds, items, list_items = proccess_workbook('op.xlsx', 'الشيت القديم')
# generate items
# for i in list_items:
# # models
# typee, created = Type.objects.get_or_create(type='labtop')
# manufacturer, created = Manufacturer.objects.get_or_create(type=i['manufacturer'])
# model, created = Models.objects.get_or_create(type=typee, manufacturer=manufacturer, name=i['model'])
# # cpus
# cpu = None
# gen = None
# if i['cpu'].__contains__('intel'):
# val = i['cpu'].split('-')
# cpu, created = CPU.objects.get_or_create(type=val[0])
# gen, created = CPUGeneration.objects.get_or_create(cpu=cpu, generation=val[1])
# else:
# val = i['cpu'].split(' ')
# if val[0] != 'none':
# cpu, created = CPU.objects.get_or_create(type=val[0])
# gen, created = CPUGeneration.objects.get_or_create(cpu=cpu, generation=' '.join(val[1:]))
# # # rams
# if i['ram-type'] != 'none':
# ram, created = Ram.objects.get_or_create(type=i['ram-type'], size=i['ram'])
# # # hdds
# hdd = None
# if i['hdd1_type'] != 'none':
# hdd, created = HDD.objects.get_or_create(type=i['hdd1_type'], size=i['hdd'])
# resolution_type, created = ScreenResolution.objects.get_or_create(type=i['screen_resolution'])
# sound_type, created = SoundType.objects.get_or_create(type=i['sound_type'])
# gpu, created = GPU.objects.get_or_create(type=i['gpu'])
# try:
# item, created = Items.objects.get_or_create(model=model, cpu=gen,
# touch_screen=True if ['touch_screen'] in ['y', 'yes'] else False,
# rotation=i['rotation_360deg'],
# illuminated_keyboard=True if i['illuminated_keyboard'] in ['y', 'yes'] else False,
# original_windows=True if i['original_windows'] in ['y', 'yes'] else False,
# screen_resolution=resolution_type, screen_size=i['screen_size'],
# sound_type=sound_type, price=i['السعر'], disc=i['خصم'] if i['خصم'] != 'none' else 0,
# note=i['note'], is_available=True)
# item.hdd.add(hdd)
# item.ram.add(ram)
# item.gpu.add(gpu)
# except:
# pass