Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions OKR_platform/elastic_search.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from elasticsearch import Elasticsearch

es = Elasticsearch()
8 changes: 6 additions & 2 deletions OKR_platform/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,12 @@

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'okr_platform',
'USER': 'postgres',
'PASSWORD': 'password',
'HOST': '127.0.0.1',
'PORT': '',
}
}

Expand Down
1 change: 1 addition & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ djangorestframework = "*"
markdown = "*"
django-filter = "*"
djangorestframework-jwt = "*"
elasticsearch = "*"

[dev-packages]

Expand Down
23 changes: 19 additions & 4 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions docker-compose.dev.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
version: '3.1'

services:
db:
image: postgres
restart: always
environment:
POSTGRES_PASSWORD: password
POSTGRES_DB: okr_platform
volumes:
- ./pgdata:/var/lib/postgresql/data

ports:
- 5432:5432


elasticsearch:
image: docker.elastic.co/elasticsearch/elasticsearch:6.4.0
environment:
- discovery.type=single-node
volumes:
- ./esdata:/var/lib/elasticsearch/data
ports:
- 9200:9200
41 changes: 41 additions & 0 deletions utils/ESmodel.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from django.conf import settings
from OKR_platform import elastic_search as es


class ESMetaClass(type):

def __new__(cls, name, bases, attrs):
if name == 'ESModel':
return type.__new__(cls, name, bases, attrs)
name = name.lower()
attrs['__doc_type__'] = name
cls.__doc_type__ = name
_class = type.__new__(cls, name, bases, attrs)
_class.__doc_type__ = name
print(_class)
return _class


class ESModel(object, metaclass=ESMetaClass):

def destroy(self):
es.delete(index=settings.ES_INDEX, doc_type=self.__doc_type__, id=self.id)
return None

def save(self):
es.index(index=settings.ES_INDEX, id=self.id, doc_type=self.__doc_type__, body=self.__dict__)
return self

@classmethod
def get(cls, id):
res = es.get(index=settings.ES_INDEX, doc_type=cls.__doc_type__, id=id)
return cls(**res['_source'])

@classmethod
def filter(cls, **kwargs):
res = es.search(index=settings.ES_INDEX, doc_type=cls.__doc_type__, body={"query": {"term": kwargs}})['hits']['hits']
return [cls(**item['_source']) for item in res]

@classmethod
def destroy(cls, id):
es.delete(index=settings.ES_INDEX, doc_type=cls.__doc_type__, id=id)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general, we use editorconfig for simple unified file format. 😀

Empty file added utils/__init__.py
Empty file.