Add a source field to keep track of the source of the reports

This commit is contained in:
Lucas Verney 2018-11-01 15:43:34 +01:00
parent 903ad14bbc
commit 20dd75b152
4 changed files with 48 additions and 1 deletions

43
scripts/migrations/0.4.py Normal file
View File

@ -0,0 +1,43 @@
#!/usr/bin/env python
"""
Database migration from < 0.3 to 0.3 version.
"""
import os
import sys
import peewee
SCRIPT_DIRECTORY = os.path.dirname(os.path.realpath(__file__))
sys.path.append(os.path.abspath(os.path.join(SCRIPT_DIRECTORY, '..', '..')))
from playhouse.migrate import *
from server.models import db, Report
from server.tools import UTC_now
def run_migration():
if type(db) == peewee.SqliteDatabase:
migrator = SqliteMigrator(db)
elif type(db) == peewee.MySQLDatabase:
migrator = MySQLMigrator(db)
elif type(db) == peewee.PostgresqlDatabase:
migrator = PostgresqlMigrator(db)
else:
return
migrate(
migrator.add_column(
'report', 'source',
peewee.CharField(max_length=255)
),
)
query = Report.select()
for report in query:
report.source = 'unknown'
report.save()
if __name__ == '__main__':
db.connect()
run_migration()

View File

@ -50,6 +50,7 @@ class Report(BaseModel):
expiration_datetime = peewee.DateTimeField(null=True)
upvotes = peewee.IntegerField(default=0)
downvotes = peewee.IntegerField(default=0)
source = peewee.CharField(max_length=255)
def to_json(self):
return {

View File

@ -140,6 +140,7 @@ def post_report():
"upvotes": 0,
"lng": 2.385234797066081,
"type": "pothole",
"source": "survey"
},
"type": "reports",
@ -168,7 +169,8 @@ def post_report():
r = Report(
type=payload['type'],
lat=payload['lat'],
lng=payload['lng']
lng=payload['lng'],
source=payload.get('source', 'unknown')
)
# Handle expiration
if r.type in ['accident', 'gcum']:

View File

@ -12,6 +12,7 @@ export function saveReport(type, lat, lng) {
type,
lat,
lng,
source: 'survey',
}),
headers: AUTHORIZATION_HEADERS,
})