wodewish / kcbuser.py

# -*- coding: utf-8 -*-
import re,urllib2
import os
from google.appengine.ext.webapp import template
from google.appengine.ext import db
import cgi
from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
import datetime
import time
from datetime import datetime, timedelta, date

def getCNnow():
	return datetime.utcnow() + timedelta(hours=+8)

def f(x,y):
		list=[]
		for i in range(int(x),int(y)+1):
				list.append(i)
		return list
def rkw(kw):
	skw=[]
	for s in kw:
		s=s.split(" ")
		n=[]
		for i in s:
			i=i.split("-")
			n.append(i)

		s=[]
		for i in n:
			if len(i)==1:
				s.extend(f(int(i[0]),int(i[0])))
			else:
				s.extend(f(int(i[0]),int(i[1])))
		skw.append(s)
	return skw


class kcbuser(db.Model):
	user= db.StringProperty()
	name=db.StringProperty(multiline=True)
	url = db.StringProperty(multiline=True)
	signuptime = db.DateTimeProperty(auto_now=True)
	public=db.IntegerProperty(default=0)
	nowterm=db.StringProperty()

class kcbterm(db.Model):
	user= db.ReferenceProperty(kcbuser)
	name=db.StringProperty()
	weight=db.IntegerProperty()


class kcb(db.Model):
	name = db.StringProperty(multiline=True)
	term = db.ReferenceProperty(kcbterm)
	settime = db.DateTimeProperty()
	comtime = db.DateTimeProperty()
	type=db.BooleanProperty()







class AdminMainPage(webapp.RequestHandler):
	def get(self):
		global user,USER,TERMS
		user=str(users.get_current_user())
		USER=db.GqlQuery('SELECT __key__ FROM kcbuser WHERE user = :1', user.lower())
		user=USER[0]
		URL=db.get(user).url
		try:
			NowTerm=db.get(db.get(user).nowterm)
			NoTerm=None
		except:
			NowTerm=None
			NoTerm=u'''当前没有列表'''
		try:
			TERMS=db.get(user).kcbterm_set
		except AttributeError:
			TERMS=None

		try:
			# KCBs1=NowTerm.kcb_set
			KCBs=NowTerm.kcb_set
		except AttributeError:
			KCBs=None



		template_values = {
		'KCBs': KCBs,
		'TERMS':TERMS,
		'TERM':NowTerm,
		'USER':users.get_current_user(),
		'URL':URL,
		'NOTERM':NoTerm,
		'NAME':db.get(user).name,
		}
		path = os.path.join(os.path.dirname(__file__), 'tpl','admin.html')
		self.response.out.write(template.render(path, template_values))
class AddItemHandler(webapp.RequestHandler):
	def post(self):
		user=str(users.get_current_user())
		USER=db.GqlQuery('SELECT __key__ FROM kcbuser WHERE user = :1', user.lower())
		if USER.count()>0:
			user=USER[0]
			NowTerm=db.get(db.get(user).nowterm)
			for i in cgi.escape(self.request.get('additem')).split('\n'):
				# self.response.out.write(i+'ssss')
				KCB = kcb()
				KCB.name=i
				KCB.term = NowTerm
				KCB.type=True
				KCB.settime=getCNnow()
				KCB.put()
			self.redirect('/admin')
		else:
			self.redirect('/signup')
class DeleteHandler(webapp.RequestHandler):
	def post(self):
		delitem=self.request.get_all('delitem')
		for item in delitem:
			db.get(item).delete()

		self.redirect('/admin')


class LoginRouter(webapp.RequestHandler):
  def get(self):
		user=str(users.get_current_user())
		USER=db.GqlQuery('SELECT __key__ FROM kcbuser WHERE user = :1', user.lower())
		if USER.count() > 0:
			try:
				db.get(USER[0]).kcbterm_set[0].name
				self.redirect('/admin')
			except IndexError:
				self.redirect(users.create_login_url('/admin/deep'))
		else:
			self.redirect('/signup')

class LogoutHandler(webapp.RequestHandler):
  def get(self):
    self.redirect(users.create_logout_url('/'))
class SignupHandler(webapp.RequestHandler):
  def post(self):
		NEWUSER=kcbuser()
		NEWUSER.user=str(users.get_current_user())
		URL=self.request.get('url')
		qURL=db.GqlQuery('SELECT __key__ FROM kcbuser WHERE url = :1', URL.lower())
		if qURL.count() > 0:
			self.redirect('/errorsameurl')
		else:
			NEWUSER.name=self.request.get('name')
			NEWUSER.url=self.request.get('url')
			NEWUSER.public=int(self.request.get('public'))
			NEWUSER.nowterm='agdrZS1iaWFvcg4LEgdrY2J0ZXJtGK0RDA'
			NEWUSER.put()
			self.redirect('/admin/deep')
