def import_user(): import csv from stepbystep.models import UserModel, AccountItem, Account from stepbystep.libs.tasks import account_crawler with open('stepbystep_user.csv', 'r') as users: for sid, name, grade, sdut_id, poj_id in csv.reader( users.read().splitlines()[1:]): if sid and name and grade: user = UserModel.objects(username=sid).first() if not user: user = UserModel.create_user(username=sid, password='******', name=name, grade=grade) if sdut_id: sdut_account = AccountItem.objects.create(origin_oj='sdut', username=sdut_id) user.sdut = Account(user_id=sdut_id, account=sdut_account) account_crawler.delay(origin_oj='sdut', username=sdut_id) if poj_id: poj_account = AccountItem.objects.create(origin_oj='poj', username=poj_id) user.poj = Account(user_id=poj_id, account=poj_account) account_crawler.delay(origin_oj='poj', username=poj_id) user.save()
def import_user(filename): import csv from stepbystep.models import UserModel, AccountItem, Account, RoleModel from stepbystep.libs.tasks import account_crawler sdut_program_1 = RoleModel.objects(name='program_1').first() sdut_program_2 = RoleModel.objects(name='program_2').first() sdut_data_structure = RoleModel.objects(name='data_structure').first() with open(filename, 'r') as users: for sid, name, grade, sdut_id, poj_id in csv.reader( users.read().splitlines()[1:]): if sid and name and grade: user = UserModel.objects( username=sid).first() if not user: user = UserModel.create_user( username=sid, password='******', name=name, grade=grade ) if sdut_id: sdut_account = AccountItem.objects.create( origin_oj='sdut', username=sdut_id ) user.sdut = Account( user_id=sdut_id, account=sdut_account ) user.roles.append(sdut_program_1) user.roles.append(sdut_program_2) user.roles.append(sdut_data_structure) account_crawler.delay( origin_oj='sdut', username=sdut_id ) if poj_id: poj_account = AccountItem.objects.create( origin_oj='poj', username=poj_id ) user.poj = Account( user_id=poj_id, account=poj_account ) account_crawler.delay( origin_oj='poj', username=poj_id ) user.save()
def import_user(): import csv from stepbystep.models import UserModel, AccountItem, Account from stepbystep.libs.tasks import account_crawler with open('stepbystep_user.csv', 'r') as users: for sid, name, grade, sdut_id, poj_id in csv.reader( users.read().splitlines()[1:]): if sid and name and grade: user = UserModel.objects( username=sid).first() if not user: user = UserModel.create_user( username=sid, password='******', name=name, grade=grade ) if sdut_id: sdut_account = AccountItem.objects.create( origin_oj='sdut', username=sdut_id ) user.sdut = Account( user_id=sdut_id, account=sdut_account ) account_crawler.delay( origin_oj='sdut', username=sdut_id ) if poj_id: poj_account = AccountItem.objects.create( origin_oj='poj', username=poj_id ) user.poj = Account( user_id=poj_id, account=poj_account ) account_crawler.delay( origin_oj='poj', username=poj_id ) user.save()
def on_model_change(self, form, model): if len(model.password2): model.password = UserModel.generate_password(form.password2.data) elif not model.password: model.password = UserModel.generate_password('12345678') if model.bestcoder.user_id: account = AccountItem.objects( origin_oj='bestcoder', username=model.bestcoder.user_id ).first() if not account: account = AccountItem.objects.create( origin_oj='bestcoder', username=model.bestcoder.user_id ) account_crawler.delay( origin_oj='bestcoder', username=model.bestcoder.user_id ) model.bestcoder.account = account else: model.bestcoder = Account(user_id='') if model.topcoder.user_id: account = AccountItem.objects( origin_oj='topcoder', username=model.topcoder.user_id ).first() if not account: account = AccountItem.objects.create( origin_oj='topcoder', username=model.topcoder.user_id ) account_crawler.delay( origin_oj='topcoder', username=model.topcoder.user_id ) model.topcoder.account = account else: model.topcoder = Account(user_id='') if model.codeforces.user_id: account = AccountItem.objects( origin_oj='codeforces', username=model.codeforces.user_id ).first() if not account: account = AccountItem.objects.create( origin_oj='codeforces', username=model.codeforces.user_id ) account_crawler.delay( origin_oj='codeforces', username=model.codeforces.user_id ) model.codeforces.account = account else: model.codeforces = Account(user_id='') if model.sdut.user_id: account = AccountItem.objects( origin_oj='sdut', username=model.sdut.user_id ).first() if not account: account = AccountItem.objects.create( origin_oj='sdut', username=model.sdut.user_id ) account_crawler.delay( origin_oj='sdut', username=model.sdut.user_id ) model.sdut.account = account else: model.sdut = Account(user_id='') if model.hduoj.user_id: account = AccountItem.objects( origin_oj='hduoj', username=model.hduoj.user_id ).first() if not account: account = AccountItem.objects.create( origin_oj='hduoj', username=model.hduoj.user_id ) account_crawler.delay( origin_oj='hduoj', username=model.hduoj.user_id ) model.hduoj.account = account else: model.hduoj = Account(user_id='') if model.poj.user_id: account = AccountItem.objects( origin_oj='poj', username=model.poj.user_id ).first() if not account: account = AccountItem.objects.create( origin_oj='poj', username=model.poj.user_id ) account_crawler.delay( origin_oj='poj', username=model.poj.user_id ) model.poj.account = account else: model.poj = Account(user_id='')