# Path: screenchop/config.py
# HOST = config.get('app', 'HOST')
# PORT = int(config.get('app', 'PORT'))
# DEBUG = config.getboolean('app', 'DEBUG')
# TEMP_FILE_DIR = config.get('app', 'TEMP_FILE_DIR')
# SESSION_KEY = config.get('app', 'SESSION_KEY')
# DOMAIN_URL = config.get('app', 'DOMAIN_URL')
# SHORT_DOMAIN_URL = config.get('app', 'SHORT_DOMAIN_URL')
# ADMIN_EMAIL = config.get('app', 'ADMIN_EMAIL')
# EXCEPTION_EMAIL = config.get('app', 'EXCEPTION_EMAIL').split(',')
# CACHE_TYPE = config.get('caching', 'CACHE_TYPE')
# CACHE_SERVER_ADDRESS = config.get('caching', 'CACHE_SERVER_ADDRESS').split()
# CACHE_DEFAULT_TIMEOUT = config.getint('caching', 'CACHE_DEFAULT_TIMEOUT')
# MONGO_DATABASE = config.get('mongodb', 'DATABASE')
# MONGO_HOST = config.get('mongodb', 'HOST')
# MONGO_PORT = int(config.get('mongodb', 'PORT'))
# MONGO_USER = config.get('mongodb', 'USER')
# MONGO_PASS = config.get('mongodb', 'PASS')
# BUCKET_NAME = config.get('s3', 'BUCKET_NAME')
# AWS_ACCESS_KEY_ID = config.get('s3', 'AWS_ACCESS_KEY_ID')
# AWS_SECRET_ACCESS_KEY = config.get('s3', 'AWS_SECRET_ACCESS_KEY')
# S3_THUMBS_URL = config.get('s3', 'S3_THUMBS_URL')
# S3_MEDIUM_URL = config.get('s3', 'S3_MEDIUM_URL')
# S3_FULL_URL = config.get('s3', 'S3_FULL_URL')
# S3_AVATAR_URL = config.get('s3', 'S3_AVATAR_URL')
# REGISTRATION_LEVEL = config.get('registration', 'REGISTRATION_LEVEL')
# MAX_IMAGES_PER_ROW = int(config.get('gallery', 'MAX_IMAGES_PER_ROW'))
# HOME_MAX_IMAGES = int(config.get('gallery', 'HOME_MAX_IMAGES'))
# TAGGING_ENABLED = config.getboolean('gallery', 'TAGGING_ENABLED')
# ALLOWED_FILE_TYPES = config.get('upload', 'ALLOWED_FILE_TYPES').split(',')
# THUMB_MAX_WIDTH = int(config.get('upload', 'THUMB_MAX_WIDTH'))
# THUMB_MAX_HEIGHT = int(config.get('upload', 'THUMB_MAX_HEIGHT'))
# MEDIUM_MAX_WIDTH = int(config.get('upload', 'MEDIUM_MAX_WIDTH'))
# MEDIUM_MAX_HEIGHT = int(config.get('upload', 'MEDIUM_MAX_HEIGHT'))
# GOOGLE_ANALYTICS_ACCOUNT = config.get('marketing', 'GOOGLE_ANALYTICS_ACCOUNT')
# 
# Path: screenchop/sessions.py
# def requires_auth(f):
#     @wraps(f)
#     def decorated(*args, **kwargs):
#         auth = request.authorization
#         if not "username" in session:
#             return deny_access()
#         return f(*args, **kwargs)
#     return decorated
# 
# Path: screenchop/models.py
# class Post(Document):
#     uid = SequenceField()
#     submitter = StringField(required=True)
#     caption = StringField(max_length=200)
#     tags = ListField(StringField(max_length=30))
#     filename = StringField(max_length=30)
#     date = DateTimeField(default=datetime.datetime.now)
#     upvotes = IntField(default=1)
#     downvotes = IntField(default=0)
#     width = IntField(required=True)
#     height = IntField(required=True)
#     rank = FloatField(default=ranking.hot(1, 0, datetime.datetime.now()))
# 
#     meta = {
#         'indexes': ['uid', 'filename', 'rank'],
#         }
# 
# class Vote(Document):
#     username = StringField(required=True, max_length=60)
#     postuid = IntField(required=True)
#     downvoted = BooleanField(default=False)
#     upvoted = BooleanField(default=False)
# 
#     meta = {
#         'indexes': ['username', 'postuid']
#         }
# 
# Path: screenchop/util/ranking.py
# def epoch_seconds(date):
# def score(ups, downs):
# def hot(ups, downs, date):


import os, sys
from time import strftime
from flask import Flask
from flask import request, session
from screenchop import config
from screenchop.sessions import requires_auth
from screenchop.models import Post, Vote
from screenchop.util import ranking
                                                    set__downvoted=False)
        return 'upvoted2'
        
    else:
        Vote.objects(username=session['username'],
                        postuid=chopObject.uid).update_one(set__upvoted=True,
                                                            set__downvoted=False)
        return 'changed to upvote'

    print 'Upvoted: ', vote.upvoted
    print 'Downvoted: ', vote.downvoted

def downvote():
    if 'username' not in session:
        return 'not logged in'
        
    chop = request.form['chop']
    chopObject = Post.objects.get(filename=chop)
    vote, created = Vote.objects.get_or_create(username=session['username'], postuid=chopObject.uid)
    
    if created:
        Post.objects(filename=chop).update_one(inc__downvotes=1)

        #reload post object for updated rank since downvote
        chopObject.reload()

        # set rank using algorithm
        rank = ranking.hot(chopObject.upvotes, chopObject.downvotes, chopObject.date) 

        # Update rank in database

