コード例 #1
0
    def register(data):
        username = data["username"]
        password = data["password"]

        if User.query.filter_by(username=username).first() is not None:
            return err_resp("Username is already taken.", 403)

        try:
            new_user = User(
                username=username,
                password=password,
            )
            db.session.add(new_user)
            db.session.flush()

            user_info = user_schema.dump(new_user)
            db.session.commit()

            access_token = create_access_token(identity=new_user.id)

            resp = message(True, "User has been registered.")
            resp["access_token"] = access_token
            resp["user"] = user_info

            return resp, 201

        except Exception as error:
            current_app.logger.error(error)
            return internal_err_resp()
コード例 #2
0
 def get(self, request):
     code = request.GET.get("code")
     state = request.GET.get("state")
     user = User.from_osf_login(code)
     user.save()
     login(request, user)
     return redirect(reverse("schema_editor"))
コード例 #3
0
ファイル: __init__.py プロジェクト: d2emon/w2w
def after_login(resp):
    if resp.email is None or resp.email == "":
        flash(gettext("Invalid login. Please try again."))
        return redirect(url_for('login'))
    user = User.query.filter_by(email=resp.email).first()
    if user is None:
        nickname = resp.nickname
        if nickname is None or nickname == "":
            nickname = resp.email.split('@')[0]
        nickname = User.make_valid_nickname(nickname)
        nickname = User.make_unique_nickname(nickname)
        user = User(nickname=nickname, email=resp.email, role=ROLE_USER)

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

        db.session.add(user.follow(user))
        db.session.commit()
    remember_me = False
    if 'remember_me' in session:
        remember_me = session['remember_me']
        session.pop('remember_me', None)
    login_user(user, remember=remember_me)
    return redirect(request.args.get('next') or url_for('index'))
コード例 #4
0
    def validate(self):
        if not FlaskForm.validate(self):
            return False

        if self.nickname.data == self.original_nickname:
            return True

        if self.nickname.data != User.make_valid_nickname(self.nickname.data):
            self.nickname.errors.append(gettext("This nickname has invalid characters. Please use letters, numbers and underscores only."))
            return False

        user = User.query.filter_by(nickname=self.nickname.data).first()
        if user is not None:
            self.nickname.errors.append(gettext("This nickname is allready in use. Please choose another one."))
            return False
        return True
コード例 #5
0
ファイル: follow.py プロジェクト: d2emon/w2w
def unfollow(nickname):
    user = User.by_username(nickname)
    if user is None:
        flash(gettext('User %(nickname)s not found.', nickname=nickname))
        return redirect(url_for('index'))

    if user == g.user:
        flash(gettext("Cannot unfollow yourself!"))
        return redirect(url_for('user', nickname=nickname))

    u = g.user.unfollow(user)
    if u is None:
        flash(gettext('Cannot unfollow %(nickname)s!', nickname=nickname))
        return redirect(url_for('user', nickname=nickname))

    db.session.add(u)
    db.session.commit()
    flash(gettext('You have stopped following %(nickname)s!', nickname=nickname))
    return redirect(url_for('user', nickname=nickname))
コード例 #6
0
ファイル: follow.py プロジェクト: d2emon/w2w
def follow(nickname):
    user = User.by_username(nickname)
    if user is None:
        flash(gettext('User %(nickname)s not found.', nickname=nickname))
        return redirect(url_for('index'))

    if user == g.user:
        flash(gettext("Cannot follow yourself!"))
        return redirect(url_for('user', nickname=nickname))

    u = g.user.follow(user)
    if u is None:
        flash(gettext('Cannot follow %(nickname)s!', nickname=nickname))
        return redirect(url_for('user', nickname=nickname))

    db.session.add(u)
    db.session.commit()
    flash(gettext('You are now following %(nickname)s!', nickname=nickname))
    follower_notification(user, g.user)
    return redirect(url_for('user', nickname=nickname))
コード例 #7
0
def register():

    if current_user.is_authenticated:
        return redirect(url_for('main.home'))

    else:
        form = RegistrationForm()
        if form.validate_on_submit():
            user = User.query.filter_by(email=form.email.data).first(
            )  # if this returns a user, then the email already exists in database
            if user:
                flash('Email address already exists', 'danger')
                return render_template('register.html', form=form)

            hashed_password = bcrypt.generate_password_hash(
                form.password.data).decode('utf-8')
            user = User(email=form.email.data, password=hashed_password)
            db.session.add(user)
            db.session.commit()
            flash('Your account has been created! You are now able to log in',
                  'success')
            return redirect(url_for('auth.login'))

        return render_template('register.html', form=form)
