1
+ # Licensed under the Apache License, Version 2.0 (the "License");
2
+ # you may not use this file except in compliance with the License.
3
+ # You may obtain a copy of the License at
4
+ #
5
+ # http://www.apache.org/licenses/LICENSE-2.0
6
+ #
7
+ # Unless required by applicable law or agreed to in writing, software
8
+ # distributed under the License is distributed on an "AS IS" BASIS,
9
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10
+ # See the License for the specific language governing permissions and
11
+ # limitations under the License.
1
12
"""
2
- Indexes sqlalchemy changes to elasticsearch. It does not handle
3
- db cascades so they should be avoided.
13
+ Index sqlalchemy Session changes to ElasticSearch.
14
+
15
+ It does not handle non-ORM cascades so they should be avoided.
4
16
"""
5
17
6
- import transaction
7
- from sqlalchemy import event
8
- from elasticsearch import Elasticsearch
9
- from pyramid .threadlocal import get_current_registry
18
+ from elasticsearch_dsl import DocType , String , Date , Integer
10
19
11
- from warehouse .db import _Session
12
20
from warehouse .packaging .models import Release
13
21
22
+ # TODO: most basic documentation
23
+ # TODO: add travis test for server-side cascades?
24
+
25
+
26
+ class ReleaseDoc (DocType ):
27
+ name = String (analyzer = 'snowball' )
28
+ version = Integer ()
29
+ body = String (analyzer = 'snowball' )
30
+ published_from = Date ()
31
+ # TODO: all about those fields
14
32
15
- release_defaults = dict (index = "warehouse" ,
16
- doc_type = "release" )
33
+ class Meta :
34
+ index = 'release'
35
+ model = Release
17
36
18
- def handle_insert (elasticsearch_url , target ):
19
- es = Elasticsearch ([es_url ])
20
- es .index (id = target .name ,
21
- body = {"name" : target .name ,
22
- "version" : target .version ,
23
- "description" : target .description ,
24
- "summary" : target .summary ,
25
- "license" : target .license ,
26
- "download_url" : target .download_url },
27
- ** release_defaults )
37
+ @classmethod
38
+ def from_model_instance (cls , obj ):
39
+ return cls (
40
+ id = obj .name ,
41
+ name = obj .name ,
42
+ version = obj .version ,
43
+ description = obj .description ,
44
+ summary = obj .summary ,
45
+ license = obj .license ,
46
+ download_url = obj .download_url ,
47
+ )
28
48
29
- def handle_delete (elasticsearch_url , target ):
30
- es = Elasticsearch ([es_url ])
31
- es .delete (id = target .id , ** release_defaults )
32
49
33
50
def includeme (config ):
34
- """Register elasticsearch callbacks"""
35
- elasticsearch_url = config .registry .settings ["elasticsearch.url" ]
36
-
37
- @event .listens_for (Release , 'after_insert' )
38
- @event .listens_for (Release , 'after_update' )
39
- def release_insert_update (mapper , connection , target ):
40
- """Signal insert/update events for Release model"""
41
- tx = transaction .get ()
42
- tx .addAfterCommitHook (handle_insert , args = (elasticsearch_url , target ))
43
-
44
- @event .listens_for (Release , 'before_delete' )
45
- def release_delete (mapper , connection , target ):
46
- """Signal idelete event for Release model"""
47
- tx = transaction .get ()
48
- tx .addAfterCommitHook (handle_delete , args = (elasticsearch_url , target ))
49
-
50
- @event .listens_for (_Session , 'after_bulk_update' )
51
- def release_after_bulk_update (update_context ):
52
- import pdb ;pdb .set_trace ()
53
-
54
- @event .listens_for (_Session , 'before_bulk_delete' )
55
- def release_after_bulk_delete (update_context ):
56
- """Get affected ids before they are deleted"""
57
- import pdb ;pdb .set_trace ()
51
+ config .add_elasticsearch_doctype (ReleaseDoc )
0 commit comments