Skip to content

Commit ccbf117

Browse files
committed
Revert "DEV ONLY Issue to join data from other table in hybrid expression in CourseEvent"
This reverts commit e6fcdb1.
1 parent e6fcdb1 commit ccbf117

File tree

14 files changed

+40
-301
lines changed

14 files changed

+40
-301
lines changed

src/onegov/ballot/models/election/election.py

-1
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,6 @@ def counted(self) -> bool:
137137

138138
return (sum(1 for r in self.results if r.counted) == count)
139139

140-
# tschupre example for expression
141140
@counted.expression # type:ignore[no-redef]
142141
def counted(cls) -> 'ColumnElement[bool]':
143142
expr = select([

src/onegov/core/orm/session_manager.py

-3
Original file line numberDiff line numberDiff line change
@@ -675,9 +675,6 @@ def ensure_schema_exists(self, schema: str) -> None:
675675
base.metadata.create_all(conn)
676676

677677
declared_classes.update(base._decl_class_registry.values())
678-
# declared_classes.update(
679-
# base.registry._class_registry.values()) # upgrading
680-
# to sqlalchemy 1.4.x
681678

682679
conn.execute('COMMIT')
683680
finally:

src/onegov/directory/models/directory_entry.py

+2-14
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,15 @@ class DirectoryEntry(Base, ContentMixin, CoordinatesMixin, TimestampMixin,
2323
__tablename__ = 'directory_entries'
2424

2525
es_properties = {
26-
# 'keywords': {'type': 'keyword'}, # produce a literal issue when
27-
# using sqlalchemy 1.4
26+
'keywords': {'type': 'keyword'},
2827
'title': {'type': 'localized'},
2928
'lead': {'type': 'localized'},
3029
'_directory_id': {'type': 'keyword'},
3130

3231
# since the searchable text might include html, we remove it
3332
# even if there's no html -> possibly decreasing the search
3433
# quality a bit
35-
# 'text': {'type': 'localized_html'}
34+
'text': {'type': 'localized_html'}
3635
}
3736

3837
@property
@@ -104,25 +103,14 @@ def keywords(self):
104103
def keywords(self, value):
105104
self._keywords = {k: '' for k in value} if value else None
106105

107-
# @property
108106
@hybrid_property
109107
def text(self):
110108
return self.directory.configuration.extract_searchable(self.values)
111109

112-
# @text.expression
113-
# def text(cls):
114-
# expr = ''
115-
# print(f'*** hybrid prop text: {expr}')
116-
# return expr
117-
118110
@property
119111
def values(self):
120112
return self.content and self.content.get('values', {})
121113

122-
@hybrid_property
123-
def values(cls):
124-
return cls.content['values']
125-
126114
@values.setter
127115
def values(self, values):
128116
self.content = self.content or {}

src/onegov/feriennet/models/activity.py

+1-26
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
from functools import cached_property
22

3-
from sqlalchemy import func, text, select, alias, Column, and_
43
from sqlalchemy.ext.hybrid import hybrid_property
5-
from sqlalchemy.orm import Query, column_property
64

75
from onegov.activity import Activity, ActivityCollection, Occasion
86
from onegov.activity import PublicationRequestCollection
@@ -14,7 +12,6 @@
1412
from onegov.org.models.ticket import OrgTicketMixin
1513
from onegov.search import SearchableContent
1614
from onegov.ticket import handlers, Handler, Ticket
17-
from onegov.user import User
1815

1916

2017
class VacationActivity(Activity, CoordinatesExtension, SearchableContent):
@@ -27,7 +24,7 @@ class VacationActivity(Activity, CoordinatesExtension, SearchableContent):
2724
'title': {'type': 'localized'},
2825
'lead': {'type': 'localized'},
2926
'text': {'type': 'localized_html'},
30-
# 'organiser': {'type': 'text'}
27+
'organiser': {'type': 'text'}
3128
}
3229

3330
@property
@@ -42,18 +39,8 @@ def es_public(self):
4239
def es_skip(self):
4340
return self.state == 'preview'
4441

45-
@hybrid_property
46-
def lead(self):
47-
return self.meta['lead'].astext
48-
49-
@hybrid_property
50-
def text(self):
51-
return self.content['text'].astext
52-
53-
# @property
5442
@hybrid_property
5543
def organiser(self):
56-
print(' *** tschupre property organizer()')
5744
organiser = [
5845
self.user.username,
5946
self.user.realname
@@ -80,18 +67,6 @@ def organiser(self):
8067

8168
return organiser
8269

83-
@organiser.expression
84-
def organiser(cls):
85-
expr = (
86-
Query(User.username, User.realname).
87-
select_from(cls).join(User).
88-
filter(cls.username == User.username).
89-
filter(func.concat_ws(' ', User.username, User.realname)).
90-
as_scalar()
91-
)
92-
print(expr)
93-
return expr.label('organizer')
94-
9570
def ordered_tags(self, request, durations=None):
9671
tags = [request.translate(_(tag)) for tag in self.tags]
9772

src/onegov/fsi/models/course_attendee.py

+6-37
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,20 @@
22

33
from onegov.core.orm import Base
44
from onegov.core.orm.types import UUID, JSON
5-
from onegov.user import User
6-
from sqlalchemy import Boolean, case, select, and_, func
5+
from sqlalchemy import Boolean
76
from onegov.search import ORMSearchable
87
from sedate import utcnow
98
from sqlalchemy import Column, Text, ForeignKey, ARRAY, desc
109
from sqlalchemy.orm import relationship, object_session, backref
1110
from uuid import uuid4
1211

13-
from typing import TYPE_CHECKING
1412

13+
from typing import TYPE_CHECKING
1514
if TYPE_CHECKING:
1615
from onegov.user import User
1716
from .course_subscription import CourseSubscription
1817

18+
1919
external_attendee_org = "Externe Kursteilnehmer"
2020

2121

@@ -43,8 +43,8 @@ class CourseAttendee(Base, ORMSearchable):
4343
'first_name': {'type': 'text'},
4444
'last_name': {'type': 'text'},
4545
'organisation': {'type': 'text'},
46-
# 'email': {'type': 'text'},
47-
# 'title': {'type': 'text'},
46+
'email': {'type': 'text'},
47+
'title': {'type': 'text'},
4848
}
4949

5050
es_public = False
@@ -118,21 +118,6 @@ def title(self):
118118
) if p
119119
)) or self.email
120120