コード例 #8
0
def create_user(user_id=None):
    name = request.values.get('name')
    cname = request.values.get('cname')
    email = request.values.get('email')
    mobile = request.values.get('mobile')
    company = request.values.get('company')
    password = request.values.get('password')
    repassword = request.values.get('repassword')
    department = request.values.get('department')
    role_name = request.values.get('user_role')
    status = True if request.values.get('user_status') else False
    groups = request.values.get('groups')

    if request.method == 'POST':
        user = db.session.query(User).filter(User.name == name).first()
        if user:
            return jsonify(dict(status=False, desc='账号已存在'))
        if password != repassword:
            return jsonify(dict(status=False, desc='两次输入密码不一致'))
        try:
            user = User(name,
                        cname,
                        email,
                        mobile,
                        department,
                        company,
                        rid=2,
                        groups=groups)
            db.session.add(user)
            db.session.commit()
            # 设置密码及scan_key
            user.password = user.gen_password_hash(password)
            user.scan_key = user.generate_auth_uuid()
            db.session.add(user)
            db.session.commit()
        except Exception as e:
            logger.error(e)
            return jsonify(dict(status=False, desc='添加失败'))
        else:
            return jsonify(dict(status=True, desc='添加成功'))
    else:

        user = db.session.query(User).filter(User.id == user_id).first()
        if not user:
            return jsonify(dict(status=False, desc='账号不存在'))

        try:
            # user.name = name
            user.cname = cname
            user.email = email
            user.mobile = mobile
            user.company = company
            user.department = department
            role = db.session.query(Role).filter(
                Role.cname == role_name).first()
            user.rid = role.id
            user.status = status
            user.groups = groups
            db.session.add(user)
            db.session.commit()
        except Exception as e:
            logger.error(e)
            return jsonify(dict(status=False, desc='更新失败'))
        else:
            return jsonify(dict(status=True, desc='更新成功'))
