예제 #1
0
파일: router.py 프로젝트: jcuna/green-crn
def install():
    try:
        if User.query.count() > 0:
            return redirect('/')
    except (ProgrammingError, OperationalError):
        from helpers import run_migration
        run_migration()

    if request.method == 'POST':
        data = request.form
        if 'first_name' in data and data['first_name'] and 'last_name' in data and data['last_name'] \
                and 'email' in data and data['email'] and 'password' in data and data['password']\
                and 'company_name' in data and 'address' in data and 'contact' in data:
            user_data = get_fillable(User, **data)
            user = User(**user_data)
            user.email = user.email.lower()
            user.hash_password()

            user.attributes = UserAttributes(
                user_access=json.dumps(admin_access),
                user_preferences=json.dumps(admin_preferences))

            company_data = get_fillable(CompanyProfile, **data)
            company = CompanyProfile(**company_data)
            company.name = data['company_name']
            if 'logo' in request.files:
                company.logo = request.files.get('logo').read()

            admin_perms = {}

            for endpoint, permission in permissions.items():
                admin_perms.update({permission: ['read', 'write', 'delete']})

            role = Role(name='Admin', permissions=json.dumps(admin_perms))
            db.session.add(role)

            user.roles.append(role)
            db.session.add(user)
            db.session.add(company)

            db.session.commit()

            return redirect('/')
        else:
            return render_template('install.html',
                                   error='Invalid submission'), 400

    return render_template('install.html', error=None)
예제 #2
0
    def put(self, installation_id):
        c = Installation.query.filter_by(id=installation_id).first()

        if not c:
            raise HttpNotFoundException()

        json = get_fillable(Installation, **request.get_json())

        for field, value in json.items():
            setattr(c, field, value)

        data = request.get_json().copy()
        if 'panels' in data:
            InstallationPanelModel.query.filter_by(
                installation_id=installation_id).delete()
            for panel in data['panels']:
                db.session.add(
                    InstallationPanelModel(installation_id=installation_id,
                                           model_id=panel['id'],
                                           quantity=int(panel['quantity']),
                                           serials=panel['serials']))
        if 'inverters' in data:
            InstallationInverterModel.query.filter_by(
                installation_id=installation_id).delete()
            for inverter in data['inverters']:
                db.session.add(
                    InstallationInverterModel(installation_id=installation_id,
                                              model_id=inverter['id'],
                                              quantity=int(
                                                  inverter['quantity']),
                                              serials=inverter['serials']))

        db.session.commit()
        return Result.success(code=201)
예제 #3
0
    def post(self):

        c = CustomerProject(
            **get_fillable(CustomerProject, **request.get_json()))
        db.session.add(c)
        db.session.commit()
        return Result.id(c.id)
예제 #4
0
    def put(self, customer_id):
        c = Customer.query.filter_by(id=customer_id).first()
        if not c:
            raise HttpNotFoundException()

        json = get_fillable(Customer, **request.get_json())
        for field, value in json.items():
            setattr(c, field, value)

        db.session.commit()
        return Result.success(code=201)
예제 #5
0
    def put(self, project_id):
        project = CustomerProject.query.filter_by(id=project_id).first()
        if not project:
            raise HttpNotFoundException()

        json = get_fillable(CustomerProject, **request.get_json())
        for field, value in json.items():
            setattr(project, field, value)

        db.session.commit()
        return Result.success(code=201)
예제 #6
0
    def post(self):
        data = request.get_json().copy()

        if 'request_date' in data:
            data['request_date'] = local_to_utc(data['request_date'])
        if 'response_date' in data:
            data['response_date'] = local_to_utc(data['response_date'])

        financing = InstallationFinancing(
            **get_fillable(InstallationFinancing, **data))
        db.session.add(financing)
        db.session.commit()
        return Result.id(financing.id)
예제 #7
0
    def post(self):
        raw_data = request.get_json()
        user_data = get_fillable(User, **raw_data)
        if 'email' in user_data:
            user_data['email'] = user_data['email'].lower()
        user = User(**user_data)
        if 'password' in raw_data:
            user.hash_password()

        if raw_data['roles']:
            for role in Role.query.filter(Role.id.in_(raw_data['roles'])):
                user.roles.append(role)

        user.attributes = UserAttributes()
        if raw_data['attributes'] and 'access' in raw_data['attributes']:
            user.attributes.user_access = json.dumps(raw_data['attributes']['access'])

        if raw_data['attributes'] and 'preferences' in raw_data['attributes']:
            user.attributes.user_preferences = json.dumps(raw_data['attributes']['preferences'])

        db.session.add(user)
        db.session.commit()

        if not user.password:
            ut = UserToken(target=request.host_url.rstrip('/') + url_for('user_activate_url'))
            ut.new_token(user.email)
            user.tokens.append(ut)
            db.session.commit()
            msg = Message('Verifica Tu Cuenta', recipients=[user.email])
            msg.html = render_template(
                'email/account_activate.html',
                name=user.first_name,
                url=request.host_url,
                token='account/activate/' + ut.token
            )
            current_app.mail(msg)

        return dict(id=user.id)
예제 #8
0
    def post(self):
        data = request.get_json().copy()
        if 'next_follow_up' in data:
            data['next_follow_up'] = local_to_utc(data['next_follow_up'])

        inst = Installation.query.filter_by(id=data['installation_id']).first()

        if inst is None:
            raise HttpNotFoundException()

        follow_up = InstallationFollowUp(
            **get_fillable(InstallationFollowUp, **data))
        follow_up.user_id = request.user.id

        if 'comment' in data:
            follow_up.comments.append(
                InstallationFollowUpComment(user_id=request.user.id,
                                            comment=data['comment']))

        inst.follow_ups.append(follow_up)
        db.session.commit()

        return Result.id(follow_up.id)
예제 #9
0
    def post(self):

        data = request.get_json().copy()
        data['start_date'] = local_to_utc(data['start_date'])

        c = Installation(**get_fillable(Installation, **data))
        if 'panels' in data:
            for panel in data['panels']:
                c.panels.append(
                    InstallationPanelModel(model_id=panel['id'],
                                           quantity=panel['quantity'],
                                           serials=panel['serials']))

        if 'inverters' in data:
            for inverter in data['inverters']:
                c.inverters.append(
                    InstallationInverterModel(model_id=inverter['id'],
                                              quantity=inverter['quantity'],
                                              serials=inverter['serials']))
        c.status = InstallationStatus()
        db.session.add(c)
        db.session.commit()
        return Result.id(c.id)