This repository was archived by the owner on Nov 29, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathgeneric.py
More file actions
624 lines (532 loc) · 22.4 KB
/
Copy pathgeneric.py
File metadata and controls
624 lines (532 loc) · 22.4 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
"""The basic Content and ContentSource classes.
.. inheritance-diagram:: ContentSource Content PostSource AnnotatorSource assembl.models.post.Post assembl.models.post.AssemblPost assembl.models.post.SynthesisPost assembl.models.post.WidgetPost assembl.models.post.IdeaProposalPost assembl.models.post.ImportedPost assembl.models.mail.AbstractMailbox assembl.models.mail.IMAPMailbox assembl.models.mail.MailingList assembl.models.mail.AbstractFilesystemMailbox assembl.models.mail.MaildirMailbox assembl.models.mail.Email assembl.models.annotation.Webpage
:parts: 1
"""
from datetime import datetime
from abc import abstractmethod
import re
from sqlalchemy import (
Column,
Integer,
SmallInteger,
Boolean,
UnicodeText,
String,
DateTime,
ForeignKey,
inspect,
)
from sqlalchemy.orm import relationship, backref, aliased
from sqlalchemy.sql.functions import count
import assembl.graphql.docstrings as docs
from ..lib.sqla import (CrudOperation, Base)
from ..lib.model_watcher import get_model_watcher
from ..lib import logging
from . import DiscussionBoundBase
from .langstrings import (LangString, LangStringEntry)
from ..auth import (
CrudPermissions, P_ADD_POST, P_READ, P_ADMIN_DISC, P_EDIT_POST,
P_EDIT_MY_POST, P_DELETE_POST, P_DELETE_MY_POST)
from ..auth.util import get_current_user_id
from .discussion import Discussion
from ..lib.history_mixin import TombstonableMixin
from ..lib.clean_input import sanitize_text, sanitize_html
log = logging.getLogger()
class ContentSource(DiscussionBoundBase):
"""
A ContentSource is where any outside content comes from. .
"""
__tablename__ = "content_source"
id = Column(Integer, primary_key=True)
name = Column(UnicodeText, nullable=False)
type = Column(String(60), nullable=False)
creation_date = Column(DateTime, nullable=False, default=datetime.utcnow)
discussion_id = Column(Integer, ForeignKey(
'discussion.id',
ondelete='CASCADE',
onupdate='CASCADE'
), nullable=False, index=True)
connection_error = Column(SmallInteger)
error_description = Column(String)
error_backoff_until = Column(DateTime)
discussion = relationship(
"Discussion",
backref=backref(
'sources', order_by=creation_date,
cascade="all, delete-orphan"))
__mapper_args__ = {
'polymorphic_identity': 'content_source',
'polymorphic_on': type,
'with_polymorphic': '*'
}
retypeable_as = ("IMAPMailbox", "MailingList", "AbstractMailbox",
"AbstractFilesystemMailbox", "AnnotatorSource",
"PostSource", "FeedPostSource", "LoomioPostSource",
"FacebookGenericSource", "FacebookGroupSource",
"FacebookPagePostsSource", "FacebookPageFeedSource",
"FacebookSinglePostSource", "EdgeSenseDrupalSource")
@abstractmethod
def generate_message_id(self, source_post_id):
# Generate a globally unique message_id for the post using
# its source_post_id (locally unique within that source.)
# In many cases, the source_post_id is already globally unique.
return source_post_id
_non_email_chars = re.compile(r'[^!#-\'\*\+\-\./-9=\?A-Z\^_`a-z\|\~]', re.U)
@classmethod
def flatten_source_post_id(cls, source_post_id, extra_length=0):
# Ensure that a source_post_id can be used as part 1 of message_id
sanitized = cls._non_email_chars.subn(
lambda c: '_' + hex(ord(c.group()))[2:], source_post_id)[0]
if len(sanitized) + extra_length > 64:
# 64 is max according to RFC 5322
# cut it short and add a digest of original
import hashlib
import base64
d = hashlib.md5()
d.update(source_post_id)
d = base64.urlsafe_b64encode(d.digest())
sanitized = sanitized[
:max(0, 64 - len(d) - extra_length - 1)]
if sanitized:
sanitized += "_" + d
else:
sanitized = d
return sanitized
def import_content(self, only_new=True):
from assembl.processes.source_reader import wake
wake(self.id, reimport=not only_new)
def make_reader(self):
return None
def get_discussion_id(self):
return self.discussion_id or self.discussion.id
@property
def connection_error_as_text(self):
from ..processes.source_reader import ReaderStatus
return (ReaderStatus(self.connection_error).name
if self.connection_error is not None else None)
@classmethod
def get_discussion_conditions(cls, discussion_id, alias_maker=None):
return (cls.discussion_id == discussion_id,)
# Cannot be readable to all, because subclasses contain passwords
crud_permissions = CrudPermissions(P_ADMIN_DISC, P_ADMIN_DISC)
def reset_errors(self):
self.connection_error = None
self.error_description = None
self.error_backoff_until = None
class PostSource(ContentSource):
"""
A Discussion PostSource is where commentary that is handled in the form of
Assembl posts comes from.
A discussion source should have a method for importing all content, as well
as only importing new content. Maybe the standard interface for this should
be `source.import()`.
"""
__tablename__ = "post_source"
id = Column(Integer, ForeignKey(
'content_source.id',
ondelete='CASCADE',
onupdate='CASCADE'
), primary_key=True)
last_import = Column(DateTime)
__mapper_args__ = {
'polymorphic_identity': 'post_source',
}
def get_discussion_id(self):
return self.discussion_id or self.discussion.id
def get_default_prepended_id(self):
# Used for PostSource's whose incoming posts cannot guarantee
# ImportedPost.source_post_id is unique; in which case, the Post.message_id
# which is a globally unique value maintain uniqueness integrity
# by calling this function
# Must be implemented by subclasses that will not have unique
# id's on their incoming posts
return ""
@property
def number_of_imported_posts(self):
from .post import ImportedPost
return self.db.query(ImportedPost).filter_by(
source_id=self.id, tombstone_date=None).count()
@classmethod
def get_discussion_conditions(cls, discussion_id, alias_maker=None):
return (cls.discussion_id == discussion_id,)
def send_post(self, post):
""" Send a new post in the discussion to the source. """
log.warn(
"Source %s did not implement PostSource::send_post()"
% self.__class__.__name__)
class AnnotatorSource(ContentSource):
"""
A source of content coming from annotator
"""
__tablename__ = "annotator_source"
id = Column(Integer, ForeignKey(
'content_source.id',
ondelete='CASCADE',
onupdate='CASCADE'
), primary_key=True)
__mapper_args__ = {
'polymorphic_identity': 'annotator_source',
}
class ContentSourceIDs(Base):
"""
A table that keeps track of the number of external identities that
an Assembl post can be exported to.
A stepping-stone to having Sinks
"""
__tablename__ = 'content_source_ids'
id = Column(Integer, primary_key=True)
source_id = Column(
Integer, ForeignKey(
'content_source.id', onupdate='CASCADE', ondelete='CASCADE'),
nullable=False, index=True)
source = relationship('ContentSource', backref=backref(
'pushed_messages',
cascade='all, delete-orphan'))
post_id = Column(
Integer, ForeignKey(
'content.id', onupdate='CASCADE', ondelete='CASCADE'),
nullable=False, index=True)
post = relationship('Content',
backref=backref('post_sink_associations',
cascade='all, delete-orphan'))
message_id_in_source = Column(String(256), nullable=False, index=True)
class Content(TombstonableMixin, DiscussionBoundBase):
"""
Content is a polymorphic class to describe what is imported from a Source.
The body and subject properly belong to the Post but were moved here to
optimize the most common case.
"""
__tablename__ = "content"
# __table_cls__ = TableWithTextIndex
id = Column(Integer, primary_key=True)
type = Column(String(60), nullable=False)
creation_date = Column(DateTime, nullable=False, default=datetime.utcnow)
discussion_id = Column(Integer, ForeignKey(
'discussion.id',
ondelete='CASCADE',
onupdate='CASCADE',
),
nullable=False, index=True)
discussion = relationship(
"Discussion",
backref=backref(
'posts', order_by=creation_date,
cascade="all, delete-orphan")
)
subject_id = Column(Integer, ForeignKey(LangString.id), index=True)
body_id = Column(Integer, ForeignKey(LangString.id), index=True)
subject = relationship(
LangString,
primaryjoin=subject_id == LangString.id,
backref=backref("subject_of_post", lazy="dynamic"),
single_parent=True, lazy="joined",
cascade="all, delete-orphan")
body = relationship(
LangString,
primaryjoin=body_id == LangString.id,
backref=backref("body_of_post", lazy="dynamic"),
single_parent=True, lazy="joined",
cascade="all, delete-orphan")
message_classifier = Column(String(100), index=True,
doc=docs.PostInterface.message_classifier)
def __init__(self, *args, **kwargs):
if (kwargs.get('subject', None) is None and
kwargs.get('subject_id', None) is None):
kwargs['subject'] = LangString.EMPTY()
if (kwargs.get('body', None) is None and
kwargs.get('body_id', None) is None):
kwargs['body'] = LangString.EMPTY()
super(Content, self).__init__(*args, **kwargs)
@classmethod
def graphene_id_for(cls, id):
# who made the Post global Ids not follow the base class?
from graphene.relay import Node
return Node.to_global_id('Post', id)
@classmethod
def subqueryload_options(cls):
# Options for subquery loading. Use when there are many languages in the discussion.
return (
LangString.subqueryload_option(cls.subject),
LangString.subqueryload_option(cls.body))
@classmethod
def joinedload_options(cls):
# Options for joined loading. Use when there are few languages in the discussion.
return (
LangString.joinedload_option(cls.subject),
LangString.joinedload_option(cls.body))
@classmethod
def best_locale_query(cls, locales):
"BUGGY. Return a query that will load the post, best subject and best body for the given locales"
# this fails because virtuoso, but the SQL is correct.
# Note that it fails with just body, and succeeds with subject.
# Go figure. Fortunately not needed yet.
subject_ls = aliased(LangString)
body_ls = aliased(LangString)
best_subject_sq = LangString.best_lang_old(locales)
best_body_sq = LangString.best_lang_old(locales)
return cls.default_db.query(
cls, best_subject_sq, best_body_sq).join(
subject_ls, cls.subject_id == subject_ls.id).join(
best_subject_sq).join(
body_ls, cls.body_id == body_ls.id).join(best_body_sq)
# TODO: Refactor hidden into PublicationStates.WIDGET_SCOPED
hidden = Column(Boolean, server_default='0')
# Another bloody virtuoso bug. Insert with large string fails.
# body_text_index = TextIndex(body, clusters=[discussion_id])
__mapper_args__ = {
'polymorphic_identity': 'content',
'polymorphic_on': 'type',
'with_polymorphic': '*'
}
def get_created_phase(self):
from assembl.lib.frontend_urls import get_timeline_for_date
return get_timeline_for_date(self.discussion, self.creation_date)
def get_subject(self):
return self.subject
def get_body(self):
return self.body
def get_title(self):
return self.subject
def safe_set_body(self, body):
if self.get_body_mime_type() == 'text/plain':
for e in body['entries']:
e['value'] = sanitize_text(e['value'])
else:
for e in body['entries']:
e['value'] = sanitize_html(e['value'])
def safe_set_subject(self, subject):
for e in subject['entries']:
if "<" in e['value']:
e['value'] = sanitize_text(e['value'])
def remove_translations(self):
if self.subject:
self.subject.remove_translations()
self.body.remove_translations()
def get_body_mime_type(self):
""" Return the format of the body, so the frontend will know how to
display it. Currently, only:
text/plain (Understood as preformatted text)
text/html (Undestood as some subset of html)
"""
return "text/plain"
def get_body_as_html(self):
mimetype = self.get_body_mime_type()
body = self.body
if not body:
return None
if mimetype == 'text/html':
return body
elif mimetype == "text/plain":
ls = LangString()
for e in body.entries:
LangStringEntry(
value='<span style="white-space: pre-wrap">%s</div>' % (
e.value,),
langstring=ls, locale_id=e.locale_id)
return ls
else:
log.error("What is this mimetype?" + mimetype)
return body
def get_original_body_as_html(self):
mimetype = self.get_body_mime_type()
body = self.body
if not body:
return None
if mimetype == 'text/html':
return body.first_original().value
elif mimetype == "text/plain":
return '<span style="white-space: pre-wrap">%s</div>' % (
body.first_original().value,)
else:
log.error("What is this mimetype?" + mimetype)
return body
def get_original_body_as_text(self):
mimetype = self.get_body_mime_type()
body = self.body
if not body:
return ''
body = body.first_original().value or ''
if mimetype == 'text/plain':
return body
elif mimetype == 'text/html':
return sanitize_text(body)
else:
log.error("What is this mimetype?" + mimetype)
return body
def has_attachments(self):
return self.attachments or False
def get_attachments_as_html_list(self):
img_style = "margin: 15px 0 15px 0; max-width: 500px; max-height: auto;"
img_source = "<a href='%s' target='_blank' style='%s'><img src='%s'></img></a>"
other_source = "<a href='%s' target='_blank'>%s</a>"
attachments = self.attachments
attachment_sorted = sorted(attachments, key=lambda a: a.document.type)
output = []
for attachment in attachment_sorted:
document = attachment.document
mime_type = document.mime_type
if mime_type and 'image' in mime_type:
output.append(img_source % (document.external_url, img_style,
document.external_url))
else:
title = document.title or document.external_url
output.append(other_source % (document.external_url, title))
return output
def get_body_as_text(self):
mimetype = self.get_body_mime_type()
body = self.body
if not body:
return None
if mimetype == 'text/plain':
return body
elif mimetype == 'text/html':
ls = LangString()
for e in body.entries:
LangStringEntry(
value=sanitize_text(e.value),
langstring=ls, locale_id=e.locale_id)
return ls
else:
log.error("What is this mimetype?" + mimetype)
return body
def maybe_translate(self, pref_collection=None, target_locales=None):
from assembl.processes.translate import (
translate_content, PrefCollectionTranslationTable, LanguagesTranslationTable)
service = self.discussion.translation_service()
if service.canTranslate is not None:
translations = None
if pref_collection:
translations = PrefCollectionTranslationTable(
service, pref_collection)
elif target_locales:
translations = LanguagesTranslationTable(service, target_locales)
translate_content(
self, translation_table=translations, service=service)
def send_to_changes(self, connection=None, operation=CrudOperation.UPDATE,
discussion_id=None, view_def="changes"):
"""invoke the modelWatcher on creation"""
super(Content, self).send_to_changes(
connection, operation, discussion_id, view_def)
watcher = get_model_watcher()
if operation == CrudOperation.CREATE:
watcher.processPostCreated(self.id)
elif operation == CrudOperation.UPDATE:
state_changed = False
if hasattr(self, 'publication_state'):
hist = inspect(self).attrs.publication_state.history
state_changed = hist.has_changes()
watcher.processPostModified(self.id, state_changed)
def get_discussion_id(self):
return self.discussion_id or self.discussion.id
@property
def exported_to_sources(self):
return [ContentSource.uri_generic(s.source_id)
for s in self.post_sink_associations]
@classmethod
def get_discussion_conditions(cls, discussion_id, alias_maker=None):
return (cls.discussion_id == discussion_id,)
@property
def sentiment_counts(self):
from .action import SentimentOfPost
base = {
name: 0 for name in SentimentOfPost.all_sentiments
}
r = self.db.query(
SentimentOfPost.type, count(SentimentOfPost.id)
).filter(SentimentOfPost.post_id == self.id,
SentimentOfPost.tombstone_condition()
).group_by(SentimentOfPost.type)
base.update({k[SentimentOfPost.TYPE_PREFIX_LEN:]: v for (k, v) in r})
return base
def current_user(self):
# HACK! Allows to get the current user's uri from view_defs.
from .auth import User
user_id = get_current_user_id()
return User.uri_generic(user_id)
def language_priors(self, translation_service):
discussion = self.discussion
discussion_locales = discussion.discussion_locales
return {translation_service.asKnownLocale(loc): 1
for loc in discussion_locales}
def guess_languages(self):
from .langstrings import Locale
if self.discussion is None:
self.discussion = Discussion.get(self.discussion_id)
assert self.discussion
ts = self.discussion.translation_service()
priors = self.language_priors(ts)
if self.body:
body_original = self.body.first_original()
ts.confirm_locale(body_original, priors)
if self.subject:
if self.body and body_original.locale_code not in (
Locale.UNDEFINED, Locale.NON_LINGUISTIC):
# boost the body's language
priors = {k: v * 0.6 for (k, v) in priors.iteritems()}
priors[body_original.locale_code] = 1
subject_original = self.subject.first_original()
ts.confirm_locale(subject_original, priors)
@property
def my_sentiment(self):
# Use only within request
from .action import SentimentOfPost
user_id = get_current_user_id()
if user_id is None:
return
return self.db.query(SentimentOfPost).filter_by(
actor_id=user_id, post_id=self.id, tombstone_date=None).first()
widget_idea_links = relationship('IdeaContentWidgetLink')
def indirect_idea_content_links(self):
return []
def widget_ideas(self):
from .idea import Idea
return [Idea.uri_generic(wil.idea_id) for wil in self.widget_idea_links]
def nlp_keywords(self, display_lang=None, limit=20):
from .nlp import PostKeywordAnalysis, Tag
from .langstrings import LangStringEntry, Locale
sq = self.db.query(
PostKeywordAnalysis.score.label('score'),
PostKeywordAnalysis.occurences.label('count'),
Tag.id.label('id')
).filter_by(
category=None
).join(Tag).filter(
PostKeywordAnalysis.post_id == self.id
).order_by(
PostKeywordAnalysis.score.desc())
if limit:
assert isinstance(limit, int)
sq = sq.limit(limit)
sq = sq.subquery()
if display_lang is None:
label_cond = Locale.code.notlike('%-x-mtfrom-%')
else:
label_cond = (Locale.code == display_lang) \
| (Locale.code.like(display_lang + '-x-mtfrom-%'))
if display_lang != 'en':
untranslated = self.db.query(
LangString
).join(
Tag, LangString.id == Tag.label_id
).filter(
Tag.id.in_(sq)
).outerjoin(
LangStringEntry,
LangStringEntry.langstring_id == Tag.label_id
).filter(LangStringEntry.id == None).all() # noqa: E711
if untranslated:
translator = self.discussion.translation_service()
for t in untranslated:
t.ensure_translations([display_lang], translator)
q = self.db.query(
LangStringEntry.value, sq.c.score, sq.c.count
).join(Locale).filter(
label_cond
).distinct().join(
Tag, Tag.label_id == LangStringEntry.langstring_id
).join(sq, sq.c.id == Tag.id).order_by(sq.c.score.desc())
return q.all()
crud_permissions = CrudPermissions(
P_ADD_POST, P_READ, P_EDIT_POST, P_DELETE_POST,
P_EDIT_MY_POST, P_DELETE_MY_POST)
LangString.setup_ownership_load_event(Content, ['subject', 'body'])