本文整理汇总了Python中student.models.UserProfile.contract_id方法的典型用法代码示例。如果您正苦于以下问题:Python UserProfile.contract_id方法的具体用法?Python UserProfile.contract_id怎么用?Python UserProfile.contract_id使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类student.models.UserProfile
的用法示例。
在下文中一共展示了UserProfile.contract_id方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: import_user_submit
# 需要导入模块: from student.models import UserProfile [as 别名]
# 或者: from student.models.UserProfile import contract_id [as 别名]
def import_user_submit(request):
# http://www.cnblogs.com/yijun-boxing/archive/2011/04/18/2020155.html
CONTRACT_CVS_COL_CONTRACT_ID=0
CONTRACT_CVS_COL_DISTRICT_ID=1
CONTRACT_CVS_COL_EMAIL=2
CONTRACT_CVS_COL_USERNAME=3
CONTRACT_CVS_COUNT_COL=4
message={}
n=0
if request.method == 'POST':
f=request.FILES['file']
dialect = csv.Sniffer().sniff(f.read(1024), delimiters=";,")
f.seek(0)
r=csv.reader(f,dialect)
try:
for i,line in enumerate(r):
n=n+1
contract_id=line[CONTRACT_CVS_COL_CONTRACT_ID]
district_id=line[CONTRACT_CVS_COL_DISTRICT_ID]
email=line[CONTRACT_CVS_COL_EMAIL]
username=line[CONTRACT_CVS_COL_USERNAME]
for value in line:
if len(value.strip())==0:
raise Exception("Catch csv line with empty fields line")
if len(line) != CONTRACT_CVS_COUNT_COL:
raise Exception("Catch csv line of wrong fields count")
user = User(username=username, email=email, is_active=True)
user.set_password(username)
registration = Registration()
try:
user.save()
except IntegrityError:
if len(User.objects.filter(username=username)) > 0:
raise Exception("An account with the Public Username '{username}' already exists.".format(username=username))
if len(User.objects.filter(email=email)) > 0:
raise Exception("An account with the Email '{email}' already exists.".format(email=email))
registration.register(user)
profile=UserProfile(user=user)
profile.contract_id=contract_id
profile.district_id=district_id
profile.email=email
profile.username=username
profile.save()
reg = Registration.objects.get(user=user)
d = {'name': profile.name, 'key': reg.activation_key}
subject = render_to_string('emails/activation_email_subject.txt', d)
subject = ''.join(subject.splitlines())
message = render_to_string('emails/activation_email.txt', d)
try:
_res = user.email_user(subject, message, settings.DEFAULT_FROM_EMAIL)
except:
log.warning('Unable to send reactivation email', exc_info=True)
return HttpResponse(json.dumps({'success': False, 'error': _('Unable to send reactivation email')}))
message={'success': True, "message":"Success! %s users imported." % (n)}
except Exception as e:
transaction.rollback()
message={'success': False,'message':'Import error: %s, At cvs line: %s' % (e,n)}
# title = forms.CharField(max_length=50)
# file = forms.FileField()
return HttpResponse(json.dumps(message))