class SignupPage(webapp.RequestHandler):
  def get(self):
	  template_values = {
	  'USERNAME':users.get_current_user(),
	  }
	  path = os.path.join(os.path.dirname(__file__), 'tpl','signup.html')
	  self.response.out.write(template.render(path, template_values))
class errorsameurl(webapp.RequestHandler):
  def get(self):
	errormsg='你输入的shorturl已经被注册,或者未选择公开状态'
	template_values = {

	'errormsg':errormsg,
	}
	path = os.path.join(os.path.dirname(__file__), 'tpl','error.html')
	self.response.out.write(template.render(path, template_values))
class ChangeUrlHandler(webapp.RequestHandler):
	def post(self):
		URL=self.request.get('url')
		qURL=db.GqlQuery('SELECT __key__ FROM kcbuser WHERE url = :1', URL.lower())
		if qURL.count() > 0:
			self.redirect('/errorsameurl')
		else:
			NEWUSER=db.get(user)
			NEWUSER.url=URL
			NEWUSER.put()
			self.redirect('/admin')
class ChangePublicHandler(webapp.RequestHandler):
	def post(self):
		PUBLIC=int(self.request.get('public'))
		NEWUSER=db.get(user)
		NEWUSER.public=PUBLIC
		NEWUSER.put()
		self.redirect('/admin')

class DeepAdminMainPage(webapp.RequestHandler):
	def get(self):
		user=str(users.get_current_user())
		USER=db.GqlQuery('SELECT __key__ FROM kcbuser WHERE user = :1', user.lower())
		user=USER[0]
		URL=db.get(user).url
		KCBs=db.GqlQuery('SELECT * FROM kcb WHERE user = :1', USER[0])
		template_values = {
		'KCBs': KCBs,
		'USER':users.get_current_user(),
		'URL':URL,
		}
		path = os.path.join(os.path.dirname(__file__), 'tpl','deepadmin.html')
		self.response.out.write(template.render(path, template_values))
class AddTermHandler(webapp.RequestHandler):
	def post(self):
		user=str(users.get_current_user())
		USER=db.GqlQuery('SELECT __key__ FROM kcbuser WHERE user = :1', user.lower())
		user=USER[0]
		NEWTERM=kcbterm()
		NEWTERM.name=self.request.get('termname')
		NEWTERM.user=user
		NEWTERM.weight=999
		NEWTERM.put()
		NOWUSER=db.get(user)
		NOWUSER.nowterm=str(NEWTERM.key())
		NOWUSER.put()
		self.redirect("/admin")


class ChangeTermHandler(webapp.RequestHandler):
	def post(self):
		user=str(users.get_current_user())
		USER=db.GqlQuery('SELECT __key__ FROM kcbuser WHERE user = :1', user.lower())
		user=USER[0]
		TERM=self.request.get('nowterm')
		NEWUSER=db.get(user)
		NEWUSER.nowterm=TERM
		NEWUSER.put()
		self.redirect('/admin')

class ChangeTermNameHandler(webapp.RequestHandler):
	def post(self):
		TERMNAME=self.request.get('termname')
		TERM=db.get(self.request.get('termkey'))
		TERM.name=TERMNAME
		TERM.put()
		self.redirect('/admin')

class ChangeTermWeightHandler(webapp.RequestHandler):
	def post(self):
		TERMNAME=self.request.get('termweight')
		TERM=db.get(self.request.get('termkey'))
		TERM.weight=int(TERMNAME)
		TERM.put()
		self.redirect('/admin')
class SetHandler(webapp.RequestHandler):
	def post(self):
		TERM=db.get(self.request.get('termkey'))
		TERM.type=True
		TERM.put()
		self.redirect('/admin')
class ComHandler(webapp.RequestHandler):
	def post(self):
		TERM=db.get(self.request.get('termkey'))
		TERM.type=False
		TERM.comtime=getCNnow()
		TERM.put()
		self.redirect('/admin')

application = webapp.WSGIApplication(
										[
										('/admin', AdminMainPage),
										('/admin/add', AddItemHandler),
										('/admin/del',DeleteHandler),
										('/signin', LoginRouter),
									 ('/signout', LogoutHandler),
									 ('/signup', SignupPage),
									 ('/signup/done', SignupHandler),
									 ('/errorsameurl',errorsameurl),
										('/admin/url',ChangeUrlHandler),
										('/admin/public',ChangePublicHandler),
										('/admin/term',ChangeTermHandler),
										('/admin/termname',ChangeTermNameHandler),
										('/admin/termweight',ChangeTermWeightHandler),
										('/admin/deep', DeepAdminMainPage),
										('/admin/deep/addterm', AddTermHandler),
										('/admin/delterm',DeleteHandler),
										('/admin/set',SetHandler),
										('/admin/com',ComHandler),
],
                                     debug=True)

def main():
	run_wsgi_app(application)

if __name__ == "__main__":
  main()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.