# Path: screenchop/config.py
# HOST = config.get('app', 'HOST')
# PORT = int(config.get('app', 'PORT'))
# DEBUG = config.getboolean('app', 'DEBUG')
# TEMP_FILE_DIR = config.get('app', 'TEMP_FILE_DIR')
# SESSION_KEY = config.get('app', 'SESSION_KEY')
# DOMAIN_URL = config.get('app', 'DOMAIN_URL')
# SHORT_DOMAIN_URL = config.get('app', 'SHORT_DOMAIN_URL')
# ADMIN_EMAIL = config.get('app', 'ADMIN_EMAIL')
# EXCEPTION_EMAIL = config.get('app', 'EXCEPTION_EMAIL').split(',')
# CACHE_TYPE = config.get('caching', 'CACHE_TYPE')
# CACHE_SERVER_ADDRESS = config.get('caching', 'CACHE_SERVER_ADDRESS').split()
# CACHE_DEFAULT_TIMEOUT = config.getint('caching', 'CACHE_DEFAULT_TIMEOUT')
# MONGO_DATABASE = config.get('mongodb', 'DATABASE')
# MONGO_HOST = config.get('mongodb', 'HOST')
# MONGO_PORT = int(config.get('mongodb', 'PORT'))
# MONGO_USER = config.get('mongodb', 'USER')
# MONGO_PASS = config.get('mongodb', 'PASS')
# BUCKET_NAME = config.get('s3', 'BUCKET_NAME')
# AWS_ACCESS_KEY_ID = config.get('s3', 'AWS_ACCESS_KEY_ID')
# AWS_SECRET_ACCESS_KEY = config.get('s3', 'AWS_SECRET_ACCESS_KEY')
# S3_THUMBS_URL = config.get('s3', 'S3_THUMBS_URL')
# S3_MEDIUM_URL = config.get('s3', 'S3_MEDIUM_URL')
# S3_FULL_URL = config.get('s3', 'S3_FULL_URL')
# S3_AVATAR_URL = config.get('s3', 'S3_AVATAR_URL')
# REGISTRATION_LEVEL = config.get('registration', 'REGISTRATION_LEVEL')
# MAX_IMAGES_PER_ROW = int(config.get('gallery', 'MAX_IMAGES_PER_ROW'))
# HOME_MAX_IMAGES = int(config.get('gallery', 'HOME_MAX_IMAGES'))
# TAGGING_ENABLED = config.getboolean('gallery', 'TAGGING_ENABLED')
# ALLOWED_FILE_TYPES = config.get('upload', 'ALLOWED_FILE_TYPES').split(',')
# THUMB_MAX_WIDTH = int(config.get('upload', 'THUMB_MAX_WIDTH'))
# THUMB_MAX_HEIGHT = int(config.get('upload', 'THUMB_MAX_HEIGHT'))
# MEDIUM_MAX_WIDTH = int(config.get('upload', 'MEDIUM_MAX_WIDTH'))
# MEDIUM_MAX_HEIGHT = int(config.get('upload', 'MEDIUM_MAX_HEIGHT'))
# GOOGLE_ANALYTICS_ACCOUNT = config.get('marketing', 'GOOGLE_ANALYTICS_ACCOUNT')
# 
# Path: screenchop/sessions.py
# def requires_auth(f):
#     @wraps(f)
#     def decorated(*args, **kwargs):
#         auth = request.authorization
#         if not "username" in session:
#             return deny_access()
#         return f(*args, **kwargs)
#     return decorated
# 
# Path: screenchop/models.py
# class Post(Document):
#     uid = SequenceField()
#     submitter = StringField(required=True)
#     caption = StringField(max_length=200)
#     tags = ListField(StringField(max_length=30))
#     filename = StringField(max_length=30)
#     date = DateTimeField(default=datetime.datetime.now)
#     upvotes = IntField(default=1)
#     downvotes = IntField(default=0)
#     width = IntField(required=True)
#     height = IntField(required=True)
#     rank = FloatField(default=ranking.hot(1, 0, datetime.datetime.now()))
# 
#     meta = {
#         'indexes': ['uid', 'filename', 'rank'],
#         }
# 
# class Vote(Document):
#     username = StringField(required=True, max_length=60)
#     postuid = IntField(required=True)
#     downvoted = BooleanField(default=False)
#     upvoted = BooleanField(default=False)
# 
#     meta = {
#         'indexes': ['username', 'postuid']
#         }
# 
# Path: screenchop/util/ranking.py
# def epoch_seconds(date):
# def score(ups, downs):
# def hot(ups, downs, date):


# Path: screenchop/controllers/vote.py
import os, sys
from time import strftime
from flask import Flask
from flask import request, session
from screenchop import config
from screenchop.sessions import requires_auth
from screenchop.models import Post, Vote
from screenchop.util import ranking

                                                    set__downvoted=False)
        return 'upvoted2'
        
    else:
        Vote.objects(username=session['username'],
                        postuid=chopObject.uid).update_one(set__upvoted=True,
                                                            set__downvoted=False)
        return 'changed to upvote'

    print 'Upvoted: ', vote.upvoted
    print 'Downvoted: ', vote.downvoted

def downvote():
    if 'username' not in session:
        return 'not logged in'
        
    chop = request.form['chop']
    chopObject = Post.objects.get(filename=chop)
    vote, created = Vote.objects.get_or_create(username=session['username'], postuid=chopObject.uid)
    
    if created:
        Post.objects(filename=chop).update_one(inc__downvotes=1)

        #reload post object for updated rank since downvote
        chopObject.reload()

        # set rank using algorithm
        rank = ranking.hot(chopObject.upvotes, chopObject.downvotes, chopObject.date) 

        # Update rank in database

        Post.objects(filename=chop).update_one(set__rank=rank)