예제 #1
0
파일: donate.py 프로젝트: pheezer/malt.io
    def post(self):
        """
        Process a donation payment given a Stripe single-use token.
        """
        user = self.user
        stripe.api_key = settings.STRIPE_PRIVATE_KEY

        amount = int(cgi.escape(self.request.get('amount')))
        token = cgi.escape(self.request.get('stripeToken'))

        # Charge the amount (in cents) to the customer's card
        charge = stripe.Charge.create(**{
            'amount': amount * 100,
            'currency': 'usd',
            'card': token,
            'description': 'Donation from ' + (user and user.name or 'Anonymous')
        })

        if user:
            # Add an award to this user for donating
            if user and 'donated' not in user.awards:
                user.awards.append('donated')
                user.put()

            # Log action
            action = UserAction()
            action.owner = user
            action.type = action.TYPE_USER_DONATED
            action.put()

        self.redirect('/donate?success=true')
예제 #2
0
    def process(self, username, action):
        """
        Follow the given user.
        """
        user = self.user
        publicuser = UserPrefs.all().filter('name =', username).get()

        if not user or not publicuser:
            return self.render_json({
                'status': 'error',
                'error': 'User not found'
            })

        if action == 'post':
            if publicuser.user_id in user.following:
                return self.render_json({
                    'status': 'error',
                    'error': 'Already following user'
                })

            user.following.append(publicuser.user_id)

            existing = UserAction.all()\
                                 .filter('owner =', user)\
                                 .filter('type =', UserAction.TYPE_USER_FOLLOWED)\
                                 .filter('object_id =', publicuser.key().id())\
                                 .count()

            if not existing:
                user_action = UserAction()
                user_action.owner = user
                user_action.type = user_action.TYPE_USER_FOLLOWED
                user_action.object_id = publicuser.key().id()
                user_action.put()
        else:
            if publicuser.user_id not in user.following:
                return self.render_json({
                    'status': 'error',
                    'error': 'User not being followed'
                })

            user.following.remove(publicuser.user_id)

            existing = UserAction.all()\
                                 .filter('owner =', user)\
                                 .filter('type =', UserAction.TYPE_USER_FOLLOWED)\
                                 .filter('object_id =', publicuser.key().id())\
                                 .get()

            if existing:
                existing.delete()

        # Save updated following list
        user.put()

        self.render_json({
            'status': 'ok'
        })
예제 #3
0
파일: users.py 프로젝트: jensyt/malt.io
    def process(self, username, action):
        """
        Follow the given user.
        """
        user = UserPrefs.get()
        publicuser = UserPrefs.all().filter('name =', username).get()

        if not user or not publicuser:
            return self.render_json({
                'status': 'error',
                'error': 'User not found'
            })

        if action == 'post':
            if publicuser.user_id in user.following:
                return self.render_json({
                    'status': 'error',
                    'error': 'Already following user'
                })

            user.following.append(publicuser.user_id)

            existing = UserAction.all()\
                                 .filter('owner =', user)\
                                 .filter('type =', UserAction.TYPE_USER_FOLLOWED)\
                                 .filter('object_id =', publicuser.key().id())\
                                 .count()

            if not existing:
                user_action = UserAction()
                user_action.owner = user
                user_action.type = user_action.TYPE_USER_FOLLOWED
                user_action.object_id = publicuser.key().id()
                user_action.put()
        else:
            if publicuser.user_id not in user.following:
                return self.render_json({
                    'status': 'error',
                    'error': 'User not being followed'
                })

            user.following.remove(publicuser.user_id)

            existing = UserAction.all()\
                                 .filter('owner =', user)\
                                 .filter('type =', UserAction.TYPE_USER_FOLLOWED)\
                                 .filter('object_id =', publicuser.key().id())\
                                 .get()

            if existing:
                existing.delete()

        # Save updated following list
        user.put()

        self.render_json({
            'status': 'ok'
        })
예제 #4
0
    def process(self, action, username=None, recipe_slug=None):
        """
        Process a request to add or remove a user from the liked list.
        """
        user = UserPrefs.get()
        publicuser = UserPrefs.all()\
                              .filter('name = ', username)\
                              .get()

        if not publicuser:
            self.render_json({'status': 'error', 'error': 'User not found'})
            return

        recipe = Recipe.all()\
                       .filter('owner =', publicuser)\
                       .filter('slug =', recipe_slug)\
                       .get()

        if not recipe:
            self.render_json({'status': 'error', 'error': 'Recipe not found'})
            return

        if action == 'post':
            if user.user_id not in recipe.likes:
                recipe.likes.append(user.user_id)
                recipe.put()

                existing = UserAction.all()\
                                     .filter('owner =', user)\
                                     .filter('type =', UserAction.TYPE_RECIPE_LIKED)\
                                     .filter('object_id =', recipe.key().id())\
                                     .count()

                if not existing:
                    user_action = UserAction()
                    user_action.owner = user
                    user_action.type = user_action.TYPE_RECIPE_LIKED
                    user_action.object_id = recipe.key().id()
                    user_action.put()

        elif action == 'delete':
            if user.user_id in recipe.likes:
                recipe.likes.remove(user.user_id)
                recipe.put()

                existing = UserAction.all()\
                                     .filter('owner =', user)\
                                     .filter('type =', UserAction.TYPE_RECIPE_LIKED)\
                                     .filter('object_id =', recipe.key().id())\
                                     .get()

                if existing:
                    existing.delete()

        return self.render_json({'status': 'ok', 'likes': len(recipe.likes)})
