Browse Source

updates

old
Efe Aydın 6 years ago
parent
commit
34fafc0ce8
6 changed files with 125 additions and 56 deletions
  1. +5
    -3
      server_side/api/app.py
  2. +5
    -17
      server_side/api/modules/databases/ratings.json
  3. +50
    -5
      server_side/api/modules/databases/users.json
  4. +4
    -2
      server_side/api/modules/databases/votings.json
  5. +33
    -29
      server_side/api/modules/rating_system.py
  6. +28
    -0
      server_side/api/modules/utility.py

+ 5
- 3
server_side/api/app.py View File

@ -1,7 +1,7 @@
from flask import Flask, send_from_directory from flask import Flask, send_from_directory
from flask_restful import Api from flask_restful import Api
from api.modules import user_info, voting_system, rating_system
from api.modules import user_info, voting_system, rating_system, utility
app = Flask(__name__) app = Flask(__name__)
api = Api(app) api = Api(app)
@ -12,7 +12,7 @@ def send_img(path):
if __name__ == '__main__': if __name__ == '__main__':
context = ('encryption/mycity.crt','encryption/mycity-decrypted.key')
context = ('encryption/mycity.crt', 'encryption/mycity-decrypted.key')
api.add_resource( voting_system.Votings, '/votings', '/votings/' ) api.add_resource( voting_system.Votings, '/votings', '/votings/' )
api.add_resource( voting_system.Voting, '/votings/<int:voting_id>' ) api.add_resource( voting_system.Voting, '/votings/<int:voting_id>' )
api.add_resource( voting_system.Vote, '/vote', '/vote/' ) api.add_resource( voting_system.Vote, '/vote', '/vote/' )
@ -24,5 +24,7 @@ if __name__ == '__main__':
api.add_resource( user_info.Users, '/users', '/users/' ) api.add_resource( user_info.Users, '/users', '/users/' )
api.add_resource( user_info.User, '/users/<path:user_id>', '/users/<path:user_id>/' ) api.add_resource( user_info.User, '/users/<path:user_id>', '/users/<path:user_id>/' )
api.add_resource( user_info.Login, '/login', '/login/' ) api.add_resource( user_info.Login, '/login', '/login/' )
api.add_resource(utility.Resources, '/resources', '/resources/')
app.run(host='0.0.0.0', port=5000, ssl_context=context)
app.run(host='0.0.0.0', port=5000, ssl_context=context)

+ 5
- 17
server_side/api/modules/databases/ratings.json View File

@ -9,25 +9,13 @@
"longitude": 32.9101, "longitude": 32.9101,
"diff": 0.3 "diff": 0.3
}, },
"rates": [
{
"id": 1,
"score": 10,
"rater": "9vard12ty0ad2yvwp3q53rsf3h43r2vq",
"note": "IT WAS AWESOME"
},
{
"rates": {
"9vard12ty0ad2yvwp3q53rsf3h43r2vq": {
"id": 2, "id": 2,
"rater": "9vard12ty0ad2yvwp3q53rsf3h43r2vq", "rater": "9vard12ty0ad2yvwp3q53rsf3h43r2vq",
"score": 10,
"note": null
},
{
"id": 3,
"rater": "9vard12ty0ad2yvwp3q53rsf3h43r2vq",
"score": 5,
"note": null
"score": 8,
"note": "Fuck you"
} }
]
}
} }
] ]

+ 50
- 5
server_side/api/modules/databases/users.json View File

@ -11,11 +11,56 @@
"bus_usage_year": 67 "bus_usage_year": 67
}, },
"daily_electricity_usage": [ "daily_electricity_usage": [
10,
5,
5,
6,
7
32,
34,
29,
28,
35,
31,
36,
38,
37,
40,
45,
47,
45,
47,
48,
44,
49,
53,
55,
49,
46,
42,
37,
34
],
"daily_water_usage": [
32,
34,
29,
28,
35,
31,
36,
38,
37,
40,
45,
47,
45,
47,
48,
44,
49,
53,
55,
49,
46,
42,
37,
34
], ],
"points": 50 "points": 50
} }


+ 4
- 2
server_side/api/modules/databases/votings.json View File

