forked from voteflux/voting-app-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate_ballotspecs_db.py
50 lines (42 loc) · 1.2 KB
/
update_ballotspecs_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
import json
import os
import pymongo
from mode import *
import hashlib
# Data for each document
ID = "id"
SHORT_TITLE = "short_title"
QUESTION = "question"
DESCRIPTION = "description"
START_DATE = "start_date"
CHAMBER = "chamber"
SPONSOR = "sponsor"
# --- also create
BALLOTSPEC_HASH = "ballotspec_hash"
# Connection String
client = pymongo.MongoClient(mongosettings[URL])
db = client[mongosettings[MONGODB]]
ballotspecs_collection = db[mongosettings[BALLOTSPECSCOLLECTION]]
def hash_ballotspec(ballotspec_string):
h = hashlib.sha256()
h.update(str(ballotspec_string).encode('utf-8'))
return(h.hexdigest())
def update_ballotspecs(id, short_title, question, description, start_date, chamber, sponsor):
input_dict = {
ID: id,
SHORT_TITLE: short_title,
QUESTION: question,
DESCRIPTION: description,
START_DATE: start_date,
CHAMBER: chamber,
SPONSOR: sponsor,
}
# create ballotspec_hash
bs_h = hash_ballotspec(json.dumps(input_dict))
try:
ballotspecs_collection.insert_one(
{'_id': input_dict["id"],
'data': input_dict,
BALLOTSPEC_HASH: bs_h})
except Exception as e:
print(e)