-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinsert_data.py
More file actions
161 lines (119 loc) · 5.08 KB
/
insert_data.py
File metadata and controls
161 lines (119 loc) · 5.08 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
from pathlib import Path
import pandas as pd
import numpy as np
from portfolio import portfolio, db
from portfolio.models import Languages, Content, Projects
import re
def lang_exists(c_lang):
cont_item = Languages.query.filter(Languages.language == c_lang).first()
if cont_item:
return cont_item
else:
return False
def content_exists(c_template, c_langid, c_var):
cont_item = Content.query.filter(Content.template == str(c_template or ''), Content.languageid == c_langid,
Content.variable == c_var).first()
if cont_item:
return cont_item
else:
return False
def project_exists(p_date, p_langid, p_projn):
proj_item = Projects.query.filter(Projects.date == p_date,
Projects.languageid == p_langid,
Projects.project_n == p_projn).first()
if proj_item:
return proj_item
else:
return False
def load_data(sheet):
return pd.read_excel(Path('content.xlsx'), sheet_name=sheet)
def insert_language(df):
"""This function is used to insert the languages of the portfolio
Keyword arguments:
df -- dataframe
Return: None
"""
lang_exists = Languages.query.filter(
Languages.language == df['language']).first()
if not lang_exists:
lang_item = Languages(language=df['language'])
db.session.add(lang_item)
db.session.commit()
def insert_data(df):
"""
This function would be used to insert content in the database
At the moment, will be importing one excel file
when I have time to improve the project, I will give the option of
doing it using the own website
"""
lang_id = Languages.query.filter(
Languages.language == df['language'].lower()).first().id
content_item = content_exists(df['template'], lang_id, df['variable'])
if not content_item:
content_item = Content(template=df['template'], languageid=lang_id,
variable=df['variable'], value=df['value'])
db.session.add(content_item)
else:
if content_item.value != df['value']:
content_item.value = df['value']
db.session.commit()
def insert_projects(df):
"""
This function would be used to insert content in the database
At the moment, will be importing one excel file
when I have time to improve the project, I will give the option of
doing it using the own website
"""
df['keywords'] = df['keywords'].lower()
lang_id = Languages.query.filter(
Languages.language == df['language'].lower()).first().id
title_slug = Projects.set_title_slug(lang_id, df['title'])
project_item = project_exists(df['date'], lang_id, df['project_n'])
if not project_item:
project_item = Projects(date=df['date'], languageid=lang_id, project_n=df['project_n'],
title=df['title'], title_slug=title_slug,
resume=df['resume'], exposition=df['exposition'],
action=df['action'], resolution=df['resolution'], keywords=df['keywords'],
link1=df['link1'], link2=df['link2'], link3=df['link3'],
link4=df['link4'], link5=df['link5'],
image_title=df['image_title'],
image1=df['image1'], image2=df['image2'], image3=df['image3'])
db.session.add(project_item)
else:
project_item.date = df['date']
project_item.project_n = df['project_n']
project_item.languageid = lang_id
project_item.title = df['title']
project_item.item_slug = title_slug
project_item.resume = df['resume']
project_item.exposition = df['exposition']
project_item.action = df['action']
project_item.resolution = df['resolution']
project_item.keywords = df['keywords']
project_item.link1 = df['link1']
project_item.link2 = df['link2']
project_item.link3 = df['link3']
project_item.link4 = df['link4']
project_item.link5 = df['link5']
project_item.image_title = df['image_title']
project_item.image1 = df['image1']
project_item.image2 = df['image2']
project_item.image3 = df['image3']
db.session.commit()
if __name__ == '__main__':
portfolio.app_context().push()
# Insert/update the languages of the portfolio
content_df = load_data('languages')
content_df.apply(insert_language, axis=1)
# Insert/update the texts of the website
content_df = load_data('portfolio')
content_df['template'].replace(np.nan, '', inplace=True)
content_df.apply(insert_data, axis=1)
# Insert/update the content of the portfolio
content_df = load_data('content')
content_df.replace(np.nan, '', inplace=True)
content_df.apply(insert_data, axis=1)
# Insert/update the projects of the portfolio
content_df = load_data('projects')
content_df.replace(np.nan, '', inplace=True)
content_df.apply(insert_projects, axis=1)