Exemplo n.º 1
0
def empty_theme_group(user4):
    theme_group = ThemeGroup(
        user_id=user4.id,
        sort_order=3,
        is_active=True,
        is_promoted=True,
        title='empty theme group',
        description='empty theme group desc',
    )
    db.session.add(theme_group)
    user4.is_creator = True
    db.session.commit()
    populate_automatic_username(user4)

    yield theme_group
Exemplo n.º 2
0
def theme_group_1(user):
    theme_group = ThemeGroup(
        user_id=user.id,
        sort_order=10,
        is_active=False,
        is_promoted=False,
        title='theme group 1',
        description='theme group desc 1',
    )
    db.session.add(theme_group)
    user.is_creator = True
    db.session.commit()
    populate_automatic_username(user)

    yield theme_group
Exemplo n.º 3
0
 def mutate_and_get_payload(cls, root, info, **inp):
     for user in UserModel.query.all():
         populate_automatic_username(user)
     return PopulateAutomaticUsernames()
Exemplo n.º 4
0
    def mutate_and_get_payload(cls, root, info, identity, **inp):
        logging.info('saveUser mutation input: %s', inp)

        try:
            global_id = inp['id']
        except KeyError:
            user = get_or_create_user(identity.id.subject)
            is_user_new = not user.registration_finished
        else:
            user_id = from_global_id_assert_type(global_id, 'User')
            user = get_user(user_id)
            is_user_new = False

        if user.cognito_sub == identity.id.subject:
            is_admin_user = admin_user_permission.can()
        else:
            admin_user_permission.test(http_exception=401)
            is_admin_user = True

        old_firstname = user.first_name or ''
        old_lastname = user.last_name or ''
        old_username = user.username or ''
        save_input_fields(
            inp,
            (
                'first_name',
                'last_name',
                'username',
                'avatar',
            ),
            user
        )
        new_firstname = user.first_name or ''
        new_lastname = user.last_name or ''
        new_username = user.username or ''

        if new_username != old_username:
            # TODO otereshchenko: make sure invalid characters are escaped in manual usernames as well
            user.username_set_manually = True

        if new_firstname != old_firstname or new_lastname != old_lastname:
            populate_automatic_username(user)

        # Save address
        save_input_subfields(
            inp,
            'user_address',
            (
                'country',
                'state_region',
                'city',
                'street1',
                'street2',
                'postcode',
            ),
            user,
            lambda: UserAddressModel(user_id=user.id),
            model_attr='primary_user_address'
        )

        # Save subscription
        subscription_inp = inp.get('user_subscription')
        if subscription_inp is not None:
            subscription_inp.produce_budget_decimal()
            subscription = save_subscription(user.id, subscription_inp)
            user.primary_user_subscription_id = subscription.id

        # Save device_token
        device_token_inp = inp.get('device_token')
        if device_token_inp is not None:
            save_device_token(user.id, device_token_inp)

        # Save Stripe customer id
        stripe_token = inp.get('stripe_token')
        if stripe_token is not None:
            save_stripe_customer_id(user.id, stripe_token)

        save_user_cards(inp, user)

        if is_admin_user:
            cognito_user_sync = CognitoUserSync(username=user.cognito_sub)
        else:
            cognito_user_sync = CognitoUserSync(access_token=identity.id.encoded_token)

        cognito_updated = False
        if is_admin_user or not is_user_new:
            cognito_updated = save_cognito_user(cognito_user_sync, inp)

        if cognito_updated or is_user_new:
            try:
                update_user_from_cognito(
                    cognito_user_sync=cognito_user_sync, user=user, clear_missing_data=False
                )
            except:
                logging.exception(
                    'Failed to synchronize a user with id=%s from Cognito to DB!', user.id
                )

        if is_user_new and user.first_name:
            user.registration_finished = True

        db.session.commit()

        return SaveUser(user=user)