# -*- coding: utf-8 -*-
"""
    demeter web page
    name:site.py 站点相关
    author:rabin
"""
from .__load__ import *

class site_path(Load):
	@Web.auth
	@Web.setting
	def get(self):
		self.set(
			name = u'站点管理'
			,path = '/site/site'
			,width = '600'
			,height = '600'
			,search = (('label-1','workdate-time-start','workdate-time-end','name-input-mlike'), (u'日期范围',u'开始时间',u'截止时间',u'站点名称'))
			,thead = (u'站点名称', u'快捷功能', u'授权信息', u'授权时间')
			,tbody = ('name', 'func', 'app', 'time')
			,state = True
		)
		menu = (
			{'name':'转换文件列表', 'url':'/site/convert'}
			,
			)
		self.list('site')
		if self.data['list']:
			for key, value in enumerate(self.data['list']):
				id = str(value['id'])
				self.data['list'][key]['time'] = Demeter.date(value['sdate']) + ' 至 ' + Demeter.date(value['edate'])
				self.data['list'][key]['app'] = u'[appid]：' + value['appid'] + u'<br />[appsecret]：' + value['appsecret']
				param = '?search_site_id-select-=' + id
				self.data['list'][key]['func'] = ''
				for i in menu:
					self.data['list'][key]['func'] = self.data['list'][key]['func'] + '<a href="'+i['url']+''+param+'">'+i['name']+'</a>&nbsp;&nbsp;&nbsp;&nbsp;'
		self.show('list')

class site_update_path(Load):
	@Web.auth
	@Web.setting
	def get(self):
		self.set(
			path = '/site/site'
			,label = (u'站点名称',u'站点网址',u'通知接口',u'限制页数',u'开始时间', u'结束时间')
			,update = ('name-input-required','link-input-required','api-input-required','page-input-required','sdate-date-required','edate-date-required')
		)
		self.one('site')
		self.show('update')
	@Web.auth
	@Web.setting
	def post(self):
		id = self.input('id')
		if not id:
			self.getAppId()
		else:
			info = self.service('common').one('site', id=id)
			if not info['appid']:
				self.getAppId()
		self.update('site')
	@Web.auth
	@Web.setting
	def delete(self):
		self.drop('site')
	@Web.auth
	@Web.setting
	def getAppId(self):
		self.data['update']['appid'] = Demeter.compressUuid(Demeter.uuid('convert'))
		self.data['update']['appsecret'] = Demeter.hash()

		model = Demeter.model('site')
		model.appid = self.data['update']['appid']
		model.appsecret = self.data['update']['appsecret']
		info = model.select(type='fetchone')
		if info:
			self.getAppId()

class convert_path(Load):
	@Web.auth
	@Web.setting
	def get(self):
		self.set(
			name = u'转换的文件'
			,path = '/site/convert'
			,width = '600'
			,height = '600'
			,edit = False
			,search = (('label-1','cdate-time-start','cdate-time-end','site_id-select-','status-select-','name-input-mlike','url-input-mlike','file_id-input-'), (u'日期范围',u'开始时间',u'截止时间',u'选择站点',u'选择状态',u'源文件名',u'新文件名',u'文件ID'))
			,thead = (u'文件id',u'所属站点', u'源文件', u'文件名', u'访问路径', u'状态', u'更新时间')
			,tbody = ('id','site', 'file','name', 'url', 'status', 'cdate')
			,state = True
		)
		self.data['common']['search_site_id-select-'] = self.service('common').list('site')
		self.data['common']['search_status-select-'] = [{'id':1,'value':1,'name':u'待机'},{'id':2,'value':2, 'name':u'转换中'},{'id':3,'value':3,'name':u'转换完成'},{'id':4,'value':4,'name':u'转换失败'}]

		self.list('convert')
		status = {}
		status[1] = '待机'
		status[2] = '转换中'
		status[3] = '转换完成'
		status[4] = '转换失败'
		if self.data['list']:
			for key, value in enumerate(self.data['list']):
				site = self.service('common').one('site', id=value['site_id'])
				self.data['list'][key]['site'] = site['name']
				if value['status'] == 3:
					self.data['list'][key]['url'] = '<a href="'+str(value['url']).replace('.html', '.pdf')+'" target="_blank">[点此访问]</a>'
				else:
					self.data['list'][key]['url'] = '无法访问'
				self.data['list'][key]['status'] = status[value['status']]

				if 'file_id' in value:
					self.data['list'][key]['file'] = value['file'] + ':' + value['file_id']

		self.show('list')

class convert_update_path(Load):
	@Web.auth
	@Web.setting
	def get(self):
		status = [
			{'id':'1', 'name': '待机'},
		]
		self.set(
			path = '/site/convert'
			,label = (u'所属站点', u'源文件路径', u'状态')
			,update = ('site_id-select-required', 'file-input-required', 'status-select-')
			,update_site_id = self.service('common').list('site')
			,update_status = status
		)
		self.one('convert')
		self.show('update')
	@Web.auth
	@Web.setting
	def post(self):
		model = Demeter.model('site')
		model.id = self.data['update']['site_id']
		site = model.select(type='fetchone')


		info = self.service('convert').getFile(site['key'], self.data['update']['file'])
		self.data['update']['uid'] = -1
		self.data['update']['file_id'] = -1
		self.data['update']['file_type'] = 2
		self.data['update']['name'] = info['name']
		self.data['update']['key'] = info['key']
		self.data['update']['ext'] = info['ext']

		self.data['update']['local'] = info['local']		
		self.data['update']['path'] = info['path']
		self.data['update']['pdf'] = info['pdf']
		self.data['update']['html'] = info['html']
		self.data['update']['url'] = info['url']

		sid = self.update('convert')

		if sid:
			redis = Demeter.redis()
			config = Demeter.config['redis']
			redis.rpush(config['name'], sid)
	@Web.auth
	@Web.setting
	def delete(self):
		self.drop('convert')