Exemplo n.º 1
0
    def get(self):
        config = get_config()
        subvoat_utils = UserUtils(self.db)
        parser = reqparse.RequestParser()
        return_data = []

        parser.add_argument('subvoat_name')

        args = parser.parse_args()

        schema = Schema({
            Required('subvoat_name'):
            All(str, Length(min=config['min_length_subvoat_name']))
        })

        try:
            schema({'subvoat_name': args.get('subvoat_name')})

        except MultipleInvalid as e:
            return {'error': '%s %s' % (e.msg, e.path)}

        posts = subvoat_utils.get_posts(args['subvoat_name'])

        for p in posts:
            return_data.append(p)

        return {'result': return_data}
Exemplo n.º 2
0
    def post(self):
        subvoat_utils = SubvoatUtils(self.db)
        config        = get_config()
        parser        = reqparse.RequestParser()

        parser.add_argument('subvoat_name')
        parser.add_argument('title')
        parser.add_argument('body')
        parser.add_argument('username')
        parser.add_argument('api_token')
       
        args = parser.parse_args()

    
        
        result, message = subvoat_utils.add_thread(args['subvoat_name'],
                                                 args['title'],
                                                 args['body'],
                                                 args['username'])


        if not result:
            return {'error':message}

        return {'result':message}
Exemplo n.º 3
0
def send_thread():
    # yep, getting the config 2x. Otherwise it wont update the whitelist when a new task is started
    # FIX CONFIG?
    config = get_config()
    for address in config['whitelisted_servers']:
        print(address)
        requests.post('%s/update_data' % (address), {'test': 'test'})
Exemplo n.º 4
0
    def post(self):
        user_utils = UserUtils(self.db)

        parser.add_argument('setting')
        parser.add_argument('value')
        parser.add_argument('username')
        parser.add_argument('api_token')

        args = parser.parse_args()

        user = user_utils.authenticate_by_token(args.get('username'),
                                                args.get('api_token'))

        if not user:
            # FIX: should probably log this
            # FIX: should probably rate limit this as well
            return {'error': 'incorrect login'}

        # Gonna put get_config here, instead of up there. That way it doesn't load everytime someone tries to access the admin API.
        config = get_config()
        setting = args.get('setting')
        value = args.get('value')

        if setting not in config.keys():
            return {'error': 'setting not in config'}

        write_value(setting, value)

        return {'result': 'setting updated'}
Exemplo n.º 5
0
    def post(self):
        config = get_config()
        parser = reqparse.RequestParser()
        user_utils = UserUtils(self.db)
        subvoat_utils = SubvoatUtils(self.db)

        parser.add_argument('username')
        parser.add_argument('api_token')
        parser.add_argument('body')
        parser.add_argument('thread_uuid')
        parser.add_argument('reply_to_uuid')

        args = parser.parse_args()

        # Validation setup
        uuid_schema = Schema(
            {Required('reply_to_uuid'): All(str, Length(min=36, max=36))})
        schema = Schema({
            Required('body'):
            All(
                str,
                Length(min=config['min_length_comment_body'],
                       max=config['max_length_comment_body']))
        })

        # Validation
        try:
            schema({'body': args.get('body')})

            if args.get('reply_to_uuid'):
                uuid_schema({'reply_to_uuid': args.get('reply_to_uuid')})

        except MultipleInvalid as e:
            return {'error': '%s %s' % (e.msg, e.path)}

        user = user_utils.authenticate_by_token(args.get('username'),
                                                args.get('api_token'))

        if not user:
            return {'error': 'incorrect login'}

        # See if we are replying to a comment
        if args.get('reply_to_uuid'):
            status, result = subvoat_utils.add_comment(
                thread_uuid=args.get('thread_uuid'),
                reply_uuid=args.get('reply_to_uuid'),
                body=args.get('body'),
                user_obj=user)

        else:
            status, result = subvoat_utils.add_comment(
                thread_uuid=args.get('thread_uuid'),
                body=args.get('body'),
                user_obj=user)

        if status == True:
            return {'result': 'comment added'}
Exemplo n.º 6
0
    def post(self):
        config = get_config()
        subvoat_utils = SubvoatUtils(self.db)
        user_utils = UserUtils(self.db)
        parser = reqparse.RequestParser()
        return_data = []

        parser.add_argument('subvoat_name')

        args = parser.parse_args()
        schema = Schema({
            Required('subvoat_name'):
            All(str, Length(min=config['min_length_subvoat_name']))
        })

        try:
            schema({'subvoat_name': args.get('subvoat_name')})

        except MultipleInvalid as e:
            return {'error': '%s %s' % (e.msg, e.path)}

        threads = subvoat_utils.get_all_threads(args['subvoat_name'])

        # see the thread schema in voat_sql/schemas/subvoat_schema.py
        # I convert the user_id to username
        for t in threads:
            user_status, user_result = user_utils.get_user_by_id(t.user_id)

            if user_status:
                username = user_result.username

            else:
                # NEED TO LOG THIS
                continue

            c = 0

            for v in t.votes:
                c += v.direction

            return_data.append({
                'uuid': t.uuid,
                'title': t.title,
                'body': t.body,
                'username': username,
                'creation_date': t.creation_date.isoformat(),
                'votes': c
            })

        return {'result': return_data}
