-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreference.py
83 lines (60 loc) · 2.18 KB
/
reference.py
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
from sqlalchemy import Column, Integer
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import Session
from IndicoWp.database import MySQLDatabaseAccess
from IndicoWp.config import Config, PROJECT_PATH
import logging
import pathlib
import json
Base = declarative_base()
class EventReference(Base):
__tablename__ = 'event_reference'
internal_id = Column(Integer, primary_key=True)
indico_id = Column(Integer)
wordpress_id = Column(Integer)
class ReferenceController:
def __init__(self):
self.session = MySQLDatabaseAccess.get_session() # type: Session
self.id_manager = IDManager()
def close(self):
self.id_manager.save()
def insert_reference(self, internal_id, indico_id, wordpress_id):
self.session.add(EventReference(
internal_id=internal_id,
indico_id=indico_id,
wordpress_id=wordpress_id
))
self.session.commit()
def select_all(self):
return self.session.query(EventReference)
def event_from_indico_event(self, indico_event):
# Replacing the indico id with the internal id generated by the id manager
internal_id = self.id_manager.get()
indico_event_copy = indico_event.copy()
indico_event_copy.id = internal_id
return indico_event_copy
class IDManager:
def __init__(self):
self.config = Config.get_instance()
self.logger = logging.getLogger('IdManagement')
self.path = pathlib.Path(PROJECT_PATH) / self.config['LOGGING']['ids']
self.counter = None
self.used = None
self.load()
def load(self):
with self.path.open(mode='r') as file:
content_dict = json.load(file)
self.counter = content_dict['counter']
self.used = content_dict['used']
def save(self):
with self.path.open(mode='w+') as file:
content_dict = {
'counter': self.counter,
'used': self.used
}
json.dump(content_dict, file)
def get(self):
current_id = self.counter
self.used.append(current_id)
self.counter += 1
return current_id