forked from taoteg/es-django-tut
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added commond for populating index wiht data. Had to adapt to ES6. Se…
…ems to be working without error now. Movingon to final steps of part 3.
- Loading branch information
Showing
3 changed files
with
74 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,88 +1,30 @@ | ||
from django.core.management.base import BaseCommand | ||
from core.models import University, Course, Student | ||
|
||
# ADDED. | ||
from model_mommy import mommy | ||
import random | ||
import names | ||
from elasticsearch.client import IndicesClient | ||
from django.conf import settings | ||
from django.core.management.base import BaseCommand | ||
from core.models import Student | ||
|
||
|
||
class Command(BaseCommand): | ||
help = "My shiny new management command." | ||
|
||
|
||
def add_arguments(self, parser): | ||
# parser.add_argument('sample', nargs='+') | ||
# Take arg for number of students to generate. | ||
parser.add_argument('count', nargs=1, type=int) | ||
|
||
|
||
def handle(self, *args, **options): | ||
# TEST COMMAND WORKS. | ||
# print('Generating dummy data...') | ||
# GENERATE THE DUMMY DATA. | ||
self.clear() | ||
self.make_universities() | ||
self.make_courses() | ||
self.make_students(options) | ||
self.connect_courses() | ||
|
||
|
||
def clear(self): | ||
Student.objects.all().delete() | ||
University.objects.all().delete() | ||
Course.objects.all().delete() | ||
|
||
|
||
def make_universities(self): | ||
university_names = ('UT', 'TAMU', 'MIT', 'MGU', 'CalTech', 'KPI', 'DPI', 'PSTU') | ||
self.universities = [] | ||
for name in university_names: | ||
uni = mommy.make(University, name=name) | ||
self.universities.append(uni) | ||
|
||
print('Populating elastic search index...') | ||
self.recreate_index() | ||
|
||
def make_courses(self): | ||
template_options = ['CS%s0%s', 'MATH%s0%s', 'CHEM%s0%s', 'PHYS%s0%s'] | ||
self.courses = [] | ||
for num in range(1, 4): | ||
for course_num in range(1, 4): | ||
for template in template_options: | ||
name = template % (course_num, num) | ||
course = mommy.make(Course, name=name) | ||
self.courses.append(course) | ||
def recreate_index(self): | ||
indices_client = IndicesClient(client=settings.ES_CLIENT) | ||
|
||
index_name = Student._meta.es_index_name | ||
|
||
def make_students(self, options): | ||
self.students = [] | ||
# for _ in xrange(options.get('count')[0]): # Python 2 version. | ||
for _ in range(options.get('count')[0]): # Python 3 version. | ||
stud = mommy.prepare( | ||
Student, | ||
university=random.choice(self.universities), | ||
first_name=names.get_first_name(), | ||
last_name=names.get_last_name(), | ||
age=random.randint(17, 25) | ||
) | ||
self.students.append(stud) | ||
Student.objects.bulk_create(self.students) | ||
if indices_client.exists(index_name): | ||
indices_client.delete(index=index_name) | ||
|
||
indices_client.create(index=index_name) | ||
|
||
def connect_courses(self): | ||
ThroughModel = Student.courses.through | ||
stud_courses = [] | ||
for student_id in Student.objects.values_list('pk', flat=True): | ||
courses_already_linked = [] | ||
for _ in range(random.randint(1, 10)): | ||
index = random.randint(0, len(self.courses) - 1) | ||
if index not in courses_already_linked: | ||
courses_already_linked.append(index) | ||
else: | ||
continue | ||
stud_courses.append( | ||
ThroughModel( | ||
student_id=student_id, | ||
course_id=self.courses[index].pk | ||
) | ||
) | ||
ThroughModel.objects.bulk_create(stud_courses) | ||
indices_client.put_mapping( | ||
doc_type=Student._meta.es_type_name, | ||
body=Student._meta.es_mapping, | ||
index=index_name | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters