-
Notifications
You must be signed in to change notification settings - Fork 0
/
load_sample_db.py
128 lines (107 loc) · 3.51 KB
/
load_sample_db.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
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
import csv
import random
from datetime import datetime
from afroquotes.models import Quote, Annotation, User # Replace 'your_app' with the actual app name
from afroquotes.serializers import QuotesSerializer, CreateUserSerializer, QResponseSerializer # Replace with actual import paths
from faker import Faker
import logging
faker = Faker()
def read_csv(filename):
# Assume users is a predefined dictionary with user data
users = {
"password":"pass",
"username":"prosper56",
"email":"[email protected]",
"first_name": "prosper",
"last_name":"Onye",
},
"password":"pass",
"username":"shaun88",
"email":"[email protected]",
"first_name": "shaun",
"last_name":"shaun88",
},
"password":"pass",
"username":"promise45",
"email":"[email protected]",
"first_name": "promise",
"last_name":"prom",
}
}
# Create users
for user in users.items():
print(user[1])
print(type(user[1]))
try:
userializer = CreateUserSerializer(data=user[1])
userializer.is_valid(raise_exception=True)
userializer.save()
except:
pass
# Load CSV
with open(filename, 'r') as filter_file:
csv_r = csv.reader(filter_file, delimiter=",")
quotes = []
next(csv_r)
for f in csv_r:
artist = f[2].strip()
song = f[3].strip()
quote_text = f[1].strip()
annotation_text = f[4].strip() # Assuming the annotation is in the 5th column
contributor_email = f[6] if f[6] in list(users.keys()) else "[email protected]"
contributor = User.objects.get(email=contributor_email)
if quote_text and artist and song:
quote_data = {
"quote": quote_text,
"artist": artist,
"song": song,
"contributor": contributor
}
quote = Quote(**quote_data)
logging.info(f"CREATED QUOTE: : {quote_data}")
quote.save()
# Create annotation
if len(annotation_text) > 5:
annotation_data = {
"annotation": annotation_text,
"annotator": contributor,
"annotated": quote,
"verified": False,
"annotation_view_count": random.randint(0, 100),
"last_viewed": faker.date_time_this_year().strftime('%Y-%m-%d %H:%M:%S'),
"timestamp": datetime.now(),
}
annotation = Annotation(**annotation_data)
annotation.save()
logging.info(f"CREATED QUOTE: : {annotation_data}")
# Add approvers
approvers = User.objects.filter(email__in=list(users.keys()))
for approver in approvers:
annotation.approvers.add(approver)
annotation.save()
read_csv("afroquotes/afroquotes_dumps.csv")
# def main():
# print("RUnnig Thangs")
# logging.info("RUNNING THANGS::::::::")
# quotes = Quote.objects.all()
# logging.info(f"FOUND TOTAL QUOTES: : {len(quotes)}")
# quotes = [q.serialize() for q in quotes]
# logging.info(f"FOUND TOTAL QUOTES: : {quotes[:6]}")
# count = 0
# for i in quotes:
# annotation = i["annotation"]["annotation"]
# # logging.info(f"FOUND BLANK ANNOTATION FOR QUOTE: : {annotation}")
# if annotation == '':
# count += 1
# snippets = {"id": {i["id"]}, "annotation":{**i["annotation"]} }
# logging.info(f"FOUND BLANK ANNOTATION FOR QUOTE: : {snippets}")
# Annotation.objects.get(id=i["id"]).delete()
# # # logging.info(f"REMOVED BLANK ANNOTATION FOR QUOTE: : {snippets}")
# logging.info(f"TOTAL BLANK ANNOTATION REMOVED: : {count}")
# main()
# annotation = Annotation.objects.get(id=i["id"]).delete()
# if __name__ == '__main__':
# main()