7
7
from icalendar import Event as vEvent
8
8
from sedate import utcnow , to_timezone
9
9
from sqlalchemy import (
10
- Column , Boolean , SmallInteger , Enum , Text , Interval , ForeignKey , or_ , and_ )
10
+ Column , Boolean , SmallInteger , Enum , Text , Interval , ForeignKey , or_ , and_ ,
11
+ select )
11
12
from sqlalchemy .ext .hybrid import hybrid_property
12
- from sqlalchemy .orm import relationship , backref , object_session
13
+ from sqlalchemy .orm import relationship , backref , object_session , join
13
14
from uuid import uuid4
14
15
15
16
from onegov .core .mail import Attachment
16
17
from onegov .core .orm import Base
17
18
from onegov .core .orm .mixins import TimestampMixin
18
19
from onegov .core .orm .types import UUID , UTCDateTime
19
20
from onegov .fsi import _
21
+ from onegov .fsi .models .course import Course
20
22
from onegov .fsi .models .course_attendee import CourseAttendee
21
23
from onegov .fsi .models .course_subscription import CourseSubscription
22
24
from onegov .fsi .models .course_subscription import subscription_table
23
25
from onegov .search import ORMSearchable
24
26
25
27
from typing import TYPE_CHECKING
28
+
26
29
if TYPE_CHECKING :
27
30
from .course import Course
28
31
from .course_notification_template import (
40
43
41
44
# for forms...
42
45
def course_status_choices (request = None , as_dict = False ):
43
-
44
46
if request :
45
47
translations = (
46
48
request .translate (v ) for v in COURSE_EVENT_STATUSES_TRANSLATIONS )
@@ -54,14 +56,14 @@ def course_status_choices(request=None, as_dict=False):
54
56
55
57
56
58
class CourseEvent (Base , TimestampMixin , ORMSearchable ):
57
-
58
59
default_reminder_before = datetime .timedelta (days = 14 )
59
60
60
61
__tablename__ = 'fsi_course_events'
61
62
62
63
es_properties = {
64
+ # expression for name and desc implementiert aber subquery issue
63
65
'name' : {'type' : 'localized' },
64
- 'description' : {'type' : 'localized' },
66
+ # 'description': {'type': 'localized'},
65
67
'location' : {'type' : 'localized' },
66
68
'presenter_name' : {'type' : 'text' },
67
69
'presenter_company' : {'type' : 'text' },
@@ -89,6 +91,41 @@ def title(self):
89
91
def name (self ):
90
92
return self .course .name
91
93
94
+ @name .expression
95
+ def name (cls ):
96
+ # both variants lead to the same error:
97
+ # sqlalchemy.exc.NotSupportedError: (psycopg2.errors.FeatureNotSupported) cannot use subquery in column generation expression
98
+ # subquery issue probably produced due where clause
99
+
100
+ # expr = select([Course.name])
101
+ # expr = expr.where(Course.id == cls.course_id)
102
+ # expr = expr.label('name')
103
+ # return expr
104
+
105
+ # expr = (select([Course.name]).where(Course.id == cls.course_id).
106
+ # select_from(join(cls, Course)).label('name'))
107
+
108
+ # the variant below are closer to the final version (explicit
109
+ # join) but throw: 'Can't find any foreign key relationships between
110
+ # 'Select object' and 'fsi_courses'
111
+ expr = select ([Course .name ]).select_from (cls ).join (
112
+ Course ).where (Course .id == cls .course_id ).label ('name' )
113
+
114
+ # throws: subquery in FROM must-have an alias
115
+ # j = select([cls.course_id]).join(Course, Course.id ==
116
+ # cls.course_id).alias('j')
117
+ # expr = select([Course.name]).select_from(j).label('name')
118
+
119
+ # example https://docs.sqlalchemy.org/en/20/orm/queryguide/select.html#joining-to-subqueries
120
+ # throwing: TypeError: 'DeclarativeMeta' object is not iterable
121
+ # subq = select(Address).where(Address.email_address == "[email protected] ").subquery()
122
+ # stmt = select(User).join(subq, User.id == subq.c.user_id)
123
+ # subquery = select(cls).where(cls.course_id == Course.id).subquery()
124
+ # expr = select(Course).join(subquery, Course.id == subquery.course_id)
125
+
126
+ print (expr )
127
+ return expr
128
+
92
129
@property
93
130
def lead (self ):
94
131
return (
@@ -101,6 +138,13 @@ def lead(self):
101
138
def description (self ):
102
139
return self .course .description
103
140
141
+ @description .expression
142
+ def description (cls ):
143
+ expr = select ([Course .description ])
144
+ expr = expr .where (Course .id == cls .course_id )
145
+ expr = expr .label ('description' )
146
+ return expr
147
+
104
148
def __str__ (self ):
105
149
start = to_timezone (
106
150
self .start , 'Europe/Zurich' ).strftime ('%d.%m.%Y %H:%M' )
0 commit comments