<repo_name>wtachau/IOU<file_sep>/iou.py
class User:
# state represents the board, next piece to be played, and other relevant info
    def __init__(self, user_id, friends, tickets):
    	self.user_id = user_id
    	self.friends = friends
    	self.tickets = tickets

    def get_balance(self):
    	balance = 0
    	for ticket in tickets:
    		balance = balance + ticket.get_value()
    	return balance
    
class Ticket:
	def __init__(self, id, label, value, date, user, active):
		self.id = id
		self,label = label
		self.value = value
		self.date = date
		self.user = users

	def get_value(self):
		return self.value()

	def get_date(self):
		return self.date()

	def get_label(self):
		return self.label()

	def get_users(self):
		return self.users()

class Label:
	def __init__(self, image, title, description):
		self.image = image
		self.title = title
		self.description = description

	def get_image(self):
		return self.image

	def get_title(self):
		return self.title

	def get_description(self):
		return self.description





    

# end of program
<file_sep>/db.py
from flask import (
    Flask,
    render_template,
    session,
    redirect,
    request,
    url_for
)
from mongokit import Connection, Document, ObjectId
import datetime
import json
import requests
import urllib


MONGODB_HOST = 'localhost'
MONGODB_PORT = 27017
DEBUG = True
SECRET_KEY = 'development key'
names = ''


app = Flask(__name__)
app.config.from_object(__name__)
connection = Connection(app.config['MONGODB_HOST'], app.config['MONGODB_PORT'])

class Entry(Document):
    use_dot_notation = True
    __collection__='entry'
    __database__='main'


    structure = {
        'nameAndIDOfOwed': (basestring, int),
        'ticketAmount' : int,
        'ticketType' : basestring,
        'ticketDate' : datetime.datetime,
        'ticketMessage' : basestring,
        'ticketActive' : bool,
        'nameAndIDOfOwers' : [(basestring, int)],
    }

    default_values= {'ticketDate' : datetime.datetime.utcnow}


    def id(self):
        return self._id
    
    

connection.register([Entry])
connection.main.entry.Entry()


def new_entry(name, pin):
	new_entry=connection.main.entry.Entry()
	new_entry.nameAndIDOfOwed = (name, pin)
	#entries.add(new_entry)
	new_entry.save()


new_entry("Alana", 0)
new_entry("Jessie", 1)

@app.route('/')
def hello():
	names_before = ''
	i = 0
    	for item in connection.main.entry.find():
    		names_before += item['nameAndIDOfOwed'][0] + str(i) +"\n"
    		i+=1
    		connection.main.entry.remove()
    	return names_before


if __name__=="__main__":
	app.run(debug=True)
<file_sep>/views.py
@app.route('/')
def index():
    return render_template('index.html')

@app.route('/login')
def login():
    return render_template('login.html')

"""@app.route('/profile')
@login_required
def profile():
    return render_template(
        'profile.html',
        content='Profile Page',
        facebook_conn=social.facebook.get_connection())"""<file_sep>/app.py
from flask import (
    Flask,
    render_template,
    session,
    redirect,
    request,
    url_for
)
from mongokit import Connection, Document, ObjectId
import datetime
from oauth import sign_url
from flask_oauth import OAuth
import json
import requests
import urllib

MONGODB_HOST = 'localhost'
MONGODB_PORT = 27017
DEBUG = True
SECRET_KEY = 'development key'

app = Flask(__name__) 
app.config.from_object(__name__)
connection = Connection(app.config['MONGODB_HOST'], app.config['MONGODB_PORT'])

personCollection = connection['personEntry'].entries
ticketCollection = connection['ticketEntry'].entries

class User(Document):
    use_dot_notation = True
    __collection='user'
    __database__='main'

    structure = {
        'name': basestring,
        'created_at': datetime.datetime,
        'email': basestring,
        'password': <PASSWORD>,
    }

    default_values = {'created_at': datetime.datetime.utcnow}
    def id(self):
        return self._id
    def __repr__(self):
        return '<Entry %s>' % self['name']