예제 #5
0
파일: recipes.py 프로젝트: jensyt/malt.io
    def post(self, username=None, recipe_slug=None):
        publicuser = UserPrefs.all()\
                              .filter('name = ', username)\
                              .get()

        if not publicuser:
            self.render_json({
                'status': 'error',
                'error': 'User not found'
            })
            return

        recipe = Recipe.all()\
                       .filter('owner =', publicuser)\
                       .filter('slug =', recipe_slug)\
                       .get()

        if not recipe:
            self.render_json({
                'status': 'error',
                'error': 'Recipe not found'
            })
            return

        new_recipe = Recipe(**{
            'owner': UserPrefs.get(),
            'cloned_from': recipe,
            'color': recipe.color,
            'ibu': recipe.ibu,
            'alcohol': recipe.alcohol,
            'name': recipe.name,
            'description': recipe.description,
            'type': recipe.type,
            'style': recipe.style,
            'batch_size': recipe.batch_size,
            'boil_size': recipe.boil_size,
            'bottling_temp': recipe.bottling_temp,
            'bottling_pressure': recipe.bottling_pressure,
            '_ingredients': recipe._ingredients
        })

        new_recipe.slug = generate_usable_slug(new_recipe)
        new_recipe.put()

        action = UserAction()
        action.owner = UserPrefs.get()
        action.type = action.TYPE_RECIPE_CLONED
        action.object_id = new_recipe.key().id()
        action.put()

        return self.render_json({
            'status': 'ok',
            'redirect': new_recipe.url
        })
예제 #6
0
    def post(self, username=None, recipe_slug=None):
        publicuser = UserPrefs.all()\
                              .filter('name = ', username)\
                              .get()

        if not publicuser:
            self.render_json({'status': 'error', 'error': 'User not found'})
            return

        recipe = Recipe.all()\
                       .filter('owner =', publicuser)\
                       .filter('slug =', recipe_slug)\
                       .get()

        if not recipe:
            self.render_json({'status': 'error', 'error': 'Recipe not found'})
            return

        new_recipe = Recipe(
            **{
                'owner': UserPrefs.get(),
                'cloned_from': recipe,
                'color': recipe.color,
                'ibu': recipe.ibu,
                'alcohol': recipe.alcohol,
                'name': recipe.name,
                'description': recipe.description,
                'type': recipe.type,
                'style': recipe.style,
                'batch_size': recipe.batch_size,
                'boil_size': recipe.boil_size,
                'bottling_temp': recipe.bottling_temp,
                'bottling_pressure': recipe.bottling_pressure,
                '_ingredients': recipe._ingredients
            })

        new_recipe.slug = generate_usable_slug(new_recipe)
        new_recipe.put()

        action = UserAction()
        action.owner = UserPrefs.get()
        action.type = action.TYPE_RECIPE_CLONED
        action.object_id = new_recipe.key().id()
        action.put()

        return self.render_json({'status': 'ok', 'redirect': new_recipe.url})
예제 #7
0
    def post(self):
        """
        Import a new recipe or list of recipes from BeerXML to the
        currently logged in user's account.
        """
        user = self.user
        recipesxml = self.request.POST['file'].value

        for recipe in Recipe.new_from_beerxml(recipesxml):
            recipe.owner = user
            recipe.slug = generate_usable_slug(recipe)
            recipe.update_cache();
            key = recipe.put()

            action = UserAction()
            action.owner = user
            action.object_id = key.id()
            action.type = action.TYPE_RECIPE_CREATED
            action.put()

        self.redirect('/users/' + user.name + '/recipes')
예제 #8
0
파일: recipes.py 프로젝트: pheezer/malt.io
    def post(self):
        """
        Import a new recipe or list of recipes from BeerXML to the
        currently logged in user's account.
        """
        user = self.user
        recipesxml = self.request.POST['file'].value

        for recipe in Recipe.new_from_beerxml(recipesxml):
            recipe.owner = user
            recipe.slug = generate_usable_slug(recipe)
            recipe.update_cache()
            key = recipe.put()

            action = UserAction()
            action.owner = user
            action.object_id = key.id()
            action.type = action.TYPE_RECIPE_CREATED
            action.put()

        self.redirect('/users/' + user.name + '/recipes')
