text: from django.shortcuts import render
  from django.http import HttpResponse, HttpResponseRedirect
  # from django.template import loader
  from wenzi_blog.models import *
  from django.core.mail import send_mail
  
  
  # Create your views here.
  
  
  def book(request):
      return render(request, 'book.html', locals())
  
  
  def login(request):
      if request.method == 'GET':
          return render(request, 'login.html', locals())
      elif request.method == 'POST':
          if 'name' in request.POST:
              try:
                  User.objects.get(name=request.POST['name'])
                  return HttpResponse('用户名已存在，请返回重新输入。')
              except User.DoesNotExist:
                  if len(request.POST['name']) > 15:
                      return HttpResponse('用户名长度有误请重新输入')
                  elif '$' in request.POST['name']:
                      return HttpResponse('用户名非法字符”$“')
                  elif '^' in request.POST['name']:
                      return HttpResponse('用户名非法字符”^“')
                  elif request.POST['password'] != request.POST['repassword']:
                      return HttpResponse('两次密码输入不一致请重新填写')
                  elif len(request.POST['password']) < 6 or len(request.POST['password']) > 20:
                      return HttpResponse('密码长度有误')
                  else:
                      num = False
                      en = False
                      el = True
                      for w in str(request.POST['password']):
                          if w.isdigit():
                              num = True
                          elif w.isalpha():
                              en = True
                          elif w == '$' or w == '^':
                              el = False
                      if num and en and el:
                          i = 1
                          like = ''
                          while i <= 5:
                              location = 'like_' + str(i)
                              try:
                                  like += request.POST[location]
                                  i += 1
                              except KeyError:
                                  i += 1
                          newuser = User(
                              name=request.POST['name'],
                              password=request.POST['password'],
                              sex=request.POST['sex'],
                              age=request.POST['age'],
                              email=request.POST['email'],
                              intro=request.POST['intro'],
                              job=request.POST['job'],
                              like=like,
                          )
                          newuser.save()
                          c = {'user': User.objects.get(name=request.POST['name'])}
                          return render(request, 'login.html', c)
                      else:
                          return HttpResponse('密码格式有误')
          elif 'user_name' in request.POST:
              try:
                  theuser = User.objects.get(name=request.POST['user_name'])
                  if theuser.password == request.POST['user_password']:
                      request.session['name'] = request.POST['user_name']
                      return HttpResponseRedirect('/user/' + request.POST['user_name'])
                  else:
                      return HttpResponse('密码错误')
              except User.DoesNotExist:
                  return HttpResponse('您输入的用户不存在')
          else:
              return render(request, 'login.html', locals())
  
  
  def user(request, name):
      if request.method == 'GET':
          if name == request.session['name']:
              request.session['name'] = ''
              c = {
                  'users': User.objects.get(name=name),
                  'books': Book.objects.order_by('-id')[0:5],
                  'reviews': Review.objects.order_by('-id')[0:5],
                  'ownerreview': Review.objects.filter(username=name)
              }
              return render(request, 'user.html', c)
          else:
              return HttpResponseRedirect('/login')
      elif request.method == 'POST':
          request.session['set'] = name
          return HttpResponseRedirect('/change/' + name)
  
  
  def change(request, name):
      if request.method == 'GET':
          if name == request.session['set']:
              request.session['set'] = ''
              return render(request, 'change.html', {'users': User.objects.get(name=name)})
          else:
              return HttpResponseRedirect('/login')
      elif request.method == 'POST':
          if 'oldpassword' in request.POST:
              theuser = User.objects.get(name=name)
              if theuser.password == request.POST['oldpassword']:
                  if request.POST['newpassword'] != request.POST['renewpassword']:
                      return HttpResponse('两次密码输入不一致请重新填写')
                  elif len(request.POST['newpassword']) < 6 or len(request.POST['newpassword']) > 20:
                      return HttpResponse('密码长度有误')
                  else:
                      num = False
                      en = False
                      el = True
                      for w in str(request.POST['newpassword']):
                          if w.isdigit():
                              num = True
                          elif w.isalpha():
                              en = True
                          elif w == '$' or w == '^':
                              el = False
                      if num and en and el:
                          theuser.password = request.POST['newpassword']
                          theuser.save()
                          return HttpResponseRedirect('/login')
                      else:
                          return HttpResponse('密码格式有误')
              else:
                  return HttpResponse('旧密码错误')
  
          elif 'intro' in request.POST:
              usermes = User.objects.get(name=name)
              i = 1
              like = ''
              while i <= 5:
                  location = 'like_' + str(i)
                  try:
                      like += request.POST[location]
                      i += 1
                  except KeyError:
                      i += 1
              if like != '':
                  usermes.like = like
              if usermes.job != request.POST['job']:
                  usermes.job = request.POST['job']
              if usermes.intro != request.POST['intro']:
                  usermes.intro = request.POST['intro']
              if request.POST['email'] != 'xx@xx':
                  usermes.email = request.POST['email']
              usermes.save()
              return HttpResponseRedirect('/login')
  
  
  def newbook(request, category):
      return render(request, 'newbook.html', {'category': category})
  
  
  def introbooks(request, catagory):
      if request.method == 'GET':
          c = {'category': catagory, 'book': Book.objects.filter(bookcategory=catagory).order_by('-id')}
          return render(request, 'introbooks.html', c)
      elif request.method == 'POST':
          try:
              Book.objects.get(bookname=request.POST['bookname'])
              return HttpResponse('该书已被推荐')
          except Book.DoesNotExist:
              newbooks = Book(
                  bookcategory=catagory,
                  bookname=request.POST['bookname'],
                  writer=request.POST['writer'],
                  title=request.POST['title'],
                  bookintro=request.POST['bookintro']
              )
              newbooks.save()
              return render(request, 'introbooks.html', {
                  'category': catagory,
                  'book': Book.objects.filter(bookcategory=catagory).order_by('-id')
              })
  
  
  def review(request, bookname):
      if request.method == 'GET':
          re = {'review': Review.objects.filter(bookname=bookname), 'book': Book.objects.get(bookname=bookname)}
          return render(request, 'review.html', re)
      elif request.method == 'POST':
          newreview = Review(
              username=request.POST['username'],
              bookname=bookname,
              text=request.POST['text']
          )
          newreview.save()
          return render(request, 'review.html', {
              'review': Review.objects.filter(bookname=bookname).order_by('-id'),
              'book': Book.objects.get(bookname=bookname)})
  
  
  def send_email(request):
      if request.method == 'POST':
          text = '邮箱'+request.POST['email']+'问题：'+request.POST['text']
          # 值1：  邮件标题   值2： 邮件主体
          # 值3： 发件人      值4： 收件人
          res = send_mail('用户求助',
                          text,
                          'songyuwong@163.com',
                          ['songyuwong@163.com'])
          if res == 1:
              return HttpResponse('邮件发送成功')
          else:
              return HttpResponse('邮件发送失败')
      else:
          return render(request, 'Email.html')
  