class Ticket(Document):
    use_dot_notation = True
    __collection='ticket'
    __database__='main'


    structure = {
        'nameAndIDOfOwed': (basestring, int),
        'ticketAmount' : int,
        #'ticketType' : basestring,
        'ticketDate' : datetime.datetime,
        'ticketMessage' : basestring,
        'ticketActive' : bool,
        'nameAndIDOfOwers' : [(basestring, int)],
    }

    default_values= {'ticketDate' : datetime.datetime.utcnow, 'ticketActive' : True}

    def id(self):
        return self._id


connection.register([Ticket])
connection.register([User])
connection.main.entry.Ticket()
connection.main.entry.User()
current = User()
oauth = OAuth()


@app.route('/')
def login():
    #return facebook.authorize(callback=url_for('facebook_authorized',
    #    next=request.args.get('next') or request.referrer or None,
    #    _external=True))

    alreadylogged = False;
    if 'username' in session:
        alreadylogged = True;
        print "Already logged in as %s" % session['username']
    return render_template('login.html', islogged = alreadylogged)


@app.route('/home')
def home():
    if (session['logged_in']):
        print session['username']
        return render_template('index.html')
    else:
        print "NOT LOGGED IN!"
        return render_template('login.html')

@app.route('/make_ticket', methods=['GET', 'POST'])
def make_ticket():
    if (session['logged_in']):
        if request.method=='POST':
            new_ticket = connection.main.ticketCollection.Ticket()
            new_ticket.nameAndIDOfOwed= (session['username'], 0)
            new_ticket.nameAndIDOfOwers = [(request.form['friend_name'], 0)]
            if(request.form['direction']=='negative'):
                new_ticket.ticketAmount = -(int(request.form['amount']))
            else:
                new_ticket.ticketAmount = int(request.form['amount'])
            new_ticket.ticketMessage = request.form['message']
            new_ticket.save()
            return redirect(url_for('profile'))
        else:
            return render_template('makeTix.html')
    else:
        print "NOT LOGGED IN!"
        return render_template('login.html', islogged=False)

@app.route('/profile')
def profile():
    if (session['logged_in']):
        print session['username']
        ticket_list = []
        balance=0
        for item in connection.main.ticketCollection.find():
            if session['username'] == item['nameAndIDOfOwed'][0]:
                balance+=item['ticketAmount']
                printed = "Name of Friend: " + item['nameAndIDOfOwers'][0][0] + ".  Amount: $" + str(item['ticketAmount']) + ".\n\"" + item['ticketMessage']+ "\"\n"
                ticket_list.append(printed)
            elif(item['nameAndIDOfOwers'][0]==session['username']):
                balance-=item['ticketAmount']
        return render_template('profile.html', tickets=ticket_list, balance=balance)
    else:
        print "NOT LOGGED IN!"
        return render_template('login.html', islogged=False)

@app.route('/profile', methods=['POST'])
def save_entry():
    #new_entry = personCollection.User()
    new_entry = connection.main.personCollection.User()
    new_entry.name = request.form['user_name']
    new_entry.url = request.form['email']
    new_entry.phone_number = request.form['password']
    new_entry.save()



@app.route('/logout', methods=['POST'])
def logout():
    print"logging out"
    session.pop('username', None)
    session['logged_in'] = False
    return render_template('login.html')

@app.route('/loginattempt', methods=['GET', 'POST'])
def trylogin():
    print "here"
    error = None
    if request.method == 'POST':
            print('You were logged in')
            new_entry = connection.main.personCollection.User()
            new_entry.name = request.form['user_name']
            new_entry.url = request.form['email']
            new_entry.phone_number = request.form['password']
            new_entry.save()

            session['logged_in'] = True
            session['username'] = new_entry.name

    #for item in connection.main.personCollection.find():
        #print item['name']

    return redirect(url_for('home'))

def get_tickets():
    for item in connection.main.ticketCollection.Ticket():
        print item


"""# We'll need a user class, but how?
class User(db.Model):
    User Model Class
    id = db.StringProperty(required=True) #facebook user-id
    created = db.DateTimeProperty(auto_now_add=True)
    updated = db.DateTimeProperty(auto_now=True)
    name = db.StringProperty(required=True)
    profile_url = db.StringProperty(required=True)
    access_token = db.StringProperty(required=True)  #fb OAUTH access token"""

if __name__ == '__main__':
    app.run(debug=True)