コード例 #9
0
def seed():
    try:
        transformer = Transformer(
            transformer_id=1,
            feeder='feeder_one',
            capacity=1,
        )
        db.session.add(transformer)
        db.session.commit()
    except exc.IntegrityError:
        db.session.rollback()
        print('transformer already exists')

    try:
        addr = Address(address_id=1,
                       address='asdfasdf',
                       city='RWC',
                       country='USA',
                       postal_code='94065')
        db.session.add(addr)
        db.session.commit()
    except exc.IntegrityError:
        db.session.rollback()
        print('address already exists')

    try:
        sl = ServiceLocation(service_location_id=1,
                             alternate_service_location_id='1234asdf',
                             address_id=1,
                             map_location='RWC',
                             is_active=True,
                             is_archived=False)
        db.session.add(sl)
        db.session.commit()
    except exc.IntegrityError:
        db.session.rollback()
        print('service location already exists')

    try:
        market = Market(market_id=1,
                        source='asdf',
                        ts=1.0,
                        p_max=1.0,
                        is_active=True,
                        is_archived=False)
        db.session.add(market)
        db.session.commit()
    except exc.IntegrityError:
        db.session.rollback()
        print('market already exists')

    try:
        hh = HomeHub(home_hub_id=1,
                     service_location_id=1,
                     market_id=1,
                     is_active=True,
                     is_archived=False)
        db.session.add(hh)
        db.session.commit()
    except exc.IntegrityError:
        db.session.rollback()
        print('home hub already exists')

    try:
        util = Utility(utility_id=1,
                       name='utilName',
                       subscription_start=datetime.datetime.now(),
                       subscription_end=datetime.datetime.now())
        db.session.add(util)
        db.session.commit()
    except exc.IntegrityError:
        db.session.rollback()
        print('utility already exists')

    try:
        meter = Meter(meter_id=1,
                      utility_id=1,
                      service_location_id=1,
                      home_hub_id=1,
                      transformer_id=1,
                      alternate_meter_id=1,
                      feeder='asdf',
                      substation='asdf',
                      meter_type=MeterType.COMMERCIAL,
                      is_active=True,
                      is_archived=False)
        db.session.add(meter)
        db.session.commit()
    except exc.IntegrityError:
        db.session.rollback()
        print('meter already exists')

    try:
        rate = Rate(rate_id=1, description='rate one')
        db.session.add(rate)
        db.session.commit()
    except exc.IntegrityError:
        db.session.rollback()
        print('rate already exists')

    try:
        mi = MeterInterval(
            meter_interval_id=1,
            meter_id=1,
            rate_id=1,
            start_time=datetime.datetime.fromisoformat('2020-01-01T00:05:00'),
            end_time=datetime.datetime.fromisoformat('2020-01-01T00:05:23'),
            e=1.0,
            qmtp=1.0,
            p_bid=1.0,
            q_bid=1.0,
            mode=0,
            is_bid=True)
        db.session.add(mi)
        db.session.commit()
    except exc.IntegrityError:
        db.session.rollback()
        print('meter interval already exists')

    try:
        mai = MarketInterval(
            market_interval_id=1,
            market_id=1,
            p_exp=1.0,
            p_dev=1.0,
            p_clear=1.0,
            q_clear=1.0,
            alpha=1.0,
            start_time=datetime.datetime.fromisoformat('2020-01-01T00:05:00'),
            end_time=datetime.datetime.fromisoformat('2020-01-01T00:05:23'))
        db.session.add(mai)
        db.session.commit()
    except exc.IntegrityError:
        db.session.rollback()
        print('market interval already exists')

    try:
        hceb = HceBids(
            bid_id=1,
            start_time=datetime.datetime.fromisoformat('2020-01-01T00:05:00'),
            end_time=datetime.datetime.fromisoformat('2020-01-01T00:05:23'),
            comment='asdfasdf',
            market_id=1)
        db.session.add(hceb)
        db.session.commit()
    except exc.IntegrityError:
        db.session.rollback()
        print('hce bids already exists')

    try:
        tri = TransformerInterval(
            transformer_interval_id=1,
            transformer_id=1,
            import_capacity=1.0,
            export_capacity=1.0,
            q=1.0,
            start_time=datetime.datetime.fromisoformat('2020-01-01T00:05:00'),
            end_time=datetime.datetime.fromisoformat('2020-01-01T00:05:23'))
        db.session.add(tri)
        db.session.commit()
    except exc.IntegrityError:
        db.session.rollback()
        print('transformer interval already exists')

    try:
        at = AlertType(
            alert_type_id=1,
            utility_id=1,
            name='YELLOW_ALARM_LOAD',
            limit=1.0,
            updated_at=datetime.datetime.fromisoformat('2020-01-01T00:05:00'),
            created_at=datetime.datetime.fromisoformat('2020-01-01T00:05:23'))
        db.session.add(at)
        db.session.commit()
    except exc.IntegrityError:
        db.session.rollback()
        print('alert type already exists')

    try:
        at = AlertType(
            alert_type_id=2,
            utility_id=1,
            name='RED_ALARM_LOAD',
            limit=3.0,
            updated_at=datetime.datetime.fromisoformat('2020-01-01T00:05:32'),
            created_at=datetime.datetime.fromisoformat('2020-01-01T00:05:35'))
        db.session.add(at)
        db.session.commit()
    except exc.IntegrityError:
        db.session.rollback()
        print('alert type already exists')

    try:
        at = AlertType(
            alert_type_id=3,
            utility_id=1,
            name='YELLOW_ALARM_PRICE',
            limit=3.0,
            updated_at=datetime.datetime.fromisoformat('2020-01-01T00:05:00'),
            created_at=datetime.datetime.fromisoformat('2020-01-01T00:05:23'))
        db.session.add(at)
        db.session.commit()
    except exc.IntegrityError:
        db.session.rollback()
        print('alert type already exists')

    try:
        at = AlertType(
            alert_type_id=4,
            utility_id=1,
            name='RED_ALARM_PRICE',
            limit=3.0,
            updated_at=datetime.datetime.fromisoformat('2020-01-01T00:05:00'),
            created_at=datetime.datetime.fromisoformat('2020-01-01T00:05:23'))
        db.session.add(at)
        db.session.commit()
    except exc.IntegrityError:
        db.session.rollback()
        print('alert type already exists')

    try:
        at = AlertType(
            alert_type_id=5,
            utility_id=1,
            name='PRICE_ALERT',
            limit=3.0,
            updated_at=datetime.datetime.fromisoformat('2020-01-01T00:05:00'),
            created_at=datetime.datetime.fromisoformat('2020-01-01T00:05:23'))
        db.session.add(at)
        db.session.commit()
    except exc.IntegrityError:
        db.session.rollback()
        print('alert type already exists')

    try:
        at = AlertType(
            alert_type_id=6,
            utility_id=1,
            name='IMPORT_CAPACITY',
            limit=3.0,
            updated_at=datetime.datetime.fromisoformat('2020-01-01T00:05:00'),
            created_at=datetime.datetime.fromisoformat('2020-01-01T00:05:23'))
        db.session.add(at)
        db.session.commit()
    except exc.IntegrityError:
        db.session.rollback()
        print('alert type already exists')

    try:
        at = AlertType(
            alert_type_id=7,
            utility_id=1,
            name='EXPORT_CAPACITY',
            limit=3.0,
            updated_at=datetime.datetime.fromisoformat('2020-01-01T00:05:00'),
            created_at=datetime.datetime.fromisoformat('2020-01-01T00:05:23'))
        db.session.add(at)
        db.session.commit()
    except exc.IntegrityError:
        db.session.rollback()
        print('alert type already exists')

    try:
        at = AlertType(
            alert_type_id=8,
            utility_id=1,
            name='RESOURCE_DEPLETION',
            limit=3.0,
            updated_at=datetime.datetime.fromisoformat('2020-01-01T00:05:00'),
            created_at=datetime.datetime.fromisoformat('2020-01-01T00:05:23'))
        db.session.add(at)
        db.session.commit()
    except exc.IntegrityError:
        db.session.rollback()
        print('alert type already exists')

    try:
        at = AlertType(
            alert_type_id=9,
            utility_id=1,
            name='TELECOMM_ALERT',
            limit=3.0,
            updated_at=datetime.datetime.fromisoformat('2020-01-01T00:05:00'),
            created_at=datetime.datetime.fromisoformat('2020-01-01T00:05:23'))
        db.session.add(at)
        db.session.commit()
    except exc.IntegrityError:
        db.session.rollback()
        print('alert type already exists')

    try:
        at = AlertType(
            alert_type_id=10,
            utility_id=1,
            name='PEAK_EVENT',
            limit=3.0,
            updated_at=datetime.datetime.fromisoformat('2020-01-01T00:05:00'),
            created_at=datetime.datetime.fromisoformat('2020-01-01T00:05:23'))
        db.session.add(at)
        db.session.commit()
    except exc.IntegrityError:
        db.session.rollback()
        print('alert type already exists')

    try:
        user = User(
            id=1,
            email='*****@*****.**',
            email_confirmed_at=datetime.datetime.fromisoformat(
                '2020-01-01T00:05:00'),
            first_name='fname',
            last_name='lname',
            address_id=1,
            utility_id=1,
            updated_at=datetime.datetime.fromisoformat('2020-01-01T00:05:00'),
            created_at=datetime.datetime.fromisoformat('2020-01-01T00:05:23'))
        db.session.add(user)
        db.session.commit()
    except exc.IntegrityError:
        db.session.rollback()
        print('user already exists')

    try:
        alert = Alert(alert_id=1,
                      alert_type_id=1,
                      assigned_to='*****@*****.**',
                      description='sdjfd',
                      status='open',
                      context='feeder',
                      context_id='1',
                      resolution='fdjgh')
        db.session.add(alert)
        db.session.commit()
    except exc.IntegrityError:
        db.session.rollback()
        print('alert already exists')

    try:
        alert = Alert(alert_id=2,
                      alert_type_id=1,
                      assigned_to='*****@*****.**',
                      description='sdjfd',
                      status='open',
                      context='feeder',
                      context_id='1',
                      resolution='fdjgh')
        db.session.add(alert)
        db.session.commit()
    except exc.IntegrityError:
        db.session.rollback()
        print('alert already exists')

    try:
        notification = Notification(
            notification_id=1,
            alert_type_id=1,
            email="*****@*****.**",
            is_active=False,
            created_by=1,
            updated_at=datetime.datetime.fromisoformat('2020-01-01T00:05:00'),
            created_at=datetime.datetime.fromisoformat('2020-01-01T00:05:23'))
        db.session.add(notification)
        db.session.commit()
    except exc.IntegrityError:
        db.session.rollback()
        print('notification already exists')

    try:
        notification = Notification(
            notification_id=2,
            alert_type_id=1,
            email="*****@*****.**",
            is_active=False,
            created_by=1,
            updated_at=datetime.datetime.fromisoformat('2020-01-01T00:05:00'),
            created_at=datetime.datetime.fromisoformat('2020-01-01T00:05:23'))
        db.session.add(notification)
        db.session.commit()
    except exc.IntegrityError:
        db.session.rollback()
        print('notification already exists')