Skip to content

Commit

Permalink
Remodel Camera settings in database, remodel User roles in database, …
Browse files Browse the repository at this point in the history
…add two more roles (Editor and Monitor). (#122)
  • Loading branch information
kizniche committed Feb 10, 2017
1 parent 4b7f1cf commit b91485b
Show file tree
Hide file tree
Showing 29 changed files with 322 additions and 497 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## 5.0.0 (Unreleased)

### Features

- Add new user roles: Editor and Monitor

### Miscellaneous

- Database restructuring
Expand Down
12 changes: 6 additions & 6 deletions init_databases.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import sqlalchemy

from mycodo.config import SQL_DATABASE_MYCODO_5
from mycodo.databases.mycodo_db.models_5 import Users
from mycodo.databases.mycodo_db.models_5 import User
from mycodo.databases.utils import session_scope
from mycodo.scripts.utils import test_username, test_password, is_email, query_yes_no

Expand All @@ -43,7 +43,7 @@


def add_user(admin=False):
new_user = Users()
new_user = User()

print('\nAdd user to database')

Expand All @@ -70,9 +70,9 @@ def add_user(admin=False):
break

if admin:
new_user.user_restriction = 'admin'
new_user.user_role = 1
else:
new_user.user_restriction = 'guest'
new_user.user_role = 4

new_user.user_theme = 'slate'
try:
Expand All @@ -92,7 +92,7 @@ def delete_user(username):
if query_yes_no("Confirm delete user '{}' from user database.".format(username)):
try:
with session_scope(MYCODO_DB_PATH) as db_session:
user = db_session.query(Users).filter(Users.user_name == username).one()
user = db_session.query(User).filter(User.user_name == username).one()
db_session.delete(user)
print("User deleted.")
sys.exit(0)
Expand All @@ -105,7 +105,7 @@ def change_password(username):
print('Changing password for {}'.format(username))

with session_scope(MYCODO_DB_PATH) as db_session:
user = db_session.query(Users).filter(Users.user_name == username).one()
user = db_session.query(User).filter(User.user_name == username).one()

while True:
user_password = getpass.getpass('Password: ')
Expand Down
26 changes: 26 additions & 0 deletions mycodo/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,32 @@
'altitude': 'm'
}

CAM_TYPES = (
'Still',
'Time-lapse',
'Stream'
)

USER_ROLES = [
dict(id=1, name='Admin',
edit_camera=True, edit_controllers=True, edit_users=True,
view_settings=True, view_camera=True, view_stats=True,
view_logs=True),
dict(id=2, name='Editor',
edit_camera=True, edit_controllers=True, edit_users=False,
view_settings=True, view_camera=True, view_stats=True,
view_logs=True),
dict(id=3, name='Monitor',
edit_camera=False, edit_controllers=False, edit_users=False,
view_settings=True, view_camera=True, view_stats=True,
view_logs=True),
dict(id=4, name='Guest',
edit_camera=False, edit_controllers=False, edit_users=False,
view_settings=False, view_camera=False, view_stats=False,
view_logs=False)
]


INSTALL_DIRECTORY = os.path.dirname(os.path.realpath(__file__)) + '/..'

LOCK_PATH = '/var/lock'
Expand Down
31 changes: 15 additions & 16 deletions mycodo/controller_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@