@ -25,12 +25,14 @@
"name": "wooting", "name": "wooting",
"desc": "wooting desc", "desc": "wooting desc",
"img": "/img/voting.jpg", "img": "/img/voting.jpg",
"voters": [],
"voters": [
"9vard12ty0ad2yvwp3q53rsf3h43r2vq"
],
"votes": { "votes": {
"1": { "1": {
"name": "woote 1", "name": "woote 1",
"desc": "woote desc", "desc": "woote desc",
"votes": 2
"votes": 3
}, },
"2": { "2": {
"id": 1, "id": 1,


+ 33
- 29
server_side/api/modules/rating_system.py View File

@ -18,15 +18,16 @@ with open(db_path, 'r') as f:
with open(user_db, 'r') as f: with open(user_db, 'r') as f:
users = json.load(f) users = json.load(f)
class Ratings(Resource): class Ratings(Resource):
def get(self):
def post(self):
""" """
Example URL Query:
Example POST Data:
latitude=<latitude>& latitude=<latitude>&
longitude=<longitude> longitude=<longitude>
""" """
latitude = float(request.args['latitude'])
longitude = float(request.args['longitude'])
latitude = float(request.form['latitude'])
longitude = float(request.form['longitude'])
ret_data = [] ret_data = []
for rating in ratings: for rating in ratings:
@ -45,29 +46,30 @@ class Ratings(Resource):
return ret_data return ret_data
def post(self):
"""
Example POST Data:
name=<rating_name>&
desc=<rating_desc>& # OPTIONAL
img=<rating_img>& # OPTIONAL
"""
args = request.form
rating_id = len(ratings) + 1
rating = {
'id': rating_id,
'name': args['name'],
'desc': args.get('desc'),
'img' : args.get('img'),
'rates': []
}
ratings.append(rating)
with open(db_path, 'w') as f:
json.dump(ratings, f, indent=4)
# def post(self):
# """
# Example POST Data:
# name=<rating_name>&
# desc=<rating_desc>& # OPTIONAL
# img=<rating_img>& # OPTIONAL
# """
# args = request.form
# rating_id = len(ratings) + 1
# rating = {
# 'id': rating_id,
# 'name': args['name'],
# 'desc': args.get('desc'),
# 'img' : args.get('img'),
# 'rates': []
# }
#
# ratings.append(rating)
#
# with open(db_path, 'w') as f:
# json.dump(ratings, f, indent=4)
#
# return rating
return rating
class Rating(Resource): class Rating(Resource):
def get(self, rating_id): def get(self, rating_id):
@ -78,6 +80,7 @@ class Rating(Resource):
except: except:
abort(404, error="Rating {} doesn't exist".format(rating_id)) abort(404, error="Rating {} doesn't exist".format(rating_id))
class Rate(Resource): class Rate(Resource):
def post(self): def post(self):
""" """
@ -93,12 +96,12 @@ class Rate(Resource):
if 0 >= score >= 10: if 0 >= score >= 10:
abort(500, 'Score should be between 0 and 10') abort(500, 'Score should be between 0 and 10')
note = request.form.get('note') note = request.form.get('note')
ratings[rating_id - 1]['rates'].append({
ratings[rating_id - 1]['rates']['9vard12ty0ad2yvwp3q53rsf3h43r2vq'] = {
'id': len(ratings[rating_id - 1]['rates']) + 1, 'id': len(ratings[rating_id - 1]['rates']) + 1,
'rater': request.form['rater_id'], 'rater': request.form['rater_id'],
'score': score, 'score': score,
'note': note 'note': note
})
}
with open(db_path, 'w') as f: with open(db_path, 'w') as f:
json.dump(ratings, f, indent=4) json.dump(ratings, f, indent=4)
@ -106,9 +109,10 @@ class Rate(Resource):
return {'error': 'User doesn\'t exists'} return {'error': 'User doesn\'t exists'}
if __name__ == '__main__': if __name__ == '__main__':
api.add_resource(Ratings, '/ratings', '/ratings/') api.add_resource(Ratings, '/ratings', '/ratings/')
api.add_resource(Rating, '/ratings/<int:rating_id>', '/ratings/<int:rating_id>/') api.add_resource(Rating, '/ratings/<int:rating_id>', '/ratings/<int:rating_id>/')
api.add_resource(Rate, '/rate', '/rate/') api.add_resource(Rate, '/rate', '/rate/')
app.run(host='0.0.0.0', port=5000)
app.run(host='0.0.0.0', port=5000)

+ 28
- 0
server_side/api/modules/utility.py View File

@ -0,0 +1,28 @@
import os
import copy
import json
from api.modules import utils
from flask import Flask, request
from flask_restful import Resource, Api, abort
app = Flask(__name__)
api = Api(app)
db_path = os.path.join(app.root_path, 'databases', 'users.json')
with open(db_path, 'r') as f:
users = json.load(f)
class Resources(Resource):
def post(self):
user = utils.find_by_id(users.values(), request.form['user_id'])
return {
'daily_electricity_usage': user['daily_electricity_usage'],
'daily_water_usage': user['daily_water_usage']
}
if __name__ == '__main__':
api.add_resource(Resources, '/resources', '/resources/')
app.run(host='0.0.0.0', port=5000)

Loading…
Cancel
Save