-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
499 lines (331 loc) · 13.9 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
from flask import Flask, request, g, session, redirect, url_for
from flask import render_template_string , render_template
from flask.ext.github import GitHub
from flask import jsonify
#import json
from sqlalchemy import create_engine, Column, Integer, String
from sqlalchemy.orm import scoped_session, sessionmaker
from sqlalchemy.ext.declarative import declarative_base
from werkzeug import secure_filename
import stripe
import re
import markdown
import io
import sys
from flask.ext.pymongo import PyMongo
import os
import stripe
reload(sys)
sys.setdefaultencoding('utf-8')
#I am using this for stripe payments
stripe_keys = {
'secret_key': '',
'publishable_key': ''
}
stripe.api_key = stripe_keys['secret_key']
DATABASE_URI = 'sqlite:///github-flask.db'
SECRET_KEY = 'development key'
DEBUG = True
# Github App Registration
GITHUB_CLIENT_ID = ''
GITHUB_CLIENT_SECRET = ''
# setup flask
app = Flask(__name__)
app.config.from_object(__name__)
########### This is for the stripe part #####################
####
###### Setting the cconfigurating for MONGODB #########
app.config['MONGO_DBNAME'] = "Enter your DB name"
app.config['MONGO_URI'] = "Enter your DB URI"
mongo = PyMongo(app)
## check in terminal from mongo_connect import mongo
@app.route('/ty', methods=['POST'])
def charge():
# Amount in cents
amount = 5000
customer = stripe.Customer.create(
email='customer@example.com',
source=request.form['stripeToken']
)
charge = stripe.Charge.create(
customer=customer.id,
amount=amount,
currency='usd',
description='Flask Charge'
)
## Fetching the user and project from the showgig page so that i can
## read the database and see which is the dropbox link for that project
user = request.form['user']
project = request.form['project']
## Searching the database for the corresponding project
users = mongo.db.user
results = users.find_one({'name': user})
index = results['gigs'].index(project)
return render_template('ty.html', amount=amount, dropbox = results['dropbox'][index])
# setup github-flask
github = GitHub(app)
# setup sqlalchemy
engine = create_engine(app.config['DATABASE_URI'])
db_session = scoped_session(sessionmaker(autocommit=False,
autoflush=False,
bind=engine))
Base = declarative_base()
Base.query = db_session.query_property()
def init_db():
Base.metadata.create_all(bind=engine)
####################################
### Creating the SQLite3 DB Schema #
####################################
class User(Base):
__tablename__ = 'users'
id = Column(Integer, primary_key=True)
username = Column(String(200))
github_access_token = Column(String(200))
def __init__(self, github_access_token):
self.github_access_token = github_access_token
@app.before_request
def before_request():
g.user = None
if 'user_id' in session:
g.user = User.query.get(session['user_id'])
@app.after_request
def after_request(response):
db_session.remove()
return response
@app.route('/')
def index():
#Checks if the user is logged out or logged in based on the session
if g.user:
# Means session exists and the user is logged in
state="Logout"
link = url_for("logout")
else:
state = "Github Login"
link = url_for("login")
return render_template('index.html',LoginOrLogout=state,ButtonLink=link)
class Info():
userDict=dict()
def __init__(self,userDict):
self.userDict = userDict
########################################################
#### File Upload and to Markdown Conversion ###########
########################################################
@app.route('/showgig',methods = ["GET","POST"])
def showgig():
# This view function will only get POST request
#I need the author name and the video URL
if request.method == "POST":
url = str(request.form['URL'])
search = re.match(r'(.*)v=(.*)',url)
embed_code = search.group(2)
f = request.files['markdown']
'''
f.save(secure_filename(f.filename))
#This is the readme file
filename = secure_filename(f.filename)
#Opening the file to read all its contents
with io.open(str("./"+filename),'r',encoding='utf8') as markdownFile:
fileContents = markdownFile.read()
'''
fileContents = f.read()
projectname = str(request.form['projectname']).strip()
description = str(request.form['description']).strip()
dropbox = str(request.form['dropbox']).strip()
dropbox=dropbox.replace("dl=0","dl=1") # I need to add a test case here
## Fetching all user Infor for gig page
#######################################
UserDict = Info.userDict # This returns a dictionary
avatar = UserDict['avatar_url']
name = UserDict['name']
location = UserDict['location']
email = UserDict['email']
giturl = UserDict['html_url']
## Genrating the dynamic link where i can find the project portfolio
link = "https://devsupport.herokuapp.com/explore/"+ giturl.split('https://github.com/')[1]+'/'+projectname
#######################################################################
## Since gig is posted, lets add it to the collection of the users gigs
#######################################################################
users = mongo.db.user
users.update({'email':email},{'$push':{'gigs':projectname,'youtube':embed_code,'MDcontents':fileContents,'dropbox':dropbox,'thumbsup':0}})
##########################################################
## Also adding the link to the explorelinks mongo document
##########################################################
explore_links = mongo.db.explorelinks
explore_links.insert({'link':link,'gigs':projectname,'description':description,'embed_code':embed_code,'thumbsup':0})
return render_template('showgig.html',data = embed_code\
,markdownData=fileContents,projectname=projectname\
,avatar=avatar,name=name,location=location,email=email,giturl=giturl,key=stripe_keys['publishable_key'],view=0)
if request.method == "GET":
# GET request is not valid for this URL route
# Maybe flash a message if necessary
return redirect(url_for("index"))
@app.route('/gigform',methods=["GET"])
def gigform():
# This URL will only get get Requests
if request.method == "GET" and session.get('user_id', None) is not None:
# I need to fetch the user details over here
# Wish the User a Hello with his picture
UserDict = github.get('user') # This returns a dictionary
#passing the information to the Info Classs such that it can be accessed by all other routes
Info.userDict = UserDict
avatar = UserDict['avatar_url']
name = UserDict['name']
location = UserDict['location']
email = UserDict['email']
giturl = UserDict['html_url']
### DATABASE LOGIC #####################################
## If the user does not exist in database then add him ##
#########################################################
gigs = []
users = mongo.db.user
link= ""
x= users.find_one({'email': email})
if x is None:
users.insert({'email':email,'gigs':[],'MDcontents':[],'youtube':[],'avatar':avatar,'location':location,'name':giturl.split('https://github.com/')[1]})
else:
#This is an existing user :: check if he has any projects already
# if yes , we need to send the data to the created gigs tab
info = users.find_one({'email':email})
gigs= info['gigs']
link = "https://devsupport.herokuapp.com/explore/"+ giturl.split('https://github.com/')[1]+'/'
return render_template('gigform1.html',avatar=avatar,name=name\
,location=location,email=email,giturl=giturl,gigs=gigs,link=link)
else:
return "You are unauthorized to access this page. Consider logging in "
@github.access_token_getter
def token_getter():
user = g.user
if user is not None:
return user.github_access_token
@app.route('/error')
def unauth():
return "Unauthorized Access :: Invalid Password"
###############################################
## Callback page after the Auth is successfull
###############################################
@app.route('/github-callback')
@github.authorized_handler
def authorized(access_token):
next_url = url_for('unauth')
if access_token is None:
return redirect(next_url)
user = User.query.filter_by(github_access_token=access_token).first()
if user is None:
user = User(access_token)
db_session.add(user)
user.github_access_token = access_token
db_session.commit()
session['user_id'] = user.id
return redirect(url_for('gigform'))
@app.route('/login')
def login():
if session.get('user_id', None) is None:
return github.authorize()
else:
return 'Already logged in'
@app.route('/logout')
def logout():
# I need to remove the tokens from the database too
# BUT
# Flask will automatically remove database sessions
# at the end of the request or when the application shuts down
session.pop('user_id', None)
return redirect(url_for('index'))
##########################################
#### Fetching the user Json Information ##
#### Note ::-- Use this for the user Profile Info
##########################################
@app.route('/user')
def user():
UserDict = github.get('user') # This returns a dictionary
# Try seeing it in the browser using jsonify()
#avatar = UserDict['location']
return jsonify(UserDict)
#############################################
#### Explore Page
#### Need to dynamically generate the cards
#############################################
@app.route('/explore/<string:user>/<string:gig>',methods=["POST","GET"])
def dynamic_page(user,gig):
if request.method == "GET":
#check if the user exists and the gig exists
# if so then link is valid.
# then get all the user details and populate the page of showgig
#return user+' '+gig
users = mongo.db.user
data= users.find_one({'name': user})
gigs= data['gigs']
if gig !="" and user != "":
if gig in gigs:
#This means that the gig exists
index = gigs.index(gig)
embed_code= data['youtube'][index]
fileContents = data['MDcontents'][index]
avatar = data['avatar']
location = data['location']
email = data['email']
projectname = gig
explorelinks = mongo.db.explorelinks
## query the explore links database
data1 = explorelinks.find_one({'gigs':gig})
likes = data1['thumbsup']
return render_template('showgig.html',data = embed_code\
,markdownData=fileContents,projectname=projectname\
,avatar=avatar,location=location,email=email,user=user,key=stripe_keys['publishable_key'],likes=likes,view=1)
else:
#This is not a valid project
return render_template('notfound.html', pic='/static/404.png'), 404
if request.method == "POST":
# This has been done to increase the like counter for the project
users = mongo.db.user
data= users.find_one({'name': user})
gigs= data['gigs']
if gig !="" and user != "":
if gig in gigs:
#This means that the gig exists
index = gigs.index(gig)
embed_code= data['youtube'][index]
fileContents = data['MDcontents'][index]
avatar = data['avatar']
location = data['location']
projectname = gig
explorelinks = mongo.db.explorelinks
## query the explore links database
data1 = explorelinks.find_one({'gigs':gig})
## changes made here
likes = data1['thumbsup']
#incrementing the like counter
likes = int(likes) + 1
#updating the like counter in mongodb
explorelinks.update({'gigs':gig},{'$set':{'thumbsup':likes}})
return render_template('showgig.html',data = embed_code\
,markdownData=fileContents,projectname=projectname\
,avatar=avatar,location=location,user=user,key=stripe_keys['publishable_key'],likes=likes)
else:
#This is not a valid project
return render_template('notfound.html', pic='/static/404.png'), 404
@app.route('/explore',methods=["GET"])
def explore():
#this is another collection that contains the the links of projects to explore
explore_links = mongo.db.explorelinks.find().limit(12)
description = []
gigs = []
links = []
embed_code= []
# we need to iterate mongodb cursors in this particular way only
for record in explore_links:
#desc = record['description']
description.append(record['description'])
gigs.append(record['gigs'])
links.append(record['link'])
embed_code.append(record['embed_code'])
return render_template('explore.html',video=embed_code,title = gigs, description=description,link=links,length=len(links))
#############################################
## To tackle bad content requests
#############################################
@app.errorhandler(404)
def not_found_error(error):
return render_template('notfound.html', pic='/static/404.png'), 404
if __name__ == '__main__':
init_db()
app.run(debug=True)