Advertisement
Guest User

Untitled

a guest
Jan 16th, 2017
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.50 KB | None | 0 0
  1. import MySQLdb
  2. from django.shortcuts import render
  3. from django.views.generic import TemplateView
  4. from django.contrib.auth.models import User
  5.  
  6.  
  7. # Create your views here.
  8. from account.models import Account, Point
  9.  
  10.  
  11. class AccountView(TemplateView):
  12.     template_name = 'account/account_view.html'
  13.  
  14.  
  15. def user_import(row):
  16.     username = row[3]
  17.     password = row[4]
  18.     fullname = row[5]
  19.     vip_code = row[2]
  20.     points = row[6]
  21.  
  22.     user = User(username=username)
  23.     user.set_password(password)
  24.     user.save()
  25.  
  26.     account = Account(user=user, fullname=fullname, vip_code=vip_code)
  27.     account.save()
  28.  
  29.     point = Point(account=account, point=points)
  30.     point.save()
  31.     print("Пользователь %s %s добавлен" % (username, password))
  32.  
  33.  
  34. class AccountImport(TemplateView):
  35.     template_name = 'account/account_view.html'
  36.  
  37.     def get_context_data(self, **kwargs):
  38.         context = super(AccountImport, self).get_context_data()
  39.         host = '85.25.45.121'
  40.         login = ''
  41.         password = ''
  42.         database = 'main'
  43.  
  44.         con = MySQLdb.connect(host=host, user=login, passwd=password, db=database, charset='utf8')
  45.         cur = con.cursor()
  46.  
  47.         # cur.execute("SELECT VERSION()")
  48.         # ver = cur.fetchone()
  49.         # print("Database version : %s " % ver)
  50.  
  51.         sql = "select * from x17f_catalog_users"
  52.         cur.execute(sql)
  53.         rows = cur.fetchall()
  54.  
  55.         for row in rows:
  56.             user_import(row)
  57.  
  58.         return context
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement