예제 #1
0
def createsuperuser():
    """
    Create a super user of the system, requiring Email and password.
    """
    username = prompt('Username')
    password = prompt('Password')
    password_confirm = prompt('Password Again')
    if not password == password_confirm:
        sys.exit('/n Could not create superuser')
    Account.create(
        username=username,
        password=password,
        active=True,
        is_admin=True
    )
예제 #2
0
    def find_by_token(cls, token):
        session = None
        token_key = cls._get_session_token_key(token)
        if token_key:
            account_id = redis.get(token_key)

            if account_id:
                session = Session(Account.first(id=account_id))

        return session
예제 #3
0
    def post(self):
        result = {"status": False, 'msg': 'Failed to create account!'}
        status_code = 400

        if 'business_name' in request.json.keys():

            user = request.user
            # create account
            business_name = request.json.get("business_name")
            accounts = Account.query.filter_by(business_name=business_name)

            if not accounts.count() and user:
                account = Account(business_name=business_name,
                                  creator_id=user.id)
                db.session.add(account)
                db.session.commit()

                # create location
                location_name = request.json.get('location_name')
                contact_number = request.json.get('contact_number')
                contact_email = request.json.get('contact_email')
                street_address = request.json.get('street_address')
                apt_suite_building = request.json.get('apt_suite_building')
                city = request.json.get('city')
                state = request.json.get('state')
                zip_code = request.json.get('zip_code')

                # create location
                location = Location(location_name=location_name,
                                    contact_number=contact_number,
                                    contact_email=contact_email,
                                    street_address=street_address,
                                    apt_suite_building=apt_suite_building,
                                    city=city,
                                    state=state,
                                    zip_code=zip_code,
                                    account_id=account.id,
                                    creator_id=user.id)
                db.session.add(location)
                db.session.commit()

                status_code = 200
                result = {
                    "status": True,
                    'msg': 'Successfully create account.'
                }

        return Response(json.dumps(result),
                        status=status_code,
                        mimetype='application/json')
def lost_password_send():
    form = LostPasswordForm(request.form)
    if not form.validate():
        return form.errors_as_json()

    account = Account.first(email=form.email.data, deleted=None)
    if account:
        session = Session(account)
        session.save()
        try:
            session.send()
        except:
            form.errors['email'] = ['An error occurred, please try again.']
            return make_response(form.errors_as_json(), 400)

    return make_response('', 204)
예제 #5
0
    def controller_create_new_account(cls, request):
        result = {"status": False, 'msg': 'Failed to create account!'}
        status_code = 400

        if request.method == 'POST' and 'business_name' in request.json.keys(
        ) and 'token' in request.json.keys():

            # get logined user
            user = User.query.filter_by(
                token=request.json.get("token")).first()

            # create account
            business_name = request.json.get("business_name")
            account = Account(business_name=business_name, creator_id=user.id)
            db.session.add(account)
            db.session.commit()

            # create location
            location_name = request.json.get('location_name')
            contact_number = request.json.get('contact_number')
            contact_email = request.json.get('contact_email')
            street_address = request.json.get('street_address')
            apt_suite_building = request.json.get('apt_suite_building')
            city = request.json.get('city')
            state = request.json.get('state')
            zip_code = request.json.get('zip_code')

            # create location
            location = Location(location_name=location_name,
                                contact_number=contact_number,
                                contact_email=contact_email,
                                street_address=street_address,
                                apt_suite_building=apt_suite_building,
                                city=city,
                                state=state,
                                zip_code=zip_code,
                                account_id=account.id,
                                creator_id=user.id)
            db.session.add(location)
            db.session.commit()

            status_code = 200
            result = {"status": True, 'msg': 'Successfully create account.'}

        return result, status_code
def login():
    form = AuthenticationForm(request.form)
    if not form.validate():
        return form.errors_as_json()

    account = Account.first(email=form.email.data, deleted=None)
    if not account or not account.check_passwd(form.password.data):
        form.errors['email'] = ['Invalid password or account does not exist.']
        return make_response(form.errors_as_json(), 400)

    if request.form.get('api', '0') == '1':
        return jsonify(account.to_fulljson())

    session = Session(account)
    session.save()

    result = session.to_json()
    result['account'] = account.get_session_data()
    return jsonify(result)
예제 #7
0
def process(account_id):

    account = db.engine.execute(text(
        'SELECT organization_id, email FROM accounts WHERE id = :account LIMIT 1'
    ),
                                account=account_id).first()
    if account is None:
        return None

    organization_id = account[0]
    account_email = account[1]

    frequests.delete(
        'https://api.sendinblue.com/v2.0/user/{0}'.format(account_email),
        headers={'api-key': Settings.get('SENDINBLUE_API_KEY')})

    member = db.engine.execute(text(
        "SELECT id FROM accounts WHERE is_admin = 1 AND organization_id = :organization AND id != :account LIMIT 1"
    ),
                               organization=organization_id,
                               account=account_id).first()
    single_member = True if member is None else False

    account_entity = Account.first(email=account_email)
    if account_entity.admin and single_member:
        account_entity.organization.cancel_plan()

    db.engine.execute(
        text('DELETE FROM api_tokens WHERE account_id = :account'),
        account=account_id)
    db.engine.execute(text('DELETE FROM sessions WHERE account_id = :account'),
                      account=account_id)
    db.engine.execute(
        text('DELETE FROM account_emails_verification WHERE email = :email'),
        email=account_email)

    if single_member:
        db.engine.execute(text(
            'DELETE FROM contact_has_lists WHERE contact_id IN (SELECT id FROM contacts WHERE owner_id = :account)'
        ),
                          account=account_id)
        db.engine.execute(text(
            'DELETE FROM contact_histories WHERE contact_id IN (SELECT id FROM contacts WHERE owner_id = :account)'
        ),
                          account=account_id)
        db.engine.execute(text(
            'DELETE FROM contact_messages WHERE contact_id IN (SELECT id FROM contacts WHERE owner_id = :account)'
        ),
                          account=account_id)

        try:
            delete_boto(
                "SELECT filepath FROM massive_imports WHERE account_id = " +
                str(account_id))
        except:
            pass
        db.engine.execute(
            text('DELETE FROM massive_imports WHERE account_id = :account'),
            account=account_id)

        delete_boto(
            "SELECT filepath FROM message_attachments WHERE message_id IN (SELECT id FROM messages WHERE organization_id = "
            + str(organization_id) + ")")
        db.engine.execute(text(
            'DELETE FROM message_attachments WHERE message_id IN (SELECT id FROM messages WHERE organization_id = :organization)'
        ),
                          organization=organization_id)
        db.engine.execute(text(
            'DELETE FROM message_events WHERE message_id IN (SELECT id FROM messages WHERE organization_id = :organization)'
        ),
                          organization=organization_id)
        db.engine.execute(
            text('DELETE FROM messages WHERE organization_id = :organization'),
            organization=organization_id)

        db.engine.execute(text(
            'DELETE FROM organization_emails WHERE organization_id = :organization'
        ),
                          organization=organization_id)
        db.engine.execute(
            text('DELETE FROM credits WHERE organization_id = :organization'),
            organization=organization_id)
        db.engine.execute(
            text('DELETE FROM lists WHERE organization_id = :organization'),
            organization=organization_id)

        db.engine.execute(
            text('DELETE FROM contacts WHERE owner_id = :account'),
            account=account_id)
        db.engine.execute(text(
            'UPDATE organizations SET plan_id = NULL, next_payment = NULL, cc_expires = NULL, is_yearly = 0 WHERE id = :organization LIMIT 1'
        ),
                          organization=organization_id)
        # On ne supprime pas les payments, l'organizations et le compte
    else:
        new_account = long(member[0])

        db.engine.execute(
            text('DELETE FROM contact_histories WHERE owner_id = :account'),
            account=account_id)
        db.engine.execute(text(
            'DELETE FROM contact_messages WHERE contact_id IN (SELECT id FROM contacts WHERE owner_id = :account)'
        ),
                          account=account_id)
        db.engine.execute(text(
            'UPDATE contacts SET owner_id = :newowner WHERE owner_id = :account'
        ),
                          account=account_id,
                          newowner=new_account)

    db.engine.execute(text(
        'UPDATE accounts SET password = NULL, removed = UTC_DATE(), is_newsletter = 0 WHERE id = :account LIMIT 1'
    ),
                      account=account_id)

    return True
def process(account_id):
    account = Account.first(id=int(account_id))
    verification = AccountEmailVerification(account)
    verification.send()
예제 #9
0
파일: forms.py 프로젝트: ljshj/wei3h
 def save(self, role=None):
     user = Account(**self.data)
     if role:
         user.role = role
     user.save()
     return user