Changeset bd0cb38 for src/mapservice


Ignore:
Timestamp:
Feb 29, 2020, 4:48:24 PM (7 years ago)
Author:
Joachim Langenbach <joachim.langenbach@…>
Branches:
debian/bullseye, master
Children:
a88607b
Parents:
306f18a
git-author:
Joachim Langenbach <joachim.langenbach@…> (02/29/20 16:01:11)
git-committer:
Joachim Langenbach <joachim.langenbach@…> (02/29/20 16:48:24)
Message:
  • Added license activated to User object and database table
  • Registering a user is now working
Location:
src/mapservice
Files:
1 added
8 edited

Legend:

Unmodified
Added
Removed
  • src/mapservice/codegen/code_generator

    r306f18a rbd0cb38  
    2525cp -a ../../../common/resources/* static/resources/
    2626cp -a ../../../common/icon/* static/resources/
     27mkdir -p templates
     28cp -a ../../templates/* templates/
    2729
    2830echo "Generating backend and client API code"
  • src/mapservice/codegen/templates/__init__.mustache

    r306f18a rbd0cb38  
     1import connexion
     2from flask_cors import CORS
     3from flask_mail import Mail
     4
     5app = connexion.App(__name__, specification_dir='./openapi/')
     6application = app.app
     7CORS(application)
     8mail = Mail()
  • src/mapservice/codegen/templates/__main__.mustache

    r306f18a rbd0cb38  
    66{{/supportPython2}}
    77
    8 import connexion
    9 from flask_cors import CORS
    10 #from flask import render_template
    118import flask
    129from sqlalchemy import event
    1310
    14 from {{packageName}} import encoder
    15 
     11from {{packageName}} import encoder, app, application, mail
    1612from {{packageName}}.server_impl.database.Model import db, User, ApiKey, db_setup, db_test_setup
    17 
    1813from {{packageName}}.server_impl.ValidatorWorkaround import ValidatorWorkaround
    19 
    20 app = connexion.App(__name__, specification_dir='./openapi/')
    21 application = app.app
    22 CORS(application)
    2314
    2415@application.before_first_request
    … …  
    4637
    4738        db.init_app(application)
     39        mail.init_app(application)
    4840
    4941        app.run(port={{serverPort}})
  • src/mapservice/codegen/templates/requirements.mustache

    r306f18a rbd0cb38  
    88flask-sqlalchemy
    99flask-cors
     10flask-mail
    1011shapely
    1112geoalchemy2
  • src/mapservice/openapi.yaml

    r306f18a rbd0cb38  
    792792        activated:
    793793          type: boolean
     794        licenseAccepted:
     795          type: boolean
    794796        apiKeys:
    795797          $ref: '#/components/schemas/apiKeys'
    … …  
    808810        password:
    809811          type: string
     812        licenseAccepted:
     813          type: boolean
    810814      required:
    811815        - username
    … …  
    814818        - lastName
    815819        - password
     820        - licenseAccepted
    816821    Water:
    817822      title: Water
    … …  
    11581163        lastName: Roe
    11591164        password: sdfl.1238_ASDkeil?
     1165        licenseAccepted: true
    11601166    Water:
    11611167      value:
  • src/mapservice/src/server_impl/Config.py

    r306f18a rbd0cb38  
    2121        SQLALCHEMY_DATABASE_URI = 'sqlite:////tmp/test.db'
    2222        SQLALCHEMY_TRACK_MODIFICATIONS = False
     23        MAIL_SERVER = 'localhost'
     24        MAIL_PORT = 25
     25        MAIL_DEFAULT_SENDER = 'GNUBoat Mapservice <gnuboat@engsas.de>'
     26        MAPSERVICE_WEBCLIENT = '/'
    2327
    2428class ProductionConfig(Config):
    … …  
    2832        DEBUG = True
    2933        CREATE_TEST_DATABASE = True
     34        MAPSERVICE_WEBCLIENT = 'http://localhost:8081'
    3035
    3136class TestingConfig(Config):
    … …  
    3338        DEBUG = False
    3439        CREATE_TEST_DATABASE = True
     40        MAPSERVICE_WEBCLIENT = 'http://localhost:8081'
  • src/mapservice/src/server_impl/controllers/UsersController.py

    r306f18a rbd0cb38  
    11'''
    2     Copyright (C) 2019 EngSaS - Engineering Solutions and Services Langenbach. All rights reserved.
     2    Copyright (C) 2019 - 2020 EngSaS - Engineering Solutions and Services Langenbach. All rights reserved.
    33
    44    This program is free software: you can redistribute it and/or modify
    … …  
    1919import datetime
    2020import hashlib
     21import re
     22import flask
     23from flask import request, render_template
     24from flask_mail import Message
    2125
     26from openapi_server import mail
    2227from ..database.Model import User, ApiKey, db
    2328from ..HttpErrors import MapserviceBadRequest, MapserviceUnauthorized, MapserviceNotFound
    2429
     30mail_regex = re.compile('[^@]+@[^@]+\.[^@]+')
     31
    2532def users_activate(activationCode):
    2633        user = User.query.filter_by(activation_code=activationCode).all()
     34       
     35        webclient = flask.current_app.config['MAPSERVICE_WEBCLIENT']
     36       
    2737        if len(user) != 1:
    2838                return 'Bad Request', 302, {'Location': 'activation_failed.html'}
    … …  
    3141        user.activated = True
    3242        user.activation_code = ""
    33         return 'Success', 302, {'Location': 'activation_succeeded.html'}
     43        db.session.commit()
     44        return 'Success', 302, {'Location': '{}/login'.format(webclient)}
    3445
    3546def users_api_key_create():
    … …  
    6677                raise MapserviceUnauthorized
    6778       
     79        return user_to_dict(user)
     80
     81def users_register(user_creation):
     82        if user_creation is None:
     83                raise MapserviceBadRequest(detail='No data specified')
     84        if user_creation.username is None or user_creation.username == "":
     85                raise MapserviceBadRequest(detail='No username given')
     86        if user_creation.password is None or user_creation.password == "":
     87                raise MapserviceBadRequest(detail='No password given')
     88        if user_creation.first_name is None or user_creation.first_name == "":
     89                raise MapserviceBadRequest(detail='No first name given')
     90        if user_creation.last_name is None or user_creation.last_name == "":
     91                raise MapserviceBadRequest(detail='No last name given')
     92        if user_creation.email is None or user_creation.email == "":
     93                raise MapserviceBadRequest(detail='No email given')
     94        if not mail_regex.match(user_creation.email):
     95                raise MapserviceBadRequest(detail='Email contains more than one @, no @ or no .')
     96        if user_creation.license_accepted is None or not user_creation.license_accepted:
     97                raise MapserviceBadRequest(detail='You must accept the license to register an account')
     98       
     99        # add user to the database
     100        user = User.factory(user_creation.username, user_creation.password, user_creation.email)
     101        user.first_name = user_creation.first_name
     102        user.last_name = user_creation.last_name
     103        user.license_accepted = user_creation.license_accepted
     104        db.session.add(user)
     105        db.session.commit()
     106       
     107        # send activation mail
     108        activation_url = '{}/activate/{}'.format(request.base_url, user.activation_code)
     109       
     110        message = Message("Activate your GNUBoat account", recipients=[user.email])
     111        message.body = 'Hello {},\nthank you for support the project with your own measurings. To activate your GNUBoat account, visit {}.\nKind regards,\nYour GNUBoat team'.format(user.first_name, activation_url)
     112        message.html = render_template('activation_mail.html', first_name=user.first_name, activation_url=activation_url)
     113        mail.send(message)
     114       
     115        return user_to_dict(user)
     116
     117def user_to_dict(user):
    68118        # create user dict and remove not needed entries
    69119        result = user.to_dict()
    … …  
    76126        for apiKey in user.apikeys:
    77127                result['apiKeys'].append(apiKey.key)
    78        
    79128        return result
    80 
    81 def users_register(user_creation):
    82         print(user_creation)
    83        
    84         return 'Not Found', 500
  • src/mapservice/src/server_impl/database/Model.py

    r306f18a rbd0cb38  
    5353        activation_code = db.Column(db.String(255), default='1234')
    5454        #activation_code = db.Column(db.String(255), server_default=text('LEFT(MD5(NOW()), 50)'))
     55        license_accepted = db.Column(db.Boolean(), default=False)
    5556       
    5657        def to_dict(user):
    … …  
    6364                        'lastName': "",
    6465                        'activated': user.activated,
    65                        
     66                        'licenseAccepted': user.license_accepted,
    6667                }
    6768                if user.first_name is not None:
    … …  
    382383                        test_user_john = query[0]
    383384                else:
    384                         test_user_john = User.factory('john', 'testpass2', 'john@example.org')
     385                        test_user_john = User.factory('john', 'testpass2', 'john@example.org', license_accepted=True)
    385386                        test_user_john.activation_code = 'dac9630aec642a428cd73f4be0a03569'
    386387                        db.session.add(test_user_john)
    … …  
    392393                        test_user_jane = query[0]
    393394                else:
    394                         test_user_jane = User.factory('jane', 'testpass', 'jane@example.org')
     395                        test_user_jane = User.factory('jane', 'testpass', 'jane@example.org', license_accepted=True)
    395396                        test_user_jane.activated = True
    396397                        test_user_jane.first_name = 'Jane'
Note: See TracChangeset for help on using the changeset viewer.