-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathload_reviews.py
47 lines (36 loc) · 1.29 KB
/
load_reviews.py
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
import sys, os
import pandas as pd
import datetime
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "movierec.settings")
import django
django.setup()
from django.contrib.auth.models import User
from reviews.models import Review, Movie
def save_review_from_row(review_row):
review = Review()
try:
review.id = Review.objects.last().pk + 1 #review_row[0]
except: #TypeError:
review.id = 1
user = User.objects.get(username=''+str(int(review_row[1])))
review.user_name = user
#review.user_name = User.objects.get(id=review_row[1])
review.movie = Movie.objects.get(id=review_row[2])
review.rating = review_row[3]
review.comment = review_row[4]
review.pub_date = datetime.datetime.now()
# review.pub_date = review_row[5].strip() if review_row[5].strip() else datetime.datetime.now()
print 'saving '+review.id
review.save()
if __name__ == "__main__":
if len(sys.argv) == 2:
print "Reading from file " + str(sys.argv[1])
reviews_df = pd.read_csv(sys.argv[1])
print reviews_df
reviews_df.apply(
save_review_from_row,
axis=1
)
print "There are {} reviews in DB".format(Review.objects.count())
else:
print "Please, provide Reviews file path"