121-
@title.expression
122-
def title(cls):
123-
return case((
124-
(
125-
and_(
126-
cls.first_name != '',
127-
cls.last_name != ''
128-
),
129-
# cls.first_name + ' ' + cls.last_name
130-
func.concat(cls.first_name + ' ' + cls.last_name)
131-
)
132-
),
133-
else_=cls.email,
134-
)
135-
136121
@property
137122
def lead(self):
138123
return self.organisation
@@ -155,22 +140,6 @@ def email(self):
155140
return self._email
156141
return self.user.username
157142

158-
@email.expression
159-
def email(cls):
160-
from onegov.user import User
161-
162-
return case((
163-
(
164-
# cls.user_id.isnot(None),
165-
cls.user_id != '',
166-
cls._email
167-
),
168-
), else_=(
169-
select([User.username]).
170-
where(cls.user_id == User.id).
171-
label('email'))
172-
)
173-
174143
@email.setter
175144
def email(self, value):
176145
self._email = value
@@ -225,7 +194,7 @@ def repeating_courses(self):
225194

226195
session = object_session(self)
227196

228-
result = session.query(CourseEvent).join(Course) \
197+
result = session.query(CourseEvent).join(Course)\
229198
.filter(Course.mandatory_refresh == True)
230199

231200
result = result.join(CourseSubscription)

src/onegov/fsi/models/course_event.py

+5-49
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,22 @@
77
from icalendar import Event as vEvent
88
from sedate import utcnow, to_timezone
99
from sqlalchemy import (
10-
Column, Boolean, SmallInteger, Enum, Text, Interval, ForeignKey, or_, and_,
11-
select)
10+
Column, Boolean, SmallInteger, Enum, Text, Interval, ForeignKey, or_, and_)
1211
from sqlalchemy.ext.hybrid import hybrid_property
13-
from sqlalchemy.orm import relationship, backref, object_session, join
12+
from sqlalchemy.orm import relationship, backref, object_session
1413
from uuid import uuid4
1514

1615
from onegov.core.mail import Attachment
1716
from onegov.core.orm import Base
1817
from onegov.core.orm.mixins import TimestampMixin
1918
from onegov.core.orm.types import UUID, UTCDateTime
2019
from onegov.fsi import _
21-
from onegov.fsi.models.course import Course
2220
from onegov.fsi.models.course_attendee import CourseAttendee
2321
from onegov.fsi.models.course_subscription import CourseSubscription
2422
from onegov.fsi.models.course_subscription import subscription_table
2523
from onegov.search import ORMSearchable
2624

2725
from typing import TYPE_CHECKING
28-
2926
if TYPE_CHECKING:
3027
from .course import Course
3128
from .course_notification_template import (
@@ -43,6 +40,7 @@
4340

4441
# for forms...
4542
def course_status_choices(request=None, as_dict=False):
43+
4644
if request:
4745
translations = (
4846
request.translate(v) for v in COURSE_EVENT_STATUSES_TRANSLATIONS)
@@ -56,14 +54,14 @@ def course_status_choices(request=None, as_dict=False):
5654

5755

5856
class CourseEvent(Base, TimestampMixin, ORMSearchable):
57+
5958
default_reminder_before = datetime.timedelta(days=14)
6059

6160
__tablename__ = 'fsi_course_events'
6261

6362
es_properties = {
64-
# expression for name and desc implementiert aber subquery issue
6563
'name': {'type': 'localized'},
66-
# 'description': {'type': 'localized'},
64+
'description': {'type': 'localized'},
6765
'location': {'type': 'localized'},
6866
'presenter_name': {'type': 'text'},
6967
'presenter_company': {'type': 'text'},
@@ -91,41 +89,6 @@ def title(self):
9189
def name(self):
9290
return self.course.name
9391

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-
12992
@property
13093
def lead(self):
13194
return (
@@ -138,13 +101,6 @@ def lead(self):
138101
def description(self):
139102
return self.course.description
140103

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-
148104
def __str__(self):
149105
start = to_timezone(
150106
self.start, 'Europe/Zurich').strftime('%d.%m.%Y %H:%M')

src/onegov/org/models/page.py

-4
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,6 @@ class Topic(Page, TraitInfo, SearchableContent, AccessExtension,
4545
lead_when_child = content_property(default=True)
4646

4747
@hybrid_property
48-
def lead(self): # noqa: F811
49-
return self.content['lead']
50-
51-
@lead.expression
5248
def lead(self): # noqa: F811
5349
return self.content['lead'].astext
5450

0 commit comments

Comments
 (0)