From 6c378f86aa65104834e38a0e42003d405761e437 Mon Sep 17 00:00:00 2001 From: topilski Date: Sat, 1 Jun 2019 10:36:45 -0400 Subject: [PATCH] Add user --- app/config/public_config.py | 4 +- app/constants.py | 11 ++++++ app/service/forms.py | 25 ++++++++++--- app/service/service_entry.py | 4 ++ app/service/view.py | 43 ++++++++++------------ app/templates/service/user/add.html | 4 ++ app/templates/service/user/base.html | 43 ++++++++++++++++++++++ app/templates/service/user/edit.html | 4 ++ app/templates/user/settings.html | 55 +++++++++++++++++++--------- 9 files changed, 143 insertions(+), 50 deletions(-) create mode 100644 app/templates/service/user/add.html create mode 100644 app/templates/service/user/base.html create mode 100644 app/templates/service/user/edit.html diff --git a/app/config/public_config.py b/app/config/public_config.py index dccac59..3760421 100644 --- a/app/config/public_config.py +++ b/app/config/public_config.py @@ -1,5 +1,5 @@ -PUBLIC_CONFIG = {'site': {'title': 'FastoCloud', 'keywords': 'video,cloud,iptv,service,server'}, +PUBLIC_CONFIG = {'site': {'title': 'FastoCloud', 'keywords': 'video,cloud,iptv,service,restreamer'}, 'company': {'title': 'FastoGT', 'domain': 'https://fastogt.com'}, 'support': {'contact_email': 'support@fastogt.com', 'contact_address': 'Republic of Belarus, Minsk, Stadionnay str. 5'}, - 'project': {'version': '1.0.0', 'version_type': 'release'}} + 'project': {'version': '1.1.0', 'version_type': 'rc'}} diff --git a/app/constants.py b/app/constants.py index 823d1ae..29a19da 100644 --- a/app/constants.py +++ b/app/constants.py @@ -184,3 +184,14 @@ class Roles(IntEnum): WRITE = 1 ADMIN = 2 SUPPORT = 3 + + @classmethod + def choices(cls): + return [(choice, choice.name) for choice in cls] + + @classmethod + def coerce(cls, item): + return cls(int(item)) if not isinstance(item, cls) else item + + def __str__(self): + return str(self.value) diff --git a/app/service/forms.py b/app/service/forms.py index a121b33..e26cae0 100644 --- a/app/service/forms.py +++ b/app/service/forms.py @@ -1,11 +1,11 @@ from flask_wtf import FlaskForm from flask_babel import lazy_gettext from wtforms.fields import StringField, SubmitField, FileField, SelectField, FormField -from wtforms.validators import InputRequired, Length +from wtforms.validators import InputRequired, Length, Email from app.common_forms import HostAndPortForm from app.service.service_entry import ServiceSettings -from app.constants import StreamType +import app.constants as constants class ServiceSettingsForm(FlaskForm): @@ -51,11 +51,24 @@ class ActivateForm(FlaskForm): class UploadM3uForm(FlaskForm): - AVAILABLE_STREAM_TYPES_FOR_UPLOAD = [(StreamType.RELAY, 'Relay'), (StreamType.ENCODE, 'Encode'), - (StreamType.CATCHUP, 'Catchup'), (StreamType.TEST_LIFE, 'Test life'), - (StreamType.VOD_RELAY, 'Vod relay'), (StreamType.VOD_ENCODE, 'Vod encode')] + AVAILABLE_STREAM_TYPES_FOR_UPLOAD = [(constants.StreamType.RELAY, 'Relay'), (constants.StreamType.ENCODE, 'Encode'), + (constants.StreamType.CATCHUP, 'Catchup'), + (constants.StreamType.TEST_LIFE, 'Test life'), + (constants.StreamType.VOD_RELAY, 'Vod relay'), + (constants.StreamType.VOD_ENCODE, 'Vod encode')] file = FileField() - type = SelectField(lazy_gettext(u'Type:'), coerce=StreamType.coerce, validators=[InputRequired()], + type = SelectField(lazy_gettext(u'Type:'), coerce=constants.StreamType.coerce, validators=[InputRequired()], choices=AVAILABLE_STREAM_TYPES_FOR_UPLOAD) submit = SubmitField(lazy_gettext(u'Upload')) + + +class UserAddForm(FlaskForm): + AVAILABLE_ROLES = [(constants.Roles.READ, 'Read'), (constants.Roles.WRITE, 'Write'), + (constants.Roles.ADMIN, 'Admin'), (constants.Roles.SUPPORT, 'Support')] + + email = StringField(lazy_gettext(u'Email:'), + validators=[InputRequired(), Email(message=lazy_gettext(u'Invalid email')), Length(max=30)]) + role = SelectField(lazy_gettext(u'Role:'), coerce=constants.Roles.coerce, validators=[InputRequired()], + choices=AVAILABLE_ROLES) + apply = SubmitField(lazy_gettext(u'Apply')) diff --git a/app/service/service_entry.py b/app/service/service_entry.py index 90a45cc..9c62607 100644 --- a/app/service/service_entry.py +++ b/app/service/service_entry.py @@ -38,3 +38,7 @@ class ServiceSettings(Document, ServerSettings): out.uri) return result + + def add_user(self, user: UserPair): + self.users.append(user) + self.save() diff --git a/app/service/view.py b/app/service/view.py index a81895d..bf1e760 100644 --- a/app/service/view.py +++ b/app/service/view.py @@ -1,15 +1,14 @@ import os -from bson.objectid import ObjectId - from flask_classy import FlaskView, route from flask import render_template, redirect, url_for, request, jsonify from flask_login import login_required, current_user from app import get_runtime_folder -from app.service.forms import ServiceSettingsForm, ActivateForm, UploadM3uForm +from app.service.forms import ServiceSettingsForm, ActivateForm, UploadM3uForm, UserAddForm from app.service.service_entry import ServiceSettings, UserPair from app.utils.m3u_parser import M3uParser +from app.home.user_loging_manager import User import app.constants as constants @@ -141,6 +140,20 @@ class ServiceView(FlaskView): # broadcast routes + @login_required + @route('/user/add/', methods=['GET', 'POST']) + def user_add(self, sid): + form = UserAddForm() + if request.method == 'POST' and form.validate_on_submit(): + user = User.objects(email=form.email.data).first() + server = ServiceSettings.objects(id=sid).first() + if server and user: + admin = UserPair(user.id, form.role.data) + server.add_user(admin) + return jsonify(status='ok'), 200 + + return render_template('service/user/add.html', form=form) + @login_required @route('/add', methods=['GET', 'POST']) def add(self): @@ -148,12 +161,12 @@ class ServiceView(FlaskView): form = ServiceSettingsForm(obj=model) if request.method == 'POST' and form.validate_on_submit(): new_entry = form.make_entry() - new_entry.users.append(UserPair(current_user.id, constants.Roles.ADMIN)) - new_entry.save() + admin = UserPair(current_user.id, constants.Roles.ADMIN) + new_entry.add_user(admin) current_user.add_server(new_entry) return jsonify(status='ok'), 200 - return render_template('service/add.html', form=form) + return render_template('service/user/add.html', form=form) @login_required @route('/remove', methods=['POST']) @@ -179,24 +192,6 @@ class ServiceView(FlaskView): return render_template('service/edit.html', form=form) - @login_required - @route('/find_and_add', methods=['POST']) - def find_and_add(self): - sid = request.form['sid'] - if ObjectId.is_valid(sid): - server = ServiceSettings.objects(id=sid).first() - if server: - for user in server.users: - if user.id == current_user.id: - return jsonify(status='failed'), 404 - - server.users.append(current_user.id) - server.save() - current_user.add_server(server) - return jsonify(status='ok'), 200 - - return jsonify(status='failed'), 404 - @route('/log/', methods=['POST']) def log(self, sid): # len = request.headers['content-length'] diff --git a/app/templates/service/user/add.html b/app/templates/service/user/add.html new file mode 100644 index 0000000..a79f364 --- /dev/null +++ b/app/templates/service/user/add.html @@ -0,0 +1,4 @@ +{% extends 'service/user/base.html' %} +{% block title %} +Add user to server +{% endblock %} \ No newline at end of file diff --git a/app/templates/service/user/base.html b/app/templates/service/user/base.html new file mode 100644 index 0000000..0dad536 --- /dev/null +++ b/app/templates/service/user/base.html @@ -0,0 +1,43 @@ +{% from 'bootstrap/wtf.html' import form_field %} +{% macro render_bootstrap_field(field) %} +
+ +
+ {{ field(class='form-control')|safe }} +
+
+{% endmacro %} + +{% macro render_bootstrap_form(form) %} +
+ +
+ {{ form() }} +
+
+{% endmacro %} + +
+ + + +
diff --git a/app/templates/service/user/edit.html b/app/templates/service/user/edit.html new file mode 100644 index 0000000..f81b63c --- /dev/null +++ b/app/templates/service/user/edit.html @@ -0,0 +1,4 @@ +{% extends 'service/base.html' %} +{% block title %} +Edit service +{% endblock %} \ No newline at end of file diff --git a/app/templates/user/settings.html b/app/templates/user/settings.html index bb52df2..b67e6b1 100644 --- a/app/templates/user/settings.html +++ b/app/templates/user/settings.html @@ -60,9 +60,13 @@ Settings | {{ config['PUBLIC_CONFIG'].site.title }} {{ loop.index }} {{ server.name }} - + @@ -80,21 +84,6 @@ Settings | {{ config['PUBLIC_CONFIG'].site.title }} -
-
-

Find and add server by ID:

-
-
-
- -
-
-
- -
-
-