PageRenderTime 28ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/app/controllers/settings.py

https://bitbucket.org/liutaihua/yyu
Python | 308 lines | 66 code | 61 blank | 181 comment | 5 complexity | f62c627a7f4bf33a6ab0b9b2f8bc2bd2 MD5 | raw file
  1. #!/usr/bin/env python
  2. # coding: utf-8
  3. import web
  4. import config
  5. import md5
  6. import os
  7. import Image
  8. import time
  9. import datetime
  10. import cgi
  11. import random
  12. import hashlib
  13. from web import form
  14. from app.models import applicants, postModel, users, notification
  15. from app.common import session, utils
  16. from app.common import email_templates
  17. from config import view, site_name, encryption_key
  18. siteName = site_name
  19. user = session.get_session()
  20. # password_form = form.Form(
  21. # form.Password('password',
  22. # form.notnull,
  23. # form.Validator('至少6个字符',
  24. # lambda x: users.is_valid_password(x)),
  25. # description='你的新密码:'),
  26. # form.Button('submit', type='submit', value='Change password')
  27. # )
  28. # nickname_form = form.Form(
  29. # form.Textbox('nickname',
  30. # form.notnull,
  31. # description='你的新昵称:'),
  32. # form.Button('submit', type='submit', value='Change your nickname')
  33. # )
  34. # vemail = form.regexp(r'.+@.+', '请检查你的邮箱格式')
  35. # email_form = form.Form(
  36. # form.Textbox('email',
  37. # form.notnull, vemail,
  38. # form.Validator('这个已经被占用了.',
  39. # lambda x: users.is_email_available(x)),
  40. # description='你的新邮箱:'),
  41. # form.Button('submit', type='submit', value='Change your email')
  42. # )
  43. # def render_settings(nickname_form=nickname_form(), email_form=email_form(), password_form=password_form(), on_success_message=''):
  44. # #counts = applicants.get_counts()
  45. # username = str(user.username)
  46. # u = users.get_user_by_username(username)
  47. # # if not u.avatarPath:
  48. # # src = '/static/public/img/default_48x48.jpg'
  49. # # else:
  50. # # src = '/static/upload/image' + avatar + '_48.jpg'
  51. # return view.base(
  52. # view.settings(user, nickname_form, email_form, password_form, on_success_message, u), user, site_name
  53. # )
  54. class index:
  55. @session.login_required
  56. def GET(self):
  57. # u = users.get_user_by_id(session.get_user_id())
  58. # ut = u.nicknameChangeTime #得到上次修改昵称的时间
  59. # c = datetime.datetime.now()#得到当前时间
  60. # if (c - ut).days < 30: #时间差
  61. # can_change_nickname = False
  62. # else:
  63. # can_change_nickname = True
  64. #得到权限
  65. per = users.get_permission_by_douid(user.douban_id)
  66. rights = per[0].rights
  67. #得到提醒
  68. notification_results, notification_num = notification.get_unread_notification(user.id)
  69. #得到@提醒
  70. notification_mention_results, mention_num= notification.get_unread_metion_notifition(user.id)
  71. #链表 得到提醒的详细id\名称等
  72. ntf_posts = []
  73. ntf_users = []
  74. mtf_posts = []
  75. mtf_users = []
  76. ntf_list = notification_results.list()
  77. mtf_list = notification_mention_results.list()
  78. for x in xrange(len(ntf_list)):
  79. ntf_posts += postModel.getPostsByPostId(ntf_list[x].pid)
  80. ntf_users += users.get_users_by_id(ntf_list[x].uid)
  81. for x in xrange(len(mtf_list)):
  82. mtf_posts += postModel.getPostsByPostId(mtf_list[x].pid)
  83. mtf_users += users.get_users_by_id(mtf_list[x].uid)
  84. ntf_list = ntf_list + mtf_list
  85. ntf_posts = ntf_posts + mtf_posts
  86. ntf_users = ntf_users + mtf_users
  87. notification_num = notification_num+mention_num
  88. #得到资料设置
  89. profile = users.get_profile_by_user_id(user.id)
  90. #得到邮箱验证状态
  91. confirm = users.get_confirm_email_by_douban_id(user.douban_id)
  92. return view.base(view.member_setting_profile(user, profile, confirm), user, siteName, rights, ntf_list, notification_num, ntf_posts, ntf_users)
  93. # @session.login_required
  94. # def POST(self):
  95. # f = web.input()
  96. # #判断是更新还是新增
  97. # if users.is_user_profile_changed(session.get_user_id()):
  98. # users.update_profile(session.get_user_id(), **f)
  99. # else:
  100. # users.insert_profile(session.get_user_id(), **f)
  101. # session.reset()
  102. # raise web.seeother('/settings')
  103. # class change_nickname:
  104. # @session.login_required
  105. # def POST(self):
  106. # f = web.input()
  107. # time = datetime.datetime.now()
  108. # users.update(session.get_user_id(), nickname=f.nickname, nicknameChangeTime = time)
  109. # session.reset()
  110. class change_email:
  111. # @session.login_required
  112. # def GET(self):
  113. # e = users.get_confirm_email_by_email(user.email)
  114. # c = e.get('confirmed')
  115. # if c == 1:
  116. # return view.base(view.member_setting_email(user, msg='<span class="label label-success">已验证</lable>'), user, siteName)
  117. # else:
  118. # return view.base(view.member_setting_email(user, msg='<span class="label label-important">未验证</span>'), user, siteName)
  119. @session.login_required
  120. def POST(self):
  121. email = web.input().email
  122. #检查是否变动
  123. old_email = users.get_user_by_id(user.id).email
  124. if email == old_email:
  125. return '{"status":"n", "code":"n-wbh", "info":"邮箱地址未变化"}'
  126. #检查新邮箱是否已经存在
  127. elif not users.is_email_available(email):
  128. return '{"status":"n", "code":"n-ybsy", "info":"此邮箱已被使用"}'
  129. else:
  130. #更新用户表中email字段
  131. # users.update(user.id, email=email)
  132. token = md5.md5(time.ctime() + email).hexdigest()
  133. t = datetime.datetime.now()
  134. #更新邮箱验证表中email\confirmed\token字段
  135. users.update_confirm_email_by_douban_id(user.douban_id, email, token, t)
  136. #发送通知邮件
  137. email_templates.change_email(email, token)
  138. print '======email send======'
  139. # session.reset()
  140. return '{"status":"y", "code":"y-y", "info":"验证邮件已发送,请通过邮件中的链接来验证此邮箱。在未验证之前,提醒通知还是发到旧邮箱中。"}'
  141. # class change_password:
  142. # @session.login_required
  143. # def GET(self):
  144. # return view.base(view.member_setting_password(user), user, siteName)
  145. # @session.login_required
  146. # def POST(self):
  147. # data = web.input()
  148. # email = data.email
  149. # id = data.id
  150. # password = data.password
  151. # newPassword = data.newPassword
  152. # user = users.get_user_by_id(id)
  153. # ua = web.ctx.env.get('HTTP_USER_AGENT')
  154. # ip = web.ctx.ip
  155. # timestamp = time.mktime(time.localtime())
  156. # tm = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(timestamp))
  157. # if users.is_correct_password(email, password):
  158. # users.update(id, password=hashlib.md5(newPassword + encryption_key).hexdigest())
  159. # session.reset()
  160. # email_templates.change_pwd(email, user, ua, ip, tm)
  161. # web.header('Content-Type', 'application/json')
  162. # return '{"status":"y"}'
  163. # else :
  164. # web.header('Content-Type', 'application/json')
  165. # return '{"status":"n"}'
  166. # class member_avatar:
  167. # @session.login_required
  168. # def GET(self):
  169. # return view.base(view.member_setting_avatar(user, mid_src=''), user, siteName)
  170. # @session.login_required
  171. # def POST(self):
  172. # cgi.maxlen = 2 * 1024 * 1024 # 限制2MB
  173. # try:
  174. # x = web.input(uploadImg={})
  175. # homedir = os.getcwd()
  176. # filedir = '%s/static/upload/image' %homedir #图片存放路径
  177. # if 'uploadImg' in x: # to check if the file-object is created
  178. # filepath = x.uploadImg.filename.replace('\\','/') # replaces the windows-style slashes with linux ones.
  179. # filename = filepath.split('/')[-1] # splits the and chooses the last part (the filename with extension) #获取文件名
  180. # ext = filename.split('.', -1)[-1] #获取后缀
  181. # if ext == 'jpg' or ext == 'gif' or ext == 'jpeg' or ext == 'png' or ext == 'JPG':
  182. # now = datetime.datetime.now()
  183. # d_path = filedir + '/%d/%d/%d' %(now.year, now.month, now.day)
  184. # if not os.path.exists(d_path):
  185. # os.makedirs(d_path) #创建当前日期目录
  186. # t = '%d%d%d%d%d%d' %(now.year, now.month, now.day, now.hour, now.minute, now.second)
  187. # all = list('0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSQUVWXYZ')
  188. # randStr = ''
  189. # for i in range(10):
  190. # index = random.randint(0,len(all)-1)
  191. # randStr = randStr + all[index] #生成10位随机数
  192. # authKey = hashlib.md5(randStr + user.username).hexdigest()
  193. # filename = t + '_' + authKey + '.' + ext #以时间+authKey作为文件名
  194. # fout = open(d_path + '/' + filename,'wb') # creates the file where the uploaded file should be stored
  195. # fout.write(x.uploadImg.file.read()) # writes the uploaded file to the newly created file.
  196. # fout.close() # closes the file, upload complete.
  197. # im = Image.open(d_path + '/' + filename)
  198. # width, height = im.size #判断比例
  199. # if width/height > 5 or height/width > 5 :
  200. # os.remove(d_path + '/' + filename) #删除图片
  201. # #session.reset()
  202. # return view.base(view.member_setting_avatar(user, mid_src='', message='图片的比例有些不太合适,请选择一张更容易辨识的图片吧'), user, siteName)
  203. # else:
  204. # path = d_path + '/' + filename #for thumb
  205. # utils.make_thumb(path) #创建缩略图
  206. # #big_src = '/static/upload/image/%d/%d/%d/' %(now.year, now.month, now.day) + filename
  207. # mid_src = '/static/upload/image/%d/%d/%d/' %(now.year, now.month, now.day) + t + '_' + authKey + '_160.jpg'
  208. # #sml_src = '/static/upload/image/%d/%d/%d/' %(now.year, now.month, now.day) + t + '_' + authKey + '_48x48.jpg'
  209. # user_id = user.id
  210. # avatar = '/%d/%d/%d/' %(now.year, now.month, now.day) + t + '_' + authKey
  211. # users.save_user_avatar(user_id, avatar)#入库
  212. # #session.reset()
  213. # return view.base(view.member_setting_avatar(user, mid_src), user, siteName)
  214. # else:
  215. # #session.reset()
  216. # return view.base(view.member_setting_avatar(
  217. # user, mid_src='',
  218. # message='上传格式仅支持jpg/png/gif/jpeg'), user, siteName)
  219. # except ValueError:
  220. # #session.reset()
  221. # return view.base(view.member_setting_avatar(
  222. # user, mid_src='',
  223. # message='文件太大了,我吃不消哇~'), user, siteName)
  224. # class member_avatar_crop:
  225. # @session.login_required
  226. # def POST(self):
  227. # crop_data = web.input()
  228. # path = crop_data['path']
  229. # x = crop_data['x']
  230. # y = crop_data['y']
  231. # w = crop_data['w']
  232. # h = crop_data['h']
  233. # s = utils.make_thumb_crop(path, x, y, w, h)
  234. # session.reset()
  235. # raise web.seeother('/settings')
  236. # # return view.base(view.member_setting_avatar(
  237. # # user, mid_src='',
  238. # # message='头像更新成功~'), user, siteName)
  239. # #return view.test(path, x, y, w, h, s)
  240. #邮箱提醒开启/关闭
  241. class email_subscribe:
  242. @session.login_required
  243. def POST(self):
  244. action = web.input().subscribe
  245. users.email_subscribe(user.id, action)
  246. s = 's'
  247. return s