<commit_before># coding: utf-8

from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext.webapp import template
from google.appengine.api import users
from google.appengine.ext import db

import gdata.service
import gdata.alt.appengine
import gdata.photos.service
import gdata.calendar.service
import gdata.docs.client

from StringIO import StringIO
from helpers import html_unescape
import os, sys, zipfile, re, models

sys.setrecursionlimit(10000) # SDK fix

class Page(webapp.RequestHandler):
	services = {
		'Picasa': {
			'scope': 'http://picasaweb.google.com/data/',
			'purpose': 'Photos',
			'client': gdata.photos.service.PhotosService
		},
		'Calendar': {
			'scope': 'http://www.google.com/calendar/feeds/',
			'purpose': 'Events',
			'client': gdata.calendar.service.CalendarService
		},
		'Docs': {
			'scope': 'http://docs.google.com/feeds/',
			'purpose': 'Notes',
			'client': gdata.docs.client.DocsClient
		}
	}

	def __init__(self):
		self.user = users.get_current_user()

		if self.user:
			self.url = users.create_logout_url('/')
			self.is_logged = True
		else:
			self.url = users.create_login_url('/services')
			self.is_logged = False

		self.values = {
			'user': self.user,
			'url': self.url,
			'is_logged': self.is_logged
		}

	def render(self, file, values = None):
		self.response.headers['Content-Type'] = 'text/html'

		path = os.path.join(os.path.dirname(__file__), 'templates/%s' % file)
		self.response.out.write(template.render(path, values if values else self.values))


class MainPage(Page):
	def get(self):
		"""albums = self.client.GetUserFeed(user=self.user)
		for album in albums.entry:
			self.response.out.write('title: %s, number of photos: %s, id: %s' % (album.title.text,
				album.numphotos.text, album.gphoto_id.text))"""

		if self.is_logged:
			self.render('upload.html')
		else:
			self.render('index.html')


class ServicesPage(Page):
	def get(self):
		values = self.values.copy()
		values['services'] = []
		for service in self.services:
			values['services'].append({
				'name': service,
				'purpose': self.services[service]['purpose']
			})

		self.render('services.html', values)


	def post(self):
		selected_services = self.request.POST.getall('services')

		user_info = models.User.gql('WHERE name=:1', self.user).get()
		if not user_info:
			user_info = models.User()

		gdata_client = gdata.service.GDataService()
		gdata.alt.appengine.run_on_appengine(gdata_client)

		hostname = os.environ['SERVER_NAME']
		port = os.environ['SERVER_PORT']
		if port and port != '80':
			hostname = hostname + ':' + port
		save_token_url = 'http://' + hostname + '/token'

		scopes = []
		user_info.services = []
		for service in selected_services:
			user_info.services.append(service)
			scopes.append(self.services[service]['scope'])

		if scopes:
			user_info.put()
			self.redirect(gdata_client.GenerateAuthSubURL(save_token_url, scopes, secure = False, session = True).to_string())
		else:
			self.redirect('/services')


class TokenPage(Page):
	def get(self):
		gdata_client = gdata.service.GDataService()
		gdata.alt.appengine.run_on_appengine(gdata_client)

		auth_token = gdata.auth.extract_auth_sub_token_from_url(self.request.uri)
		if not auth_token:
			self.redirect('/services')

		session_token = gdata_client.upgrade_to_session_token(auth_token)
		if not session_token:
			self.redirect('/services')

		gdata_client.token_store.add_token(session_token)
		self.redirect('/upload')

class UploadPage(Page):
	def get(self):
		#TODO: Check if the user is logged in and has authenticated the App for at least one service
		#if self.user:
		self.render('upload.html')
		#else:
		#	self.redirect('/')

	def post(self):
		archive = self.request.get("fbContents")<commit_msg>Made the render method more elegant.<commit_after># coding: utf-8

from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext.webapp import template
from google.appengine.api import users
from google.appengine.ext import db