예제 #9
0
    def create_or_update(auth_id, user_info, auth_info):
        """
        Create a new user or update an existing one with information such
        as id, name, email, avatar, etc.
        """
        prefs = UserPrefs.all()\
                         .filter('user_id =', auth_id)\
                         .get()

        # Backwards compatibility
        if not prefs:
            prefs = UserPrefs.all()\
                             .filter('email =', user_info['email'])\
                             .get()

            if prefs:
                if ':' not in prefs.user_id:
                    # Old-style user and the emails match, so update!
                    prefs.user_id = auth_id
                else:
                    # New-style user, ignore
                    prefs = None

        # Not found yet... time to create a new one!
        if not prefs:
            # Generate a nice username from the email
            username = user_info['email'].split('@')[0].lower().replace(' ', '')
            count = 0

            while True:
                check_name = count and username + str(count) or username
                if not UserPrefs.all()\
                                .filter('name =', check_name)\
                                .count() and check_name not in settings.RESERVED_USERNAMES:
                    if count:
                        username = username + str(count)
                    break

                count += 1

            # Create the preferences object and store it
            prefs = UserPrefs(**{
                'user_id': auth_id,
                'name': username,
                'joined': Date().date,
            })

            # Write to the db so this user object gets a proper id, which
            # is required to reference it in the action created below
            prefs.put()

            from models.useraction import UserAction

            action = UserAction()
            action.owner = prefs
            action.type = action.TYPE_USER_JOINED
            action.put()

            # Invalidate cached user list pages
            memcache.delete('users-content')

        # Update fields based on latest user info
        prefs.email = user_info['email']

        # Update the avatar, except for special users
        if prefs.name not in ['examples']:
            prefs.avatar = user_info['avatar']

        prefs.put()

        return prefs
예제 #10
0
    def post(self, username=None, recipe_slug=None):
        publicuser = UserPrefs.all()\
                              .filter('name = ', username)\
                              .get()

        if not publicuser:
            self.render_json({
                'status': 'error',
                'error': 'User not found'
            })
            return

        recipe = Recipe.all()\
                       .filter('owner =', publicuser)\
                       .filter('slug =', recipe_slug)\
                       .get()

        if not recipe:
            self.render_json({
                'status': 'error',
                'error': 'Recipe not found'
            })
            return

        new_recipe = Recipe(**{
            'owner': self.user,
            'cloned_from': recipe,
            'color': recipe.color,
            'ibu': recipe.ibu,
            'alcohol': recipe.alcohol,
            'name': recipe.name,
            'description': recipe.description,
            'type': recipe.type,
            'category': recipe.category,
            'style': recipe.style,
            'batch_size': recipe.batch_size,
            'boil_size': recipe.boil_size,
            'bottling_temp': recipe.bottling_temp,
            'bottling_pressure': recipe.bottling_pressure,
            'steep_efficiency': recipe.steep_efficiency,
            'mash_efficiency': recipe.mash_efficiency,
            'primary_days': recipe.primary_days,
            'primary_temp': recipe.primary_temp,
            'secondary_days': recipe.secondary_days,
            'secondary_temp': recipe.secondary_temp,
            'tertiary_days': recipe.tertiary_days,
            'tertiary_temp': recipe.tertiary_temp,
            'aging_days': recipe.aging_days,
            '_ingredients': recipe._ingredients
        })

        new_recipe.slug = generate_usable_slug(new_recipe)
        new_recipe.put()
        new_recipe.update_grade()
        new_recipe.put()

        # Update recipe ranking for sorting
        recipe.update_grade()
        recipe.put()

        action = UserAction()
        action.owner = self.user
        action.type = action.TYPE_RECIPE_CLONED
        action.object_id = new_recipe.key().id()
        action.put()

        return self.render_json({
            'status': 'ok',
            'redirect': new_recipe.url
        })
예제 #11
0
        # Update grade now that we have a key
        recipe.update_grade()
        recipe.put()

        if not historic or changed:
            action = UserAction()
            action.owner = user
            action.object_id = key.id()

            if not recipe_slug:
                action.type = action.TYPE_RECIPE_CREATED
            else:
                action.type = action.TYPE_RECIPE_EDITED

            action.put()

        self.render_json({
            'status': 'ok',
            'redirect': '/users/%(username)s/recipes/%(slug)s' % {
                'username': user.name,
                'slug': recipe.slug
            }
        })

    def delete(self, username=None, recipe_slug=None):
        """
        Handle recipe delete. This will remove a recipe and return success
        or failure.
        """
        user = self.user