Exemplo n.º 7
0
    def post(self):
        subvoat_utils = SubvoatUtils(self.db)
        user_utils    = UserUtils(self.db)
        parser        = reqparse.RequestParser()
        config        = get_config()

        parser.add_argument('subvoat_name')
        parser.add_argument('username')
        parser.add_argument('api_token')

        args = parser.parse_args()


        # authenticate first
        user = user_utils.authenticate_by_token(args.get('username'), args.get('api_token'))
    
    
        if not user:
            return {'error':'incorrect login'}


        # should validate the subvoat here
        schema = Schema({Required('subvoat_name'): All(str, Length(min=config['min_length_subvoat_name']))})
        
        try:
            # Just try the ones we need. 
            schema({'subvoat_name':args.get('subvoat_name')})

        except MultipleInvalid as e:
            # NEED BETTER ERROR MESSAGES, FIX THIS
            return {'error':'%s %s' % (e.msg, e.path)}

    
        # See if the subvoat exists
        if subvoat_utils.get_subvoat(args.get('subvoat_name')):
            return {'error':'subvoat already exists'}

    
        # If not create it (should probably add some rate-limiting or something around here)
        new_subvoat = subvoat_utils.create_subvoat_object(name=args.get('subvoat_name'),
                                                          owner_id=user.id,
                                                          creator_id=user.id,
                                                          creation_date=datetime.datetime.utcnow())
                                                          
                                                          
        if not subvoat_utils.add_subvoat(new_subvoat):
            return {'success':'subvoat created'}

        return {'error':'could not create subvoat'}
Exemplo n.º 8
0
    def post(self):
        user_utils = UserUtils()

        parser.add_argument('username')
        parser.add_argument('api_token')

        args = parser.parse_args()

        user = user_utils.authenticate_by_token(args.get('username'),
                                                args.get('api_token'))

        if not user:
            return {'error': 'incorrect login'}

        config = get_config()

        return {'result': config}
Exemplo n.º 9
0
def get_db():
    return Connect(config.get_config()['SQLALCHEMY_DATABASE_URI'])
Exemplo n.º 10
0
from voat_rest import register
from voat_rest import authenticate
from voat_rest import subvoat
from voat_rest import utils
from voat_rest import vote
from voat_utils import config

from voat_sql.utils.db import get_db

# Need a logger

app = Flask(__name__)
CORS(app)
api = Api(app)

app.config.update(config.get_config())

db = get_db()

print(db)

kwargs = {'db': db}

api.add_resource(authenticate.Authenticate,
                 '/authenticate',
                 resource_class_kwargs=kwargs)
api.add_resource(register.Register, '/register', resource_class_kwargs=kwargs)
api.add_resource(subvoat.AddSubvoat,
                 '/create_subvoat',
                 resource_class_kwargs=kwargs)
api.add_resource(subvoat.ListSubvoats,
Exemplo n.º 11
0
import requests

from celery import Celery

from flask import current_app

from voat_utils.config import get_config

app = Celery('send', broker=get_config()['broker'])


@app.task
def send_thread():
    # yep, getting the config 2x. Otherwise it wont update the whitelist when a new task is started
    # FIX CONFIG?
    config = get_config()
    for address in config['whitelisted_servers']:
        print(address)
        requests.post('%s/update_data' % (address), {'test': 'test'})
Exemplo n.º 12
0
# May use Celery here
#http://www.prschmid.com/2013/04/using-sqlalchemy-with-celery-tasks.html

from flask import Flask
#from celery import Celery

from flask_restful import Resource, reqparse, Api

from voat_utils import config
from voat_sql.utils.servers import ServerUtils

app = Flask(__name__)
api = Api(app)
#c_app = Celery('tasks', broker='redis://localhost:6379')

app.config.update(config.get_config())


class UpdatePost(Resource):
    def post(self):

        server_utils = ServerUtils()

        parser = reqparse.RequestParser()

        parser.add_argument('subvoat_name')
        parser.add_argument('user_id')
        parser.add_argument('original_instance')
        parser.add_argument('title')
        parser.add_argument('body')
        parser.add_argument('signature')
Exemplo n.º 13
0
 def __init__(self, db):
     self.db = db
     self.config = get_config()
     self.user_utils = UserUtils(self.db)
     self.session = db.session()
Exemplo n.º 14
0
 def __init__(self, db):
     self.db      = db
     self.config  = get_config()
     self.session = db.session()
Exemplo n.º 15
0
import os
#from voat_sql.utils.db  import get_db
from voat_utils.config import get_config
from voat_utils.config import write_value
from voat_utils.keys import generate_key

#db = get_db()
#classes = db.base.classes

config = get_config()


def update_server_key():
    key = generate_key(config['server_key_bits'])

    if os.path.exists('/etc/voat/config/public_key') or os.path.exists(
            '/etc/voat/config/private_key'):
        confirm = input(
            'Keys are already in /etc/voat/config/ , generate new ones anyway? [y/n]: '
        ).lower().strip()

        if confirm != 'y' and confirm != 'yes':
            exit()

    with open('/etc/voat/config/public_key', 'wb') as public_key:
        public_key.write(key.publickey().exportKey())

    with open('/etc/voat/config/private_key', 'wb') as private_key:
        private_key.write(key.exportKey())

    print('New (%s) bit keys generated' % (config['server_key_bits']))
Exemplo n.º 16
0
 def __init__(self):
     self.db = get_db()
     self.config = get_config()
     self.classes = self.db.base.classes