# Classes
from databases.mycodo_db.models_5 import (
CameraStill,
CameraStream,
Camera,
Relay,
Sensor,
SensorConditional,
Expand Down Expand Up @@ -230,7 +229,7 @@ def __init__(self, ready, sensor_id):

self.device_recognized = True

# Set up sensor
# Set up sensors or devices
if self.device in ['EDGE', 'ADS1x15', 'MCP342x']:
self.measure_sensor = None
elif self.device == 'RPiCPULoad':
Expand All @@ -240,6 +239,16 @@ def __init__(self, ready, sensor_id):
elif self.device == 'AM2302':
self.measure_sensor = DHT22Sensor(self.sensor_id,
int(self.location))
elif self.device == 'AM2315':
self.measure_sensor = AM2315Sensor(self.i2c_bus)
elif self.device == 'ATLAS_PT1000':
self.measure_sensor = AtlasPT1000Sensor(self.i2c_address,
self.i2c_bus)
elif self.device == 'BME280':
self.measure_sensor = BME280Sensor(self.i2c_address,
self.i2c_bus)
elif self.device == 'BMP':
self.measure_sensor = BMPSensor(self.i2c_bus)
elif self.device == 'CHIRP':
self.measure_sensor = ChirpSensor(self.i2c_address,
self.i2c_bus)
Expand All @@ -255,18 +264,8 @@ def __init__(self, ready, sensor_id):
state=self.power_state)
elif self.device == 'HTU21D':
self.measure_sensor = HTU21DSensor(self.i2c_bus)
elif self.device == 'AM2315':
self.measure_sensor = AM2315Sensor(self.i2c_bus)
elif self.device == 'ATLAS_PT1000':
self.measure_sensor = AtlasPT1000Sensor(self.i2c_address,
self.i2c_bus)
elif self.device == 'K30':
self.measure_sensor = K30Sensor()
elif self.device == 'BME280':
self.measure_sensor = BME280Sensor(self.i2c_address,
self.i2c_bus)
elif self.device == 'BMP':
self.measure_sensor = BMPSensor(self.i2c_bus)
elif self.device == 'SHT1x_7x':
self.measure_sensor = SHT1x7xSensor(self.location,
self.sht_clock_pin,
Expand All @@ -284,7 +283,7 @@ def __init__(self, ready, sensor_id):
self.device_recognized = False
self.logger.debug("Device '{device}' not recognized".format(
device=self.device))
raise Exception("{device} is not a valid device type.".format(
raise Exception("'{device}' is not a valid device type.".format(
device=self.device))

self.edge_reset_timer = time.time()
Expand Down Expand Up @@ -492,11 +491,11 @@ def check_conditionals(self, cond_id):
message += "Status: {}. ".format(cmd_status)

if self.cond_camera_record[cond_id] in ['photo', 'photoemail']:
camera_still = db_retrieve_table_daemon(CameraStill, entry='first')
camera_still = db_retrieve_table_daemon(Camera, device_id=1)
attachment_file = camera_record(
'photo', camera_still)
elif self.cond_camera_record[cond_id] in ['video', 'videoemail']:
camera_stream = db_retrieve_table_daemon(CameraStream, entry='first')
camera_stream = db_retrieve_table_daemon(Camera, device_id=3)
attachment_file = camera_record(
'video', camera_stream, duration_sec=5)

Expand Down
21 changes: 14 additions & 7 deletions mycodo/databases/mycodo_db/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,17 @@
DisplayOrder,
Method,
Misc,
CameraTimelapse,
CameraStream,
CameraStill,
Camera,
CameraType,
Remote,
Role,
SMTP,
Remote
User
)

from mycodo.config import (
CAM_TYPES,
USER_ROLES
)

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -95,11 +101,12 @@ def populate_db(db_path):
Session = sessionmaker(bind=engine)
session = Session()
try:
for index, each_camera_type in enumerate(CAM_TYPES, 1):
insert_or_ignore(Camera(id=index, camera_type=each_camera_type), session)
for each_role in USER_ROLES:
insert_or_ignore(Role(**each_role), session)
insert_or_ignore(AlembicVersion(), session)
insert_or_ignore(DisplayOrder(id=1), session)
insert_or_ignore(CameraTimelapse(id=1), session)
insert_or_ignore(CameraStill(id=1), session)
insert_or_ignore(CameraStream(id=1), session)
insert_or_ignore(Misc(id=1), session)
insert_or_ignore(SMTP(id=1), session)
finally:
Expand Down

0 comments on commit b91485b

Please sign in to comment.