def upload_img(file_data, img_type='other', file_name=None): # extra = qiniu.io.PutExtra() # extra.mime_type = "image/jpeg" # data 可以是str或readable对象 if isinstance(file_data, (unicode, str)): data = StringIO.StringIO(file_data) else: data = StringIO.StringIO(file_data.read()) uptoken = get_upload_token(img_type=img_type) key = '%s_%s' % (img_type, file_name or utils.uuid_without_dash()) ret, err = qiniu.io.put(uptoken, key, data) if err is not None: logging.error('upload_img error is:%s\n ret is %s' % (err, ret)) return False, err key = ret.get('key', '') # 编辑器上传图片最大宽度为600 if img_type == 'editor': key += '!600m0' # if int(ret.get('w', 0)) > 600: # key += '!600m0' return True, key
def upload_img(file_data, img_type='other', file_name=None): # extra = qiniu.io.PutExtra() # extra.mime_type = "image/jpeg" # data 可以是str或readable对象 if isinstance(file_data, (unicode, str)): data = StringIO.StringIO(file_data) else: data = StringIO.StringIO(file_data.read()) uptoken = get_upload_token(img_type=img_type) key = '%s_%s' % (img_type, file_name or utils.uuid_without_dash()) ret, err = qiniu.io.put(uptoken, key, data) if err is not None: logging.error('upload_img error is:%s\n ret is %s' % (err, ret)) return False, err key = ret.get('key', '') # 编辑器上传图片最大宽度为600 if img_type == 'editor': key += '!600m0' # if int(ret.get('w', 0)) > 600: # key += '!600m0' return True, key
def send_forget_password_email(self, email): ''' @note: 发送密码找回邮件 ''' if not email: return 99800, dict_err.get(99800) user = self.get_user_by_email(email) if not user: return 10111, dict_err.get(10111) cache_obj = cache.Cache() key = u'forget_password_email_code_%s' % email code = cache_obj.get(key) if not code: code = utils.uuid_without_dash() cache_obj.set(key, code, time_out=1800) cache_obj.set(code, user, time_out=1800) if not cache_obj.get_time_is_locked(key, 60): context = { 'reset_url': '%s/reset_password?code=%s' % (settings.MAIN_DOMAIN, code), } async_send_email( email, u'智选找回密码', utils.render_email_template('email/reset_password.html', context), 'html') return 0, dict_err.get(0)
def regist_user(self, email, nick, password, re_password, ip, mobilenumber=None, username=None, source=0, gender=0, invitation_code=None): ''' @note: 注册 ''' try: if not (email and nick and password): transaction.rollback(using=ACCOUNT_DB) return 99800, dict_err.get(99800) if password != re_password: transaction.rollback(using=ACCOUNT_DB) return 10105, dict_err.get(10105) errcode, errmsg = self.check_user_info(email, nick, password, mobilenumber) if errcode != 0: transaction.rollback(using=ACCOUNT_DB) return errcode, errmsg id = utils.uuid_without_dash() now = datetime.datetime.now() user = User.objects.create(id=id, email=email, mobilenumber=mobilenumber, last_login=now, password=self.set_password(password)) profile = Profile.objects.create(id=id, nick=nick, ip=ip, source=source, gender=gender) self.set_profile_login_att(profile, user) transaction.commit(using=ACCOUNT_DB) # 发送验证邮件 # self.send_confirm_email(user) return 0, profile except Exception, e: debug.get_debug_detail(e) transaction.rollback(using=ACCOUNT_DB) return 99900, dict_err.get(99900)
def send_confirm_email(self, user): ''' @note: 发送验证邮件 ''' cache_obj = cache.Cache() key = u'confirm_email_code_%s' % user.id code = cache_obj.get(key) if not code: code = utils.uuid_without_dash() cache_obj.set(key, code, time_out=1800) if not cache_obj.get_time_is_locked(key, 60): context = {'verify_url': '%s/account/user_settings/verify_email?code=%s' % (settings.MAIN_DOMAIN, code), } async_send_email(user.email, u'且行户外邮箱验证', utils.render_email_template('email/verify_email.html', context), 'html')
def change_profile(request, template_name='account/change_profile.html'): img_key = 'avatar_%s' % utils.uuid_without_dash() # 七牛上传图片文件名 uptoken = qiniu_client.get_upload_token(img_key) # 七牛图片上传token if request.POST: nick = request.POST.get('nick') gender = request.POST.get('gender', '').strip() birthday = request.POST.get('birthday', '').strip() des = request.POST.get('des', '').strip() errcode, result = ub.change_profile(request.user, nick, gender, birthday, des) if errcode != 0: error_msg = result else: success_msg = u'修改资料成功' request.user = result return render_to_response(template_name, locals(), context_instance=RequestContext(request))
def user_settings(request, template_name='account/change_profile.html'): img_key = 'avatar_%s' % utils.uuid_without_dash() # 七牛上传图片文件名 uptoken = qiniu_client.get_upload_token(img_key) # 七牛图片上传token if request.POST: nick = request.POST.get('nick') gender = request.POST.get('gender', '').strip() birthday = request.POST.get('birthday', '').strip() des = request.POST.get('des', '').strip() errcode, result = ub.change_profile(request.user, nick, gender, birthday, des) if errcode != 0: error_msg = result else: success_msg = u'修改资料成功' request.user = result return render_to_response(template_name, locals(), context_instance=RequestContext(request))
def regist_user(self, email, nick, password, ip, mobilenumber=None, username=None, source=0, gender=0, invitation_code=None): ''' @note: 注册 ''' try: from www.message.interface import UnreadCountBase if not (email and nick and password): transaction.rollback(using=ACCOUNT_DB) return 99800, dict_err.get(99800) errcode, errmsg = self.check_user_info(email, nick, password, mobilenumber) if errcode != 0: transaction.rollback(using=ACCOUNT_DB) return errcode, errmsg id = utils.uuid_without_dash() now = datetime.datetime.now() user = User.objects.create(id=id, email=email, mobilenumber=mobilenumber, last_login=now, password=self.set_password(password)) profile = Profile.objects.create(id=id, nick=nick, ip=ip, source=source, gender=gender) self.set_profile_login_att(profile, user) if invitation_code: invitation = InvitationBase().add_invitation_user(invitation_code, profile.id) if invitation: # 发送系统通知 content = u'成功邀请一个注册用户 <a href="%s">%s</a>' % (profile.get_url(), profile.nick) UnreadCountBase().add_system_message(user_id=invitation.user_id, content=content) # 自动关注邀请者 from www.timeline.interface import UserFollowBase UserFollowBase().follow_people(profile.id, invitation.user_id) # 初始化用户数据 UserCount.objects.get_or_create(user_id=user.id) transaction.commit(using=ACCOUNT_DB) # todo发送验证邮件 return 0, profile except Exception, e: debug.get_debug_detail(e) transaction.rollback(using=ACCOUNT_DB) return 99900, dict_err.get(99900)
def send_forget_password_email(self, email): ''' @note: 发送密码找回邮件 ''' if not email: return 99800, dict_err.get(99800) user = self.get_user_by_email(email) if not user: return 10111, dict_err.get(10111) cache_obj = cache.Cache() key = u'forget_password_email_code_%s' % email code = cache_obj.get(key) if not code: code = utils.uuid_without_dash() cache_obj.set(key, code, time_out=1800) cache_obj.set(code, user, time_out=1800) if not cache_obj.get_time_is_locked(key, 60): context = {'reset_url': '%s/reset_password?code=%s' % (settings.MAIN_DOMAIN, code), } async_send_email(email, u'且行户外找回密码', utils.render_email_template('email/reset_password.html', context), 'html') return 0, dict_err.get(0)
def regist_user(self, email, nick, password, re_password, ip, mobilenumber=None, username=None, source=0, gender=0, invitation_code=None): ''' @note: 注册 ''' try: if not (email and nick and password): transaction.rollback(using=ACCOUNT_DB) return 99800, dict_err.get(99800) if password != re_password: transaction.rollback(using=ACCOUNT_DB) return 10105, dict_err.get(10105) errcode, errmsg = self.check_user_info(email, nick, password, mobilenumber) if errcode != 0: transaction.rollback(using=ACCOUNT_DB) return errcode, errmsg id = utils.uuid_without_dash() now = datetime.datetime.now() user = User.objects.create(id=id, email=email, mobilenumber=mobilenumber, last_login=now, password=self.set_password(password)) profile = Profile.objects.create(id=id, nick=nick, ip=ip, source=source, gender=gender) self.set_profile_login_att(profile, user) transaction.commit(using=ACCOUNT_DB) # 发送验证邮件 self.send_confirm_email(user) return 0, profile except Exception, e: debug.get_debug_detail(e) transaction.rollback(using=ACCOUNT_DB) return 99900, dict_err.get(99900)
def regist_user(self, email, nick, password, ip, mobilenumber=None, username=None, source=0, gender=0, invitation_code=None): ''' @note: 注册 ''' try: from www.message.interface import UnreadCountBase if not (email and nick and password): transaction.rollback(using=ACCOUNT_DB) return 99800, dict_err.get(99800) errcode, errmsg = self.check_user_info(email, nick, password, mobilenumber) if errcode != 0: transaction.rollback(using=ACCOUNT_DB) return errcode, errmsg id = utils.uuid_without_dash() now = datetime.datetime.now() user = User.objects.create(id=id, email=email, mobilenumber=mobilenumber, last_login=now, password=self.set_password(password)) profile = Profile.objects.create(id=id, nick=nick, ip=ip, source=source, gender=gender) self.set_profile_login_att(profile, user) if invitation_code: invitation = InvitationBase().add_invitation_user( invitation_code, profile.id) if invitation: # 发送系统通知 content = u'成功邀请一个注册用户 <a href="%s">%s</a>' % ( profile.get_url(), profile.nick) UnreadCountBase().add_system_message( user_id=invitation.user_id, content=content) # 自动关注邀请者 from www.timeline.interface import UserFollowBase UserFollowBase().follow_people(profile.id, invitation.user_id) # 初始化用户数据 UserCount.objects.get_or_create(user_id=user.id) transaction.commit(using=ACCOUNT_DB) # todo发送验证邮件 return 0, profile except Exception, e: debug.get_debug_detail(e) transaction.rollback(using=ACCOUNT_DB) return 99900, dict_err.get(99900)
def share_received_like(request): ''' 后台生成分享的图片 ''' import os from django.conf import settings result = {'flag': -1, 'result': '操作失败'} # 拼装获取指定用户赞的页面url url = "%s://%s/message/show_received_like?user_id=%s" % ( request.META['wsgi.url_scheme'], 'wwwinside.zhixuan.com', # request.META['HTTP_HOST'], request.user.id ) # 定义生成临时图片位置 file_name = '%s/static_local/temp_share/capty_%s.png' % (os.path.dirname(settings.SITE_ROOT), utils.uuid_without_dash()) temp = None try: # 调用子程序 生成图片 #cmd = 'python %s/common/capty.py %s %s' % (os.path.dirname(settings.SITE_ROOT), url, file_name) #cmd = 'python %s/common/_webkit2png.py -x 1366 768 -g 1366 0 -o %s %s' % (os.path.dirname(settings.SITE_ROOT), file_name, url) cmd = '/opt/python2.7.2/bin/python2.7 %s/common/_webkit2png.py -x 1366 768 -g 1366 0 -o %s %s' % (os.path.dirname(settings.SITE_ROOT), file_name, url) flag, msg = utils.exec_command(cmd, 20) logging.error(u'cmd is:\n%s\n msg is:%s' % (cmd, msg)) if not flag: result = {'flag': -1, 'result': u'服务器响应超时, 请重试'} else: # 读取临时文件上传到七牛 temp = open(file_name, 'rb') flag, img_name = qiniu_client.upload_img(temp) if flag: result = {'flag': 0, 'result': '%s/%s' % (settings.IMG0_DOMAIN, img_name.split('!')[0])} else: result = {'flag': -1, 'result': u'分享失败, 请重试'} except Exception, e: logging.error(debug.get_debug_detail(e)) result = {'flag': -1, 'result': u'分享失败, 请重试'}
def get_prepay_id(self, body, out_trade_no, total_fee, openid, attach="", trade_type="JSAPI", ip="121.42.48.184", notify_url=None): """ @note: total_fee单位为分,不能带小数点 """ url = "%s/pay/unifiedorder" % WEIXINPAY_URL notify_url = notify_url or ("%s/weixinnotify" % MAIN_DOMAIN) params = dict(appid=self.appid, mch_id=self.mch_id, nonce_str=utils.uuid_without_dash(), body=body, attach=attach, out_trade_no=out_trade_no, total_fee=total_fee, spbill_create_ip=ip, notify_url=notify_url, trade_type=trade_type, openid=openid) params, prestr = self.format_params(params) sign = self.build_mysign(prestr) params["sign"] = sign xml = """ <xml> <appid>%(appid)s</appid> <attach><![CDATA[%(attach)s]]></attach> <body><![CDATA[%(body)s]]></body> <mch_id>%(mch_id)s</mch_id> <nonce_str>%(nonce_str)s</nonce_str> <notify_url>%(notify_url)s</notify_url> <out_trade_no>%(out_trade_no)s</out_trade_no> <spbill_create_ip>%(spbill_create_ip)s</spbill_create_ip> <total_fee>%(total_fee)s</total_fee> <trade_type>%(trade_type)s</trade_type> <openid><![CDATA[%(openid)s]]></openid> <sign><![CDATA[%(sign)s]]></sign> </xml> """ % params try: resp = requests.post(url=url, data=xml, timeout=30, verify=False) resp.encoding = "utf-8" text = resp.text # text = """ # <xml><return_code><![CDATA[SUCCESS]]></return_code> # <return_msg><![CDATA[OK]]></return_msg> # <appid><![CDATA[wx23cca542b396c669]]></appid> # <mch_id><![CDATA[1224504302]]></mch_id> # <nonce_str><![CDATA[YGJzdXq7hggzwGVx]]></nonce_str> # <sign><![CDATA[08AC09CF26D2820E833F857E61DE4B62]]></sign> # <result_code><![CDATA[SUCCESS]]></result_code> # <prepay_id><![CDATA[wx2014121518153157cbc1f6af0195338906]]></prepay_id> # <trade_type><![CDATA[JSAPI]]></trade_type> # </xml> # """ text = smart_str(text) if "prepay_id" in text: jq = pq(text) prepay_id = jq('prepay_id').html() self.prepay_id = prepay_id return True, prepay_id else: from www.tasks import async_send_email from django.conf import settings from django.utils.encoding import smart_unicode logging.error(u"get_prepay_id fail, info is:%s" % text) logging.error(u"get_prepay_id fail, params is:%s" % smart_unicode(xml)) title = u'%s 生成微信支付链接错误' % settings.SERVER_NAME content = u'params:%s\n\nreturn info:%s' % (smart_unicode(xml), text) async_send_email(settings.NOTIFICATION_EMAIL, title, content) return False, text except Exception, e: debug.get_debug_detail_and_send_email(e) return False, e
def get_prepay_id(self, body, out_trade_no, total_fee, openid, attach="", trade_type="JSAPI", ip="121.42.48.184", notify_url=None): """ @note: total_fee单位为分,不能带小数点 """ url = "%s/pay/unifiedorder" % WEIXINPAY_URL notify_url = notify_url or ("%s/weixinnotify" % MAIN_DOMAIN) params = dict(appid=self.appid, mch_id=self.mch_id, nonce_str=utils.uuid_without_dash(), body=body, attach=attach, out_trade_no=out_trade_no, total_fee=total_fee, spbill_create_ip=ip, notify_url=notify_url, trade_type=trade_type, openid=openid) params, prestr = self.format_params(params) sign = self.build_mysign(prestr) params["sign"] = sign xml = """ <xml> <appid>%(appid)s</appid> <attach><![CDATA[%(attach)s]]></attach> <body><![CDATA[%(body)s]]></body> <mch_id>%(mch_id)s</mch_id> <nonce_str>%(nonce_str)s</nonce_str> <notify_url>%(notify_url)s</notify_url> <out_trade_no>%(out_trade_no)s</out_trade_no> <spbill_create_ip>%(spbill_create_ip)s</spbill_create_ip> <total_fee>%(total_fee)s</total_fee> <trade_type>%(trade_type)s</trade_type> <openid><![CDATA[%(openid)s]]></openid> <sign><![CDATA[%(sign)s]]></sign> </xml> """ % params try: resp = requests.post(url=url, data=xml, timeout=30, verify=False) resp.encoding = "utf-8" text = resp.text # text = """ # <xml><return_code><![CDATA[SUCCESS]]></return_code> # <return_msg><![CDATA[OK]]></return_msg> # <appid><![CDATA[wx23cca542b396c669]]></appid> # <mch_id><![CDATA[1224504302]]></mch_id> # <nonce_str><![CDATA[YGJzdXq7hggzwGVx]]></nonce_str> # <sign><![CDATA[08AC09CF26D2820E833F857E61DE4B62]]></sign> # <result_code><![CDATA[SUCCESS]]></result_code> # <prepay_id><![CDATA[wx2014121518153157cbc1f6af0195338906]]></prepay_id> # <trade_type><![CDATA[JSAPI]]></trade_type> # </xml> # """ text = smart_str(text) if "prepay_id" in text: jq = pq(text) prepay_id = jq('prepay_id').html() self.prepay_id = prepay_id return True, prepay_id else: from www.tasks import async_send_email from django.conf import settings from django.utils.encoding import smart_unicode logging.error(u"get_prepay_id fail, info is:%s" % text) logging.error(u"get_prepay_id fail, params is:%s" % smart_unicode(xml)) title = u'%s 生成微信支付链接错误' % settings.SERVER_NAME content = u'params:%s\n\nreturn info:%s' % (smart_unicode(xml), text) async_send_email(settings.NOTIFICATION_EMAIL, title, content) return False, text except Exception, e: debug.get_debug_detail_and_send_email(e) return False, e