Skip to content
This repository has been archived by the owner on Sep 3, 2020. It is now read-only.

Commit

Permalink
Applied autopep8
Browse files Browse the repository at this point in the history
  • Loading branch information
notarock committed May 27, 2020
1 parent 808d3d3 commit ae7a17c
Show file tree
Hide file tree
Showing 13 changed files with 818 additions and 807 deletions.
14 changes: 7 additions & 7 deletions serveur-web/api-v2/app_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def tearDown(self):


def test_home_endpoint(self):
""""
"""
Teste que le endpoint "/" retourne "hello world"
"""
response = self.app.get('/')
Expand All @@ -46,7 +46,7 @@ def test_home_endpoint(self):


def test_buckets_get_all(self):
""""
"""
Test GET => /buckets
"""
response = self.app.get('/buckets')
Expand All @@ -60,7 +60,7 @@ def test_buckets_get_all(self):


def test_buckets_get_by_id(self):
""""
"""
Test GET => /buckets/1
"""
bucket_id = 1
Expand All @@ -76,7 +76,7 @@ def test_buckets_get_by_id(self):


def test_buckets_get_by_id_missing(self):
""""
"""
Test GET => /buckets/9999999999
"""
bucket_id = 9999999999
Expand Down Expand Up @@ -135,7 +135,7 @@ def test_buckets_addremove(self):


def test_sensors_endpoint(self):
""""
"""
Test GET => /sensors
"""
response = self.app.get('/sensors')
Expand All @@ -149,7 +149,7 @@ def test_sensors_endpoint(self):


def test_fans_endpoint(self):
""""
"""
Test GET => /fans
"""
response = self.app.get('/fans')
Expand All @@ -163,7 +163,7 @@ def test_fans_endpoint(self):


def test_lights_endpoint(self):
""""
"""
Test GET => /lights
"""
response = self.app.get('/lights')
Expand Down
243 changes: 122 additions & 121 deletions serveur-web/api-v2/controller/bucket.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,126 +18,127 @@

from datetime import datetime, timezone


class BucketController(object):

"""
INDEX
C'est ici qu'on enverra la liste des buckets.
"""
def index():
buckets = Bucket.get_all()
if buckets is not None:
return {"buckets": [bucket.to_detailed_json() for bucket in buckets]}
else:
response = {
"error": 1,
"message": "No bucket found"
}
return (response, 404)


"""
CREATE
C'est ici qu'on recoit les informations pour enregistrer un nouveau bucket
"""
def create(request):
name = request.data['name']
if name is None: raise Exception()

try:
id_plant = request.data['id_plant']
except Exception:
id_plant = 0

ip_address = request.data['ip_address']
if ip_address is None: raise Exception()


new_bucket = Bucket(None, id_plant, name, ip_address)
new_bucket = new_bucket.save()


return { "bucket" : new_bucket.to_detailed_json()}


"""
/bucket/id
C'est ici qu'on enverra la liste des buckets.
"""
def get(id):
bucket = Bucket.get(id)
if bucket is not None:
return {"bucket": bucket.to_detailed_json()}
else:
response = {
"error": 1,
"message": "bucket not found"
}
return (response, 404)


"""
POST => /bucket/id
C'est ici qu'on met a jours les informations d'un bucket
"""
def update(id):
print("Acces a /bucket/id avec POST")
try:
if request.headers['Content-Type'] != 'application/json':
response = {
"error": 1,
"message": "Content-Type is not application/json"
}
return (response, 400)
elif request.is_json:

name = request.data['name']
if name is None: raise Exception()

try:
id_plant = request.data['id_plant']
except Exception:
id_plant = 0

ip_address = request.data['ip_address']
if ip_address is None: raise Exception()

id = request.data['id']
if id is None: raise Exception()

updated_bucket = Bucket(id, id_plant, name, ip_address)

updated_bucket.save()

return {"bucket": updated_bucket.to_detailed_json()}
else:
raise Exception()
except Exception as e:
print("ERROR: Request is not JSON or has missing fields.")
response = {
"error": 1,
"message": "Missing fields in JSON"
}
print(response)
return (response, 404)


"""
/bucket/id
C'est ici qu'on supprime un bucket
"""
def delete(id):
bucket = Bucket.get(id)
if bucket is not None:
deleted = Bucket.delete(bucket)
response = {
"error": 0,
"deleted": str(deleted)
}
return (response, 200)
else:
response = {
"error": 1,
"message": "bucket not found"
}
return (response, 404)
def index():
"""
INDEX
C'est ici qu'on enverra la liste des buckets.
"""
buckets = Bucket.get_all()
if buckets is not None:
return {"buckets": [bucket.to_detailed_json() for bucket in buckets]}
else:
response = {
"error": 1,
"message": "No bucket found"
}
return (response, 404)

def create(request):
"""
CREATE
C'est ici qu'on recoit les informations pour enregistrer un nouveau bucket
"""
name = request.data['name']
if name is None:
raise Exception()

try:
id_plant = request.data['id_plant']
except Exception:
id_plant = 0

ip_address = request.data['ip_address']
if ip_address is None:
raise Exception()

new_bucket = Bucket(None, id_plant, name, ip_address)
new_bucket = new_bucket.save()

return {"bucket": new_bucket.to_detailed_json()}

def get(id):
"""
/bucket/id
C'est ici qu'on enverra la liste des buckets.
"""

bucket = Bucket.get(id)
if bucket is not None:
return {"bucket": bucket.to_detailed_json()}
else:
response = {
"error": 1,
"message": "bucket not found"
}
return (response, 404)

def update(id):
"""
POST => /bucket/id
C'est ici qu'on met a jours les informations d'un bucket
"""
print("Acces a /bucket/id avec POST")
try:
if request.headers['Content-Type'] != 'application/json':
response = {
"error": 1,
"message": "Content-Type is not application/json"
}
return (response, 400)
elif request.is_json:

name = request.data['name']
if name is None:
raise Exception()

try:
id_plant = request.data['id_plant']
except Exception:
id_plant = 0

ip_address = request.data['ip_address']
if ip_address is None:
raise Exception()

id = request.data['id']
if id is None:
raise Exception()

updated_bucket = Bucket(id, id_plant, name, ip_address)

updated_bucket.save()

return {"bucket": updated_bucket.to_detailed_json()}
else:
raise Exception()
except Exception as e:
print("ERROR: Request is not JSON or has missing fields.")
response = {
"error": 1,
"message": "Missing fields in JSON"
}
print(response)
return (response, 404)

def delete(id):
"""
/bucket/id
C'est ici qu'on supprime un bucket
"""
bucket = Bucket.get(id)
if bucket is not None:
deleted = Bucket.delete(bucket)
response = {
"error": 0,
"deleted": str(deleted)
}
return (response, 200)
else:
response = {
"error": 1,
"message": "bucket not found"
}
return (response, 404)
Loading

0 comments on commit ae7a17c

Please sign in to comment.