import gdata.service
import gdata.alt.appengine
import gdata.photos.service
import gdata.calendar.service
import gdata.docs.client

from StringIO import StringIO
from helpers import html_unescape
import os, sys, zipfile, re, models

sys.setrecursionlimit(10000) # SDK fix

class Page(webapp.RequestHandler):
	services = {
		'Picasa': {
			'scope': 'http://picasaweb.google.com/data/',
			'purpose': 'Photos',
			'client': gdata.photos.service.PhotosService
		},
		'Calendar': {
			'scope': 'http://www.google.com/calendar/feeds/',
			'purpose': 'Events',
			'client': gdata.calendar.service.CalendarService
		},
		'Docs': {
			'scope': 'http://docs.google.com/feeds/',
			'purpose': 'Notes',
			'client': gdata.docs.client.DocsClient
		}
	}

	def __init__(self):
		self.user = users.get_current_user()

		if self.user:
			self.url = users.create_logout_url('/')
			self.is_logged = True
		else:
			self.url = users.create_login_url('/services')
			self.is_logged = False

		self.values = {
			'user': self.user,
			'url': self.url,
			'is_logged': self.is_logged
		}

	def render(self, file, values = None):
		self.response.headers['Content-Type'] = 'text/html'

		path = os.path.join(os.path.dirname(__file__), 'templates/%s.html' % file)
		self.response.out.write(template.render(path, values if values else self.values))


class MainPage(Page):
	def get(self):
		"""albums = self.client.GetUserFeed(user=self.user)
		for album in albums.entry:
			self.response.out.write('title: %s, number of photos: %s, id: %s' % (album.title.text,
				album.numphotos.text, album.gphoto_id.text))"""

		if self.is_logged:
			self.render('upload')
		else:
			self.render('index')


class ServicesPage(Page):
	def get(self):
		values = self.values.copy()
		values['services'] = []
		for service in self.services:
			values['services'].append({
				'name': service,
				'purpose': self.services[service]['purpose']
			})

		self.render('services', values)


	def post(self):
		selected_services = self.request.POST.getall('services')

		user_info = models.User.gql('WHERE name=:1', self.user).get()
		if not user_info:
			user_info = models.User()

		gdata_client = gdata.service.GDataService()
		gdata.alt.appengine.run_on_appengine(gdata_client)

		hostname = os.environ['SERVER_NAME']
		port = os.environ['SERVER_PORT']
		if port and port != '80':
			hostname = hostname + ':' + port
		save_token_url = 'http://' + hostname + '/token'

		scopes = []
		user_info.services = []
		for service in selected_services:
			user_info.services.append(service)
			scopes.append(self.services[service]['scope'])

		if scopes:
			user_info.put()
			self.redirect(gdata_client.GenerateAuthSubURL(save_token_url, scopes, secure = False, session = True).to_string())
		else:
			self.redirect('/services')


class TokenPage(Page):
	def get(self):
		gdata_client = gdata.service.GDataService()
		gdata.alt.appengine.run_on_appengine(gdata_client)

		auth_token = gdata.auth.extract_auth_sub_token_from_url(self.request.uri)
		if not auth_token:
			self.redirect('/services')

		session_token = gdata_client.upgrade_to_session_token(auth_token)
		if not session_token:
			self.redirect('/services')

		gdata_client.token_store.add_token(session_token)
		self.redirect('/upload')

class UploadPage(Page):
	def get(self):
		#TODO: Check if the user is logged in and has authenticated the App for at least one service
		#if self.user:
		self.render('upload')
		#else:
		#	self.redirect('/')

	def post(self):
		archive = self.request.get("fbContents")
{'fraction_non_alphanumeric': 0.14353312302839116, 'fraction_numerical': 0.002365930599369085, 'mean_word_length': 4.306136680613668, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '3148151', 'n_tokens_mistral': 2925, 'n_tokens_neox': 2613, 'n_words': 524}