-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathstore_management.py
More file actions
128 lines (102 loc) · 3.85 KB
/
Copy pathstore_management.py
File metadata and controls
128 lines (102 loc) · 3.85 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
import os
from flask import session, g
from models import Store, db
from config import Config
def normalize_url(url):
"""Normalize a URL by removing protocol and trailing slashes."""
normalized_url = url.lower()
if '://' in normalized_url:
normalized_url = normalized_url.split('://', 1)[1]
return normalized_url.rstrip('/')
def get_current_store():
"""Get the current store from session or create a default one."""
# Check if we already have the store in g
if hasattr(g, 'current_store'):
return g.current_store
# Check if we have a store_id in session
store_id = session.get('current_store_id')
if store_id:
# Get store by ID
store = Store.query.get(store_id)
if store:
g.current_store = store
# Update environment variables to match the current store
os.environ['SHOPIFY_STORE_URL'] = store.url
if store.access_token:
os.environ['SHOPIFY_ACCESS_TOKEN'] = store.access_token
# Update Config object
Config.SHOPIFY_STORE_URL = store.url
Config.SHOPIFY_ACCESS_TOKEN = store.access_token
return store
# No store in session, try to get from environment variable
store_url = Config.SHOPIFY_STORE_URL
if store_url:
normalized_url = normalize_url(store_url)
# Find store by URL
store = Store.query.filter_by(url=normalized_url).first()
if store:
# Store found, save to session and g
session['current_store_id'] = store.id
g.current_store = store
return store
else:
# Create a new store
store = create_store_from_env()
if store:
session['current_store_id'] = store.id
g.current_store = store
return store
# Try to get the first store if no store is selected
store = Store.query.first()
if store:
session['current_store_id'] = store.id
g.current_store = store
# Update environment variables to match the current store
os.environ['SHOPIFY_STORE_URL'] = store.url
if store.access_token:
os.environ['SHOPIFY_ACCESS_TOKEN'] = store.access_token
# Update Config object
Config.SHOPIFY_STORE_URL = store.url
Config.SHOPIFY_ACCESS_TOKEN = store.access_token
return store
# No store found or created
return None
def set_current_store(store_id):
"""Set the current store in the session."""
store = Store.query.get(store_id)
if store:
session['current_store_id'] = store.id
# Update environment variables
os.environ['SHOPIFY_STORE_URL'] = store.url
if store.access_token:
os.environ['SHOPIFY_ACCESS_TOKEN'] = store.access_token
# Clear g.current_store if it exists
if hasattr(g, 'current_store'):
delattr(g, 'current_store')
return store
return None
def create_store_from_env():
"""Create a store from environment variables."""
store_url = Config.SHOPIFY_STORE_URL
if not store_url:
return None
normalized_url = normalize_url(store_url)
store_name = normalized_url.split('.')[0]
access_token = Config.SHOPIFY_ACCESS_TOKEN
store = Store(
name=store_name,
url=normalized_url,
access_token=access_token
)
db.session.add(store)
db.session.commit()
return store
def filter_query_by_store(query, model):
"""Filter a query by the current store."""
store = get_current_store()
if store:
return query.filter(model.store_id == store.id)
return query
def get_all_stores():
"""Get all stores."""
return Store.query.all()