예제 #12
0
파일: recipes.py 프로젝트: pheezer/malt.io
    def post(self, username=None, recipe_slug=None):
        publicuser = UserPrefs.all()\
                              .filter('name = ', username)\
                              .get()

        if not publicuser:
            self.render_json({'status': 'error', 'error': 'User not found'})
            return

        recipe = Recipe.all()\
                       .filter('owner =', publicuser)\
                       .filter('slug =', recipe_slug)\
                       .get()

        if not recipe:
            self.render_json({'status': 'error', 'error': 'Recipe not found'})
            return

        new_recipe = Recipe(
            **{
                'owner': self.user,
                'cloned_from': recipe,
                'color': recipe.color,
                'ibu': recipe.ibu,
                'alcohol': recipe.alcohol,
                'name': recipe.name,
                'description': recipe.description,
                'type': recipe.type,
                'category': recipe.category,
                'style': recipe.style,
                'batch_size': recipe.batch_size,
                'boil_size': recipe.boil_size,
                'bottling_temp': recipe.bottling_temp,
                'bottling_pressure': recipe.bottling_pressure,
                'steep_efficiency': recipe.steep_efficiency,
                'mash_efficiency': recipe.mash_efficiency,
                'primary_days': recipe.primary_days,
                'primary_temp': recipe.primary_temp,
                'secondary_days': recipe.secondary_days,
                'secondary_temp': recipe.secondary_temp,
                'tertiary_days': recipe.tertiary_days,
                'tertiary_temp': recipe.tertiary_temp,
                'aging_days': recipe.aging_days,
                '_ingredients': recipe._ingredients
            })

        new_recipe.slug = generate_usable_slug(new_recipe)
        new_recipe.put()
        new_recipe.update_grade()
        new_recipe.put()

        # Update recipe ranking for sorting
        recipe.update_grade()
        recipe.put()

        action = UserAction()
        action.owner = self.user
        action.type = action.TYPE_RECIPE_CLONED
        action.object_id = new_recipe.key().id()
        action.put()

        return self.render_json({'status': 'ok', 'redirect': new_recipe.url})
예제 #13
0
파일: recipes.py 프로젝트: pheezer/malt.io
        # Update grade now that we have a key
        recipe.update_grade()
        recipe.put()

        if not historic or changed:
            action = UserAction()
            action.owner = user
            action.object_id = key.id()

            if not recipe_slug:
                action.type = action.TYPE_RECIPE_CREATED
            else:
                action.type = action.TYPE_RECIPE_EDITED

            action.put()

        self.render_json({
            'status': 'ok',
            'redirect': '/users/%(username)s/recipes/%(slug)s' % {
                'username': user.name,
                'slug': recipe.slug
            }
        })

    def delete(self, username=None, recipe_slug=None):
        """
        Handle recipe delete. This will remove a recipe and return success
        or failure.
        """
        user = self.user
예제 #14
0
파일: recipes.py 프로젝트: jensyt/malt.io
    def process(self, action, username=None, recipe_slug=None):
        """
        Process a request to add or remove a user from the liked list.
        """
        user = UserPrefs.get()
        publicuser = UserPrefs.all()\
                              .filter('name = ', username)\
                              .get()

        if not publicuser:
            self.render_json({
                'status': 'error',
                'error': 'User not found'
            })
            return

        recipe = Recipe.all()\
                       .filter('owner =', publicuser)\
                       .filter('slug =', recipe_slug)\
                       .get()

        if not recipe:
            self.render_json({
                'status': 'error',
                'error': 'Recipe not found'
            })
            return

        if action == 'post':
            if user.user_id not in recipe.likes:
                recipe.likes.append(user.user_id)
                recipe.put()

                existing = UserAction.all()\
                                     .filter('owner =', user)\
                                     .filter('type =', UserAction.TYPE_RECIPE_LIKED)\
                                     .filter('object_id =', recipe.key().id())\
                                     .count()

                if not existing:
                    user_action = UserAction()
                    user_action.owner = user
                    user_action.type = user_action.TYPE_RECIPE_LIKED
                    user_action.object_id = recipe.key().id()
                    user_action.put()

        elif action == 'delete':
            if user.user_id in recipe.likes:
                recipe.likes.remove(user.user_id)
                recipe.put()

                existing = UserAction.all()\
                                     .filter('owner =', user)\
                                     .filter('type =', UserAction.TYPE_RECIPE_LIKED)\
                                     .filter('object_id =', recipe.key().id())\
                                     .get()

                if existing:
                    existing.delete()

        return self.render_json({
            'status': 'ok',
            'likes': len(recipe.likes)
        })