<commit_before>#!/usr/bin/env python

import json
from flask import request
from flask.ext.restful import Resource
from models.charity import Charity as C
from karma import db
from karma.models import User
from karma.api import profile


def create_response(charity, recursing=False):
    if recursing:
        return {
            "id": charity.id,
            "name": charity.name,
            "description": charity.description}
    return {
        "id": charity.id,
        "name": charity.name,
        "description": charity.description,
        "followers": map(lambda x: profile.create_response(x, recursing=True), charity.followers)
    }


class Charity(Resource):
    def get(self, charity_name):
        c = C.query.filter_by(id=charity_name).first_or_404()

        return create_response(c)

    def put(self, charity_name):
        try:
            obj = json.loads(request.data)
        except ValueError:
            return {"status": 400}
        user = User.query.get_or_404(obj['user'])
        charity = C.query.get_or_404(charity_name)
        user.following_charities.append(charity)
        db.session.add(user)
        db.session.commit()

        return create_response(charity)


class Charities(Resource):
    def get(self):

        charities = C.query.all()
        ret = list()<commit_msg>Return 400 if already subscribed to charity<commit_after>#!/usr/bin/env python

import json
from flask import request
from flask.ext.restful import Resource
from models.charity import Charity as C
from karma import db
from karma.models import User
from karma.api import profile


def create_response(charity, recursing=False):
    if recursing:
        return {
            "id": charity.id,
            "name": charity.name,
            "description": charity.description}
    return {
        "id": charity.id,
        "name": charity.name,
        "description": charity.description,
        "followers": map(lambda x: profile.create_response(x, recursing=True), charity.followers)
    }


class Charity(Resource):
    def get(self, charity_name):
        c = C.query.filter_by(id=charity_name).first_or_404()

        return create_response(c)

    def put(self, charity_name):
        try:
            obj = json.loads(request.data)
        except ValueError:
            return {"status": 400}
        user = User.query.get_or_404(obj['user'])
        charity = C.query.get_or_404(charity_name)
        if charity in user.following_charities:
            return {"status": 400}

        user.following_charities.append(charity)
        db.session.add(user)
        db.session.commit()

        return create_response(charity)


class Charities(Resource):
    def get(self):

        charities = C.query.all()
        ret = list()
{'fraction_non_alphanumeric': 0.10486211901306242, 'fraction_numerical': 0.01088534107402032, 'mean_word_length': 2.608638743455497, 'pattern_counts': {'":': 17, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2840612', 'n_tokens_mistral': 850, 'n_tokens_neox': 807, 'n_words': 196}