[ee46bc]: / Allura / allura / model / artifact.py  Maximize  Restore  History

Download this file

1230 lines (1035 with data), 42.5 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
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
from __future__ import annotations
import logging
from collections import defaultdict
from datetime import datetime
import typing
import pymongo
from tg import tmpl_context as c, app_globals as g
from tg import request
from ming import schema as S
from ming.odm import state, session
from ming.odm import FieldProperty, ForeignIdProperty, RelationProperty
from ming.odm.declarative import MappedClass
from ming.utils import LazyProperty
import feedgenerator as FG
from allura.lib import helpers as h
from allura.lib import security
from allura.lib import utils
from allura.lib import plugin
from allura.lib import exceptions as forge_exc
from allura.lib.decorators import memoize
from allura.lib.search import SearchIndexable
from .session import main_orm_session
from .session import project_orm_session
from .session import artifact_orm_session
from .index import ArtifactReference, Shortlink
from .types import ACL, MarkdownCache
from .project import AppConfig
from .notification import MailFooter
from .filesystem import File
import six
if typing.TYPE_CHECKING:
from ming.odm.mapper import Query
from allura.model import BaseAttachment
log = logging.getLogger(__name__)
class Artifact(MappedClass, SearchIndexable):
"""
Base class for anything you want to keep track of.
- Automatically indexed into Solr (see index() method)
- Has a discussion thread that can have files attached to it
:var mod_date: last-modified :class:`datetime`
:var acl: dict of permission name => [roles]
:var labels: list of plain old strings
"""
class __mongometa__:
session = artifact_orm_session
name = 'artifact'
indexes = [
('app_config_id', 'labels'),
]
def before_save(data):
_session = artifact_orm_session._get()
skip_mod_date = getattr(_session, 'skip_mod_date', False)
skip_last_updated = getattr(_session, 'skip_last_updated', False)
if not skip_mod_date:
data['mod_date'] = datetime.utcnow()
else:
log.debug('Not updating mod_date')
if c.project and not skip_last_updated:
c.project.last_updated = datetime.utcnow()
query: 'Query[Artifact]'
type_s = 'Generic Artifact'
# Artifact base schema
_id = FieldProperty(S.ObjectId)
mod_date = FieldProperty(datetime, if_missing=datetime.utcnow)
app_config_id = ForeignIdProperty(
'AppConfig', if_missing=lambda: c.app.config._id)
plugin_verson = FieldProperty(S.Deprecated)
tool_version = FieldProperty(S.Deprecated)
acl = FieldProperty(ACL)
tags = FieldProperty(S.Deprecated)
labels = FieldProperty([str])
references = FieldProperty(S.Deprecated)
backreferences = FieldProperty(S.Deprecated)
app_config = RelationProperty('AppConfig')
# Not null if artifact originated from external import. The import ID is
# implementation specific, but should probably be an object indicating
# the source, original ID, and any other info needed to identify where
# the artifact came from. But if you only have one source, a str might do.
import_id = FieldProperty(None, if_missing=None)
deleted = FieldProperty(bool, if_missing=False)
def __json__(self, posts_limit=None, is_export=False, user=None):
"""Return a JSON-encodable :class:`dict` representation of this
Artifact.
"""
return dict(
_id=str(self._id),
mod_date=self.mod_date,
labels=list(self.labels),
related_artifacts=[a.url() for a in self.related_artifacts(user=user or c.user)],
discussion_thread=self.discussion_thread.__json__(limit=posts_limit, is_export=is_export),
discussion_thread_url=h.absurl('/rest%s' %
self.discussion_thread.url()),
)
def parent_security_context(self):
"""Return the :class:`allura.model.project.AppConfig` instance for
this Artifact.
ACL processing for this Artifact continues at the AppConfig object.
This lets AppConfigs provide a 'default' ACL for all artifacts in the
tool.
"""
return self.app_config
@classmethod
def attachment_class(cls):
raise NotImplementedError('attachment_class')
@LazyProperty
def ref(self):
"""Return :class:`allura.model.index.ArtifactReference` for this
Artifact.
"""
if hasattr(self, '_ref'):
return self._ref
return ArtifactReference.from_artifact(self)
@LazyProperty
def refs(self):
"""Artifacts referenced by this one.
:return: list of :class:`allura.model.index.ArtifactReference`
"""
return self.ref.references
@LazyProperty
def backrefs(self):
"""Artifacts that reference this one.
:return: list of :attr:`allura.model.index.ArtifactReference._id`'s
"""
if hasattr(self, '_backrefs'):
return self._backrefs
q = ArtifactReference.query.find(dict(references=self.index_id()))
return [aref._id for aref in q]
def related_artifacts(self, user=None):
"""Return all Artifacts that are related to this one.
"""
related_artifacts = []
for ref_id in self.refs + self.backrefs:
ref = ArtifactReference.query.get(_id=ref_id)
if ref is None:
continue
artifact = ref.artifact
if artifact is None:
continue
artifact = artifact.primary()
if artifact is None:
continue
# don't link to artifacts in deleted tools
if hasattr(artifact, 'app_config') and artifact.app_config is None:
continue
try:
if user and not h.has_access(artifact, 'read', user):
continue
except Exception:
log.debug('Error doing permission check on related artifacts of {}, '
'probably because the "artifact" is a Commit not a real artifact'.format(self.index_id()),
exc_info=True)
# TODO: This should be refactored. We shouldn't be checking
# artifact type strings in platform code.
if artifact.type_s == 'Commit' and not artifact.repo:
ac = AppConfig.query.get(_id=ref.artifact_reference['app_config_id'])
app = ac.project.app_instance(ac) if ac else None
if app:
artifact.set_context(app.repo)
if artifact not in related_artifacts and (getattr(artifact, 'deleted', False) is False):
related_artifacts.append(artifact)
return sorted(related_artifacts, key=lambda a: a.url())
def subscribe(self, user=None, topic=None, type='direct', n=1, unit='day'):
"""Subscribe ``user`` to the :class:`allura.model.notification.Mailbox`
for this Artifact.
:param user: :class:`allura.model.auth.User`
If ``user`` is None, ``c.user`` will be subscribed.
"""
from allura.model import Mailbox
if user is None:
user = c.user
return Mailbox.subscribe(
user_id=user._id,
project_id=self.app_config.project_id,
app_config_id=self.app_config._id,
artifact=self, topic=topic,
type=type, n=n, unit=unit)
def unsubscribe(self, user=None):
"""Unsubscribe ``user`` from the
:class:`allura.model.notification.Mailbox` for this Artifact.
:param user: :class:`allura.model.auth.User`
If ``user`` is None, ``c.user`` will be unsubscribed.
"""
from allura.model import Mailbox
if user is None:
user = c.user
Mailbox.unsubscribe(
user_id=user._id,
project_id=self.app_config.project_id,
app_config_id=self.app_config._id,
artifact_index_id=self.index_id())
@memoize # since its called many times from edit_post.html within threaded comments
def subscribed(self, user=None, include_parents=True) -> bool:
from allura.model import Mailbox
if user is None:
user = c.user
if user.is_anonymous():
return False
user_proj_app_q = dict(user_id=user._id,
project_id=self.app_config.project_id,
app_config_id=self.app_config._id)
art_subscribed = Mailbox.subscribed(artifact=self, **user_proj_app_q)
if art_subscribed:
return True
if include_parents:
tool_subscribed = Mailbox.subscribed(**user_proj_app_q)
if tool_subscribed:
return True
return False
def primary(self):
"""If an artifact is a "secondary" artifact (discussion of a ticket, for
instance), return the artifact that is the "primary".
"""
return self
@classmethod
def artifacts_labeled_with(cls, label, app_config):
"""Return all artifacts of type ``cls`` that have the label ``label`` and
are in the tool denoted by ``app_config``.
:param label: str
:param app_config: :class:`allura.model.project.AppConfig` instance
"""
return cls.query.find({'labels': label, 'app_config_id': app_config._id})
def email_link(self, subject='artifact'):
"""Return a 'mailto' URL for this Artifact, with optional subject.
"""
if subject:
return 'mailto:{}?subject=[{}:{}:{}] Re: {}'.format(
self.email_address,
self.app_config.project.shortname,
self.app_config.options.mount_point,
self.shorthand_id(),
subject)
else:
return 'mailto:%s' % self.email_address
@property
def email_domain(self):
"""Return domain part of email address for this Artifact"""
url = self.app.url[1:-1].split('/')
return '.'.join(reversed(url)).replace('_', '-')
@property
def project(self):
"""Return the :class:`allura.model.project.Project` instance to which
this Artifact belongs.
"""
return getattr(self.app_config, 'project', None)
@property
def project_id(self):
"""Return the ``_id`` of the :class:`allura.model.project.Project`
instance to which this Artifact belongs.
"""
return self.app_config.project_id
@LazyProperty
def app(self):
"""Return the :class:`allura.model.app.Application` instance to which
this Artifact belongs.
"""
if not self.app_config:
return None
if getattr(c, 'app', None) and c.app.config._id == self.app_config._id:
return c.app
else:
return self.app_config.load()(self.project, self.app_config)
def index(self):
project = self.project
return dict(
id=self.index_id(),
mod_date_dt=self.mod_date,
title='Artifact %s' % self._id,
project_id_s=str(project._id),
project_name_t=project.name,
project_shortname_t=project.shortname,
tool_name_s=self.app_config.tool_name,
mount_point_s=self.app_config.options.mount_point,
is_history_b=False,
url_s=self.url(),
type_s=self.type_s,
labels_t=' '.join(l for l in self.labels),
snippet_s='',
deleted_b=self.deleted)
@property
def type_name(self):
"""
:return: a presentation name for this type of artifact
:rtype: str
"""
return self.type_s.lower()
def url(self):
"""Return the URL for this Artifact.
Subclasses must implement this.
"""
raise NotImplementedError('url') # pragma no cover
def shorthand_id(self):
"""How to refer to this artifact within the app instance context.
For a wiki page, it might be the title. For a ticket, it might be the
ticket number. For a discussion, it might be the message ID. Generally
this should have a strong correlation to the URL.
"""
return str(self._id) # pragma no cover
def link_text(self):
"""Return the link text to use when a shortlink to this artifact
is expanded into an <a></a> tag.
By default this method returns :attr:`type_s` + :meth:`shorthand_id`. Subclasses should
override this method to provide more descriptive link text.
"""
return self.shorthand_id()
def _get_discussion_thread(self):
from .discuss import Thread
threads = Thread.query.find(dict(ref_id=self.index_id())).all()
if not threads:
return None
if len(threads) == 1:
return threads[0]
# there should not be multiple threads, we'll merge them
destination = threads.pop()
for thread in threads:
for post in thread.posts:
post.thread_id = destination._id
destination.num_replies += 1
destination.last_post_date = max(destination.last_post_date, post.mod_date)
session(post).flush(post)
session(post).expunge(post) # so thread.posts ref later in the code doesn't use stale posts
Thread.query.remove({'_id': thread._id}) # NOT thread.delete() since that would remove its posts too
thread.attachment_class().query.update({'thread_id': thread._id},
{'$set': {'thread_id': destination._id}},
multi=True)
return destination
def get_discussion_thread(self, data=None):
"""Return the discussion thread and parent_id for this artifact.
:return: (:class:`allura.model.discuss.Thread`, parent_thread_id (int))
"""
from .discuss import Thread
thread = self._get_discussion_thread()
if not thread:
idx = self.index()
thread = Thread.new(
app_config_id=self.app_config_id,
discussion_id=self.app_config.discussion_id,
ref_id=idx['id'],
subject='%s discussion' % h.get_first(idx, 'title'))
parent_id = None
if data:
in_reply_to = data.get('in_reply_to', [])
if in_reply_to:
parent_id = in_reply_to[0]
return thread, parent_id
@LazyProperty
def discussion_thread(self):
"""Return the :class:`discussion thread <allura.model.discuss.Thread>`
for this Artifact.
"""
return self.get_discussion_thread()[0]
def add_multiple_attachments(self, file_info):
if not isinstance(file_info, list):
file_info = [file_info]
for attach in file_info:
if hasattr(attach, 'file'):
self.attach(attach.filename, attach.file,
content_type=attach.type)
def attach(self, filename, fp, **kw):
"""Attach a file to this Artifact.
:param filename: file name
:param fp: a file-like object (implements ``read()``)
:param kw: passed through to Attachment class constructor
"""
att = self.attachment_class().save_attachment(
filename=filename,
fp=fp, artifact_id=self._id, **kw)
return att
@LazyProperty
def attachments(self):
return self._get_attachments()
def _get_attachments(self, unique_files_only=True, include_thumbnails=False) -> list[BaseAttachment]:
if hasattr(self, '_attachments'):
atts = self._attachments
else:
try:
atts = self.attachment_class().query.find({
'app_config_id': self.app_config_id,
'artifact_id': {
# some artifact_ids are ObjectIds and some are strings
'$in': [self._id, str(self._id)]
},
'type': {'$in': ['attachment'] + (['thumbnail'] if include_thumbnails else [])}
}).all()
except NotImplementedError:
atts = []
if unique_files_only:
atts = utils.unique_attachments(atts)
return atts
def delete(self):
"""Delete this Artifact.
"""
thread = self._get_discussion_thread()
if thread:
thread.delete()
for att in self._get_attachments(unique_files_only=False, include_thumbnails=True):
att.delete()
session(att).flush(att)
idx_id = self.index_id()
ArtifactReference.query.remove(dict(_id=idx_id))
Shortlink.query.remove(dict(ref_id=idx_id))
super().delete()
session(self).flush(self)
session(self).imap.expunge(self)
def get_mail_footer(self, notification, toaddr):
allow_email_posting = self.app.config.options.get('AllowEmailPosting', True)
return MailFooter.standard(notification, allow_email_posting)
def message_id(self):
'''Persistent, email-friendly (Message-ID header) id of this artifact'''
return h.gen_message_id(self._id)
@classmethod
def is_limit_exceeded(cls, app_config, user=None, count_by_user=None):
"""
Returns True if any of artifact creation rate limits are exceeded,
False otherwise
"""
pkg = cls.__module__.split('.', 1)[0]
opt = f'{pkg}.rate_limits'
def count_in_app():
return cls.query.find(dict(app_config_id=app_config._id)).count()
provider = plugin.ProjectRegistrationProvider.get()
start = provider.registration_date(app_config.project)
try:
h.rate_limit(opt, count_in_app, start)
if user and not user.is_anonymous() and count_by_user is not None:
h.rate_limit(opt + '_per_user', count_by_user, user.registration_date())
except forge_exc.RatelimitError:
return True
return False
class Snapshot(Artifact):
"""
A snapshot of an :class:`Artifact <allura.model.artifact.Artifact>`,
used in :class:`VersionedArtifact <allura.model.artifact.VersionedArtifact>`
"""
class __mongometa__:
session = artifact_orm_session
name = 'artifact_snapshot'
unique_indexes = [('artifact_class', 'artifact_id', 'version')]
indexes = [('artifact_id', 'version'),
'author.id',
]
query: 'Query[Snapshot]'
_id = FieldProperty(S.ObjectId)
artifact_id = FieldProperty(S.ObjectId)
artifact_class = FieldProperty(str)
version = FieldProperty(S.Int, if_missing=0)
author = FieldProperty(dict(
id=S.ObjectId,
username=str,
display_name=str,
logged_ip=str))
timestamp = FieldProperty(datetime)
data = FieldProperty(None)
def index(self):
result = Artifact.index(self)
original = self.original()
if original:
original_index = original.index()
result.update(original_index)
result['title'] = '%s (version %d)' % (h.get_first(original_index, 'title'), self.version)
else:
result['title'] = None
result.update(
id=self.index_id(),
version_i=self.version,
author_username_t=self.author.username,
author_display_name_t=self.author.display_name,
timestamp_dt=self.timestamp,
is_history_b=True)
return result
def original(self):
raise NotImplementedError('original') # pragma no cover
def shorthand_id(self):
return f'{self.original().shorthand_id()}#{self.version}'
def clear_user_data(self):
""" Redact author data for a given user """
new_author = {
"username": "",
"display_name": "",
"id": None,
"logged_ip": None
}
self.author = new_author
@classmethod
def from_username(cls, username):
return cls.query.find({'author.username': username}).all()
@property
def attachments(self):
orig = self.original()
if not orig:
return None
return orig.attachments
def __getattr__(self, name):
return getattr(self.data, name)
class VersionedArtifact(Artifact):
"""
An :class:`Artifact <allura.model.artifact.Artifact>` that has versions.
Associated data like attachments and discussion thread are not versioned.
"""
class __mongometa__:
session = artifact_orm_session
name = 'versioned_artifact'
history_class = Snapshot
query: 'Query[VersionedArtifact]'
version = FieldProperty(S.Int, if_missing=0)
def commit(self, update_stats=True):
'''Save off a snapshot of the artifact and increment the version #'''
try:
ip_address = utils.ip_address(request)
except Exception:
ip_address = '0.0.0.0'
data = dict(
artifact_id=self._id,
artifact_class='{}.{}'.format(
self.__class__.__module__,
self.__class__.__name__),
author=dict(
id=c.user._id,
username=c.user.username,
display_name=c.user.get_pref('display_name'),
logged_ip=ip_address),
data=state(self).clone())
while True:
self.version += 1
data['version'] = self.version
data['timestamp'] = datetime.utcnow()
ss = self.__mongometa__.history_class(**data)
try:
session(ss).insert_now(ss, state(ss))
except pymongo.errors.DuplicateKeyError:
log.warning('Trying to create duplicate version %s of %s',
self.version, self.__class__)
session(ss).expunge(ss)
continue
else:
break
log.debug('Snapshot version %s of %s',
self.version, self.__class__)
if update_stats:
if self.version > 1:
g.statsUpdater.modifiedArtifact(
self.type_s, self.mod_date, self.project, c.user)
else:
g.statsUpdater.newArtifact(
self.type_s, self.mod_date, self.project, c.user)
return ss
def get_version(self, n):
if n < 0:
n = self.version + n + 1
ss = self.__mongometa__.history_class.query.get(
artifact_id=self._id,
artifact_class='{}.{}'.format(
self.__class__.__module__,
self.__class__.__name__),
version=n)
if ss is None:
raise IndexError(n)
return ss
def revert(self, version):
ss = self.get_version(version)
old_version = self.version
for k, v in ss.data.items():
setattr(self, k, v)
self.version = old_version
def history(self):
HC = self.__mongometa__.history_class
q = HC.query.find(dict(artifact_id=self._id)).sort(
'version', pymongo.DESCENDING)
return q
@property
def last_updated(self):
history = self.history()
if history.count():
return self.history().first().timestamp
else:
return self.mod_date
def delete(self):
# remove history so that the snapshots aren't left orphaned
HC = self.__mongometa__.history_class
for version in HC.query.find(dict(artifact_id=self._id)):
version.delete()
super().delete()
@classmethod
def is_limit_exceeded(cls, *args, **kwargs):
if 'user' in kwargs:
def distinct_artifacts_by_user():
# count distinct items, not total (e.g. many edits to a single wiki page doesn't count against you)
# query history here, as regular base artifacts have no author information
HC = cls.__mongometa__.history_class
artifacts = HC.query.find({'author.id': kwargs['user']._id}).distinct('artifact_id')
"""
# some useful debugging:
log.info(artifacts)
for art_id in artifacts:
art = cls.query.get(_id=art_id)
log.info(' ' + art.url())
"""
return len(artifacts)
kwargs['count_by_user'] = distinct_artifacts_by_user
return super().is_limit_exceeded(*args, **kwargs)
class Message(Artifact):
"""
A message
:var _id: an email friendly (e.g. message-id) string id
:var slug: slash-delimeted random identifier. Slashes useful for threaded searching and ordering
:var full_slug: string of slash-delimited "timestamp:slug" components. Useful for sorting by timstamp
"""
class __mongometa__:
session = artifact_orm_session
name = 'message'
query: 'Query[Message]'
type_s = 'Generic Message'
_id = FieldProperty(str, if_missing=h.gen_message_id)
slug = FieldProperty(str, if_missing=h.nonce)
full_slug = FieldProperty(str, if_missing=None)
parent_id = FieldProperty(str)
app_id = FieldProperty(S.ObjectId, if_missing=lambda: c.app.config._id)
timestamp = FieldProperty(datetime, if_missing=datetime.utcnow)
author_id = FieldProperty(S.ObjectId, if_missing=lambda: c.user._id)
text = FieldProperty(str, if_missing='')
@classmethod
def make_slugs(cls, parent=None, timestamp=None):
part = h.nonce()
if timestamp is None:
timestamp = datetime.utcnow()
dt = timestamp.strftime('%Y%m%d%H%M%S%f')
slug = part
full_slug = dt + ':' + part
if parent:
return (parent.slug + '/' + slug,
parent.full_slug + '/' + full_slug)
else:
return slug, full_slug
def author(self):
from .auth import User
return User.query.get(_id=self.author_id) or User.anonymous()
def index(self):
result = Artifact.index(self)
author = self.author()
result.update(
author_user_name_t=author.username,
author_display_name_t=author.get_pref('display_name'),
timestamp_dt=self.timestamp,
text=self.text)
return result
def shorthand_id(self):
return self.slug
class AwardFile(File):
class __mongometa__:
session = main_orm_session
name = 'award_file'
query: 'Query[AwardFile]'
award_id = FieldProperty(S.ObjectId)
class Award(Artifact):
class __mongometa__:
session = main_orm_session
name = 'award'
indexes = ['short']
query: 'Query[Award]'
type_s = 'Generic Award'
from .project import Neighborhood
_id = FieldProperty(S.ObjectId)
created_by_neighborhood_id = ForeignIdProperty(
Neighborhood, if_missing=None)
created_by_neighborhood = RelationProperty(
Neighborhood, via='created_by_neighborhood_id')
short = FieldProperty(str, if_missing=h.nonce)
timestamp = FieldProperty(datetime, if_missing=datetime.utcnow)
full = FieldProperty(str, if_missing='')
def index(self):
result = Artifact.index(self)
result.update(
_id_s=self._id,
short_s=self.short,
timestamp_dt=self.timestamp,
full_s=self.full)
result['created_by_s'] = self.created_by.name if self.created_by else None
return result
@property
def icon(self):
return AwardFile.query.get(award_id=self._id)
def url(self):
return str(self._id)
def longurl(self):
return self.created_by_neighborhood.url_prefix + "_admin/awards/" + self.url()
def shorthand_id(self):
return self.short
class AwardGrant(Artifact):
"An :class:`Award <allura.model.artifact.Award>` can be bestowed upon a project by a neighborhood"
class __mongometa__:
session = main_orm_session
name = 'grant'
indexes = ['short']
query: 'Query[AwardGrant]'
type_s = 'Generic Award Grant'
_id = FieldProperty(S.ObjectId)
award_id = ForeignIdProperty(Award, if_missing=None)
award = RelationProperty(Award, via='award_id')
granted_by_neighborhood_id = ForeignIdProperty(
'Neighborhood', if_missing=None)
granted_by_neighborhood = RelationProperty(
'Neighborhood', via='granted_by_neighborhood_id')
granted_to_project_id = ForeignIdProperty('Project', if_missing=None)
granted_to_project = RelationProperty(
'Project', via='granted_to_project_id')
award_url = FieldProperty(str, if_missing='')
comment = FieldProperty(str, if_missing='')
timestamp = FieldProperty(datetime, if_missing=datetime.utcnow)
def index(self):
result = Artifact.index(self)
result.update(
_id_s=self._id,
short_s=self.short,
timestamp_dt=self.timestamp,
full_s=self.full)
result['award_s'] = self.award.short if self.award else None
return result
@property
def icon(self):
return AwardFile.query.get(award_id=self.award_id)
def url(self):
slug = str(self.granted_to_project.shortname).replace('/', '_')
return h.urlquote(slug)
def longurl(self):
slug = str(self.granted_to_project.shortname).replace('/', '_')
slug = self.award.longurl() + '/' + slug
return h.urlquote(slug)
def shorthand_id(self):
if self.award:
return self.award.short
else:
return None
class RssFeed(FG.Rss201rev2Feed):
def rss_attributes(self):
attrs = super().rss_attributes()
attrs['xmlns:atom'] = 'http://www.w3.org/2005/Atom'
return attrs
def add_root_elements(self, handler):
super().add_root_elements(handler)
if self.feed['feed_url'] is not None:
handler.addQuickElement('atom:link', '', {
'rel': 'self',
'href': self.feed['feed_url'],
'type': 'application/rss+xml',
})
class Feed(MappedClass):
"""
Used to generate rss/atom feeds. This does not need to be extended;
all feed items go into the same collection
"""
class __mongometa__:
session = project_orm_session
name = 'artifact_feed'
indexes = [
'pubdate',
('artifact_ref.project_id', 'artifact_ref.mount_point'),
(('ref_id', pymongo.ASCENDING),
('pubdate', pymongo.DESCENDING)),
(('project_id', pymongo.ASCENDING),
('app_config_id', pymongo.ASCENDING),
('pubdate', pymongo.DESCENDING)),
# used in ext/user_profile/user_main.py for user feeds
(('author_link', pymongo.ASCENDING),
('pubdate', pymongo.DESCENDING)),
# used in project feed
(('project_id', pymongo.ASCENDING),
('pubdate', pymongo.DESCENDING)),
]
query: 'Query[Feed]'
_id = FieldProperty(S.ObjectId)
ref_id: str = ForeignIdProperty('ArtifactReference')
neighborhood_id = ForeignIdProperty('Neighborhood')
project_id = ForeignIdProperty('Project')
app_config_id = ForeignIdProperty('AppConfig')
tool_name = FieldProperty(str)
title = FieldProperty(str)
link = FieldProperty(str)
pubdate = FieldProperty(datetime, if_missing=datetime.utcnow)
description = FieldProperty(str)
description_cache = FieldProperty(MarkdownCache)
unique_id = FieldProperty(str, if_missing=lambda: h.nonce(40))
author_name = FieldProperty(str, if_missing=lambda: c.user.get_pref(
'display_name') if hasattr(c, 'user') else None)
author_link = FieldProperty(
str, if_missing=lambda: c.user.url() if hasattr(c, 'user') else None)
artifact_reference = FieldProperty(S.Deprecated)
def clear_user_data(self):
""" Redact author data """
self.author_name = ""
self.author_link = ""
title_parts = self.title.partition(" modified by ")
self.title = "".join(title_parts[0:2]) + ("<REDACTED>" if title_parts[2] else '')
@classmethod
def from_username(cls, username):
return cls.query.find({'author_link': f"/u/{username}/"}).all()
@classmethod
def has_access(cls, artifact):
# Enable only for development.
# return True
from allura import model as M
anon = M.User.anonymous()
if not security.has_access(artifact, 'read', user=anon):
return False
if not security.has_access(c.project, 'read', user=anon):
return False
return True
@classmethod
def post(cls, artifact, title=None, description=None, author=None,
author_link=None, author_name=None, pubdate=None, link=None, **kw):
"""
Create a Feed item. Returns the item.
But if anon doesn't have read access, create does not happen and None is
returned.
"""
if not Feed.has_access(artifact):
return
idx = artifact.index()
if author is None:
author = c.user
if author_name is None:
author_name = author.get_pref('display_name')
if title is None:
title = '{} modified by {}'.format(
h.get_first(idx, 'title'), author_name)
if description is None:
description = title
if pubdate is None:
pubdate = datetime.utcnow()
if link is None:
link = artifact.url()
item = cls(
ref_id=artifact.index_id(),
neighborhood_id=artifact.app_config.project.neighborhood_id,
project_id=artifact.app_config.project_id,
app_config_id=artifact.app_config_id,
tool_name=artifact.app_config.tool_name,
title=title,
description=g.markdown.convert(description),
link=link,
pubdate=pubdate,
author_name=author_name,
author_link=author_link or author.url())
unique_id = kw.pop('unique_id', None)
if unique_id:
item.unique_id = unique_id
return item
@classmethod
def feed(cls, q, feed_type, title, link, description,
since=None, until=None, page=None, limit=None):
"Produces feedgenerator Feed"
d = dict(title=title, link=h.absurl(h.urlquote(link)),
description=description, language='en',
feed_url=request.url)
if feed_type == 'atom':
feed = FG.Atom1Feed(**d)
elif feed_type == 'rss':
feed = RssFeed(**d)
limit, page = h.paging_sanitizer(limit or 10, page)
query = defaultdict(dict)
if callable(q):
q = q(since, until, page, limit)
query.update(q)
if since is not None:
query['pubdate']['$gte'] = since
if until is not None:
query['pubdate']['$lte'] = until
cur = cls.query.find(query)
cur = cur.sort('pubdate', pymongo.DESCENDING)
cur = cur.limit(limit)
cur = cur.skip(limit * page)
for r in cur:
feed.add_item(title=r.title,
link=h.absurl(h.urlquote_path_only(r.link)),
pubdate=r.pubdate,
description=r.description,
unique_id=h.absurl(r.unique_id),
author_name=r.author_name,
author_link=h.absurl(r.author_link))
return feed
class VotableArtifact(MappedClass):
"""Voting support for the Artifact. Use as a mixin."""
class __mongometa__:
session = main_orm_session
name = 'vote'
query: 'Query[VotableArtifact]'
votes = FieldProperty(int, if_missing=0)
votes_up = FieldProperty(int, if_missing=0)
votes_down = FieldProperty(int, if_missing=0)
votes_up_users = FieldProperty([str], if_missing=list())
votes_down_users = FieldProperty([str], if_missing=list())
def vote_up(self, user):
voted = self.user_voted(user)
if voted == 1:
# Already voted up - unvote
self.votes_up_users.remove(user.username)
self.votes_up -= 1
elif voted == -1:
# Change vote to negative
self.votes_down_users.remove(user.username)
self.votes_down -= 1
self.votes_up_users.append(user.username)
self.votes_up += 1
else:
self.votes_up_users.append(user.username)
self.votes_up += 1
self.votes = self.votes_up - self.votes_down
def vote_down(self, user):
voted = self.user_voted(user)
if voted == -1:
# Already voted down - unvote
self.votes_down_users.remove(user.username)
self.votes_down -= 1
elif voted == 1:
# Change vote to positive
self.votes_up_users.remove(user.username)
self.votes_up -= 1
self.votes_down_users.append(user.username)
self.votes_down += 1
else:
self.votes_down_users.append(user.username)
self.votes_down += 1
self.votes = self.votes_up - self.votes_down
def user_voted(self, user):
"""Check that user voted for this artifact.
Return:
1 if user voted up
-1 if user voted down
0 if user doesn't vote
"""
if user.username in self.votes_up_users:
return 1
if user.username in self.votes_down_users:
return -1
return 0
@property
def votes_up_percent(self):
votes_count = self.votes_up + self.votes_down
if votes_count == 0:
return 0
return int(float(self.votes_up) / votes_count * 100)
def __json__(self):
return {
'votes_up': self.votes_up,
'votes_down': self.votes_down,
}
class ReactableArtifact(MappedClass):
"""Reaction support for the Artifact. Use as a mixin."""
react_counts = FieldProperty({str: None}, if_missing=dict())
# dict to store reaction counts
react_users = FieldProperty({str: None}, if_missing=dict())
# dict to store reactions vs usernames
def post_reaction(self, r, user):
current_reaction = self.user_reacted(user)
if current_reaction is None:
# no prev reactions. simply append
if r in self.react_users:
self.react_users[r].append(user.username)
else:
self.react_users[r] = [user.username]
self.update_react_count(r)
elif current_reaction == r:
# prev=current so remove
self.react_users[r].remove(user.username)
self.update_react_count(r, add=False)
if len(self.react_users[r]) == 0:
self.react_users.pop(r)
else:
# prev!=currnet so remove prev then append
self.react_users[current_reaction].remove(user.username)
if r in self.react_users:
self.react_users[r].append(user.username)
else:
self.react_users[r] = [user.username]
self.update_react_count(current_reaction, add=False)
self.update_react_count(r)
if len(self.react_users[current_reaction]) == 0:
self.react_users.pop(current_reaction)
def user_reacted(self, user):
for i in self.react_users:
if user.username in self.react_users[i]:
return i
return
def update_react_count(self, r, add=True):
i = 1
if not add:
i = -1
if r in self.react_counts:
self.react_counts[r] += i
if self.react_counts[r] == 0:
self.react_counts.pop(r)
else:
self.react_counts[r] = 1
class MovedArtifact(Artifact):
class __mongometa__:
session = artifact_orm_session
name = 'moved_artifact'
query: 'Query[MovedArtifact]'
_id = FieldProperty(S.ObjectId)
app_config_id = ForeignIdProperty(
'AppConfig', if_missing=lambda: c.app.config._id)
app_config = RelationProperty('AppConfig')
moved_to_url = FieldProperty(str, required=True, allow_none=False)
class SpamCheckResult(MappedClass):
class __mongometa__:
session = main_orm_session
name = 'spam_check_result'
indexes = [
('project_id', 'result'),
('user_id', 'result'),
]
query: 'Query[SpamCheckResult]'
_id = FieldProperty(S.ObjectId)
ref_id: str = ForeignIdProperty('ArtifactReference')
ref = RelationProperty('ArtifactReference', via='ref_id')
project_id = ForeignIdProperty('Project')
project = RelationProperty('Project', via='project_id')
user_id = ForeignIdProperty('User')
user = RelationProperty('User', via='user_id')
timestamp = FieldProperty(datetime, if_missing=datetime.utcnow)
result = FieldProperty(bool)