def get(self, *args, **kwargs):
task = self.get_object()
if task.status in consts.NOT_RUN_STATUSES:
hosts = task.hosts.all()
groups = task.host_groups.all()
vars = task.vars.all()
task.id = None
task.pid = None
task.status = consts.WAIT
task.save()
task.hosts.add(*hosts)
task.host_groups.add(*groups)
task.vars.add(*vars)
models.TaskLog.objects.create(
task=task,
status=consts.IN_PROGRESS,
message='Replay task'
)
else:
messages.info(self.request, 'Not start duplicate task')
return redirect(reverse('task_log', kwargs={'pk': task.id}))
python类info()的实例源码
def secret_question_challenge_1(request, username):
up = get_object_or_404(UserProfile, user__username=username)
if request.method == 'POST':
form = SecretQuestionForm(request.POST)
if form.is_valid():
# Does the answer match?
data = form.cleaned_data
if up.password_reset_answer_1.lower() == data['answer'].lower():
ValidPasswordResetKey.objects.create(user=up.user)
messages.info(request,
'Please check your email for a special link'
' to reset your password.')
return HttpResponseRedirect(reverse('mfa_login'))
else:
messages.error(request,
'Wrong answer. Please try again.')
return HttpResponseRedirect(reverse('secret_question_challenge',
args=(username,)))
# HTTP GET
return render(request,
'generic/bootstrapform.html',
{'name': up.get_password_reset_question_1_display(),
'form': SecretQuestionForm()})
def secret_question_challenge_2(request, username):
up = get_object_or_404(UserProfile, user__username=username)
if request.method == 'POST':
form = SecretQuestionForm(request.POST)
if form.is_valid():
# Does the answer match?
data = form.cleaned_data
if up.password_reset_answer_2.lower() == data['answer'].lower():
ValidPasswordResetKey.objects.create(user=up.user)
messages.info(request,
'Please check your email for a special link'
' to reset your password.')
return HttpResponseRedirect(reverse('mfa_login'))
else:
messages.error(request,
'Wrong answer. Please try again.')
return HttpResponseRedirect(reverse('secret_question_challenge',
args=(username,)))
# HTTP GET
return render(request,
'generic/bootstrapform.html',
{'name': up.get_password_reset_question_2_display(),
'form': SecretQuestionForm()})
def secret_question_challenge_3(request, username):
up = get_object_or_404(UserProfile, user__username=username)
if request.method == 'POST':
form = SecretQuestionForm(request.POST)
if form.is_valid():
# Does the answer match?
data = form.cleaned_data
if up.password_reset_answer_3.lower() == data['answer'].lower():
ValidPasswordResetKey.objects.create(user=up.user)
messages.info(request,
'Please check your email for a special link'
' to reset your password.')
return HttpResponseRedirect(reverse('mfa_login'))
else:
messages.error(request,
'Wrong answer. Please try again.')
return HttpResponseRedirect(reverse('secret_question_challenge',
args=(username,)))
# HTTP GET
return render(request,
'generic/bootstrapform.html',
{'name': up.get_password_reset_question_3_display(),
'form': SecretQuestionForm()})
def user_code_register(request):
if request.method == 'POST':
form = EndUserRegisterForm(request.POST)
if form.is_valid():
form.save()
messages.info(
request, _('Please check your email for a link to complete registration.'))
return HttpResponseRedirect(reverse('home'))
else:
return render(request, 'user-code-login.html', {'form': form})
# this is a GET
initial = {"username": request.GET.get('username'),
"code": request.GET.get('code')}
return render(request, 'user-code-login.html',
{'form': EndUserRegisterForm(initial=initial)})
def ingest_mets_xml(request):
if request.method == 'POST':
try:
#if ingest_mets_xml_file_form.is_valid(): # TODO Check something for better error messages? And validate the file. Note: Django allows form submission, even if no file has been selected.
t_ingest_mets_xml(request.POST.get('collection'), request.FILES['mets_file'])
messages.info(request, 'File is being uploaded.')# TODO i18n,
return HttpResponse(json.dumps({'RESET': 'true', 'MESSAGE': render_to_string('library/message_modal.html', request=request)}), content_type='text/plain')
except:
messages.error(request, 'Error!')# TODO i18n,
return HttpResponse(json.dumps({'RESET': 'false', 'MESSAGE': render_to_string('library/message_modal.html', request=request)}), content_type='text/plain')
else:
ingest_mets_xml_file_form = MetsFileForm()
collections = t_collections(request)
if isinstance(collections,HttpResponse):
return collections
return render(request, 'library/ingest_mets_xml.html', {'ingest_mets_xml_form': ingest_mets_xml_file_form, 'collections': collections})
def ingest_mets_url(request):
if request.method == 'POST':
# What should be checked here and what can be left up to Transkribus?
if (t_ingest_mets_url(request.POST.get('collection'), request.POST.get('url'))):
messages.info(request, 'URL is being processed.')# TODO i18n,
return HttpResponse(json.dumps({'RESET': 'true', 'MESSAGE': render_to_string('library/message_modal.html', request=request)}), content_type='text/plain')
else:
messages.error(request, 'URL processing failed.')# TODO i18n,
return HttpResponse(json.dumps({'RESET': 'false', 'MESSAGE': render_to_string('library/message_modal.html', request=request)}), content_type='text/plain')
else:
data = {'url': request.GET.get('metsURL', '')}
ingest_mets_url_form = IngestMetsUrlForm(initial=data)
collections = t_collections(request)
if isinstance(collections,HttpResponse):
return collections
return render(request, 'library/ingest_mets_url.html', {'ingest_mets_url_form': ingest_mets_url_form, 'collections': collections})
def post(self, request):
form = self.form_class(request.POST)
if form.is_valid():
email = form.cleaned_data['email']
password = form.cleaned_data['password']
auth_user = authenticate(username=email, password=password)
if auth_user is not None:
if auth_user.is_active:
login(request, auth_user)
return redirect(reverse('user:user_home', args=[auth_user.id]))
else:
messages.info(request, '?????? ??????????? ???????????? ??????.')
else:
messages.error(request, 'Email ?????? ???????? ???? ????????. ?????? ???????? ???????')
return redirect(reverse('user:user_login'))
return render(request, self.template_name, {
'form': form
})
def register(request):
form = RegistrationForm()
if request.method == 'POST':
form = RegistrationForm(request.POST)
if form.is_valid():
form.save()
messages.info(
request,
'Kay?t ba?ar?l?. ?imdi login olabilirsiniz.'
)
return redirect('login')
return render(request, 'register.html', {
'form': form,
})
def login(request):
form = LoginForm()
if request.method == 'POST':
form = LoginForm(request.POST)
if form.is_valid():
auth_login(request, form.user)
messages.info(
request,
'Giri? yapt?n?z.'
)
return render(request, 'login.html', {
'form': form
})
def new_place(request):
form = PlaceCreationForm()
if request.method == "POST":
form = PlaceCreationForm(request.POST)
if form.is_valid():
form.instance.user = request.user
form.save()
messages.info(
request,
'Tebrikler. Yer bildiriminiz ba?ar?yla al?nd?. '
'Editör onay?ndan geçtikten sonra yay?nlanacakt?r.'
)
return redirect('/')
return render(
request,
'new_place.html',
{
'form': form,
}
)
def revert_page(self, request, page_id, language):
page = get_object_or_404(self.model, id=page_id)
# ensure user has permissions to publish this page
if not page.has_change_permission(request):
return HttpResponseForbidden(force_text(_("You do not have permission to change this page")))
page.revert(language)
messages.info(request, _('The page "%s" was successfully reverted.') % page)
if 'node' in request.GET or 'node' in request.POST:
# if request comes from tree..
return HttpResponse(admin_utils.render_admin_menu_item(request, page))
# TODO: This should never fail, but it may be a POF
path = page.get_absolute_url(language=language)
path = '%s?%s' % (path, get_cms_setting('CMS_TOOLBAR_URL__EDIT_OFF'))
return HttpResponseRedirect(path)
def get_urls(self):
"""Get the admin urls
"""
info = '%s_%s' % (self.model._meta.app_label, self.model._meta.model_name)
def pat(regex, fn):
return url(regex, self.admin_site.admin_view(fn), name='%s_%s' % (info, fn.__name__))
url_patterns = [
pat(r'^([0-9]+)/delete-translation/$', self.delete_translation),
pat(r'^([0-9]+)/([a-z\-]+)/publish/$', self.publish_article),
pat(r'^([0-9]+)/([a-z\-]+)/unpublish/$', self.unpublish),
pat(r'^([0-9]+)/([a-z\-]+)/preview/$', self.preview_article),
]
url_patterns += super(ArticleAdmin, self).get_urls()
return url_patterns
def get_redirect_url(self, **kwargs):
self.image.current_revision = self.revision
self.image.save()
messages.info(
self.request,
_('%(file)s has been changed to revision #%(revision)d') % {
'file': self.image.current_revision.imagerevision.get_filename(),
'revision': self.revision.revision_number})
if self.urlpath:
return reverse(
'wiki:images_index',
kwargs={
'path': self.urlpath.path})
return reverse(
'wiki:images_index',
kwargs={
'article_id': self.article.id})
def add_unenrollable_item_message(request, item):
"""
Add a message to the Django message store indicating that the item (i.e. course run, program) is unenrollable.
:param request: The current request.
:param item: The item that is unenrollable (i.e. a course run).
"""
messages.info(
request,
_(
'{strong_start}Something happened.{strong_end} '
'{span_start}This {item} is not currently open to new learners. Please start over and select a different '
'{item}.{span_end}'
).format(
item=item,
strong_start='<strong>',
strong_end='</strong>',
span_start='<span>',
span_end='</span>',
)
)
def add_generic_info_message_for_error(request):
"""
Add message to request indicating that there was an issue processing request.
Arguments:
request: The current request.
"""
messages.info(
request,
_(
'{strong_start}Something happened.{strong_end} '
'{span_start}This course is not available. '
'Please start over and select a different course.{span_end}'
).format(
span_start='<span>',
span_end='</span>',
strong_start='<strong>',
strong_end='</strong>',
)
)
def set_ifalias(account, fac, interface, request):
"""Set ifalias on netbox if it is requested"""
if 'ifalias' in request.POST:
ifalias = request.POST.get('ifalias')
if check_format_on_ifalias(ifalias):
try:
fac.set_if_alias(interface.ifindex, ifalias)
interface.ifalias = ifalias
LogEntry.add_log_entry(
account,
u'set-ifalias',
u'{actor}: {object} - ifalias set to "%s"' % ifalias,
subsystem=u'portadmin',
object=interface,
)
_logger.info('%s: %s:%s - ifalias set to "%s"', account.login,
interface.netbox.get_short_sysname(),
interface.ifname, ifalias)
except SnmpError as error:
_logger.error('Error setting ifalias: %s', error)
messages.error(request, "Error setting ifalias: %s" % error)
else:
messages.error(request, "Wrong format on port description")
def post(self, request, *args, **kwargs):
print('\n post', request.POST)
product_id = int(request.POST.get('product_id'))
owned_products_id = owned_products_pk(request.user)
print('product_id owned?', product_id, owned_products_id,product_id in owned_products_id, type(product_id), type(owned_products_id[0]) )
if product_id in owned_products_id:
product = Product.objects.get(id=product_id)
response = HttpResponse()
url = "/books/virtual_store/{}",format(product.file.name)
print('redirect to url', url)
response['X-Accel-Redirect'] = url
return response
return HttpResponseRedirect(reverse('customer:personal-library'))
messages.info(
self.request,
"Product not owned")
def contact(request):
if request.method == 'POST':
form = ContactForm(request.POST)
if form.is_valid():
content_vars = {
'name': form.cleaned_data['name'],
'email': form.cleaned_data['email'],
'message': form.cleaned_data['message'],
}
send_mail(
'Contact form submission from okthess.gr',
render_to_string('main/contact-email.txt', content_vars),
settings.DEFAULT_FROM_EMAIL,
[settings.CONTACT_TO_EMAIL],
)
messages.info(request, "Thank you for contacting us! We'll answer ASAP.")
return HttpResponseRedirect(reverse('main:contact'))
else:
return HttpResponse('Contact form is invalid.')
else:
return render(request, 'main/contact.html')
def apply(request):
if request.method == 'POST':
form = ApplicationForm(request.POST)
if form.is_valid():
form.save()
messages.info(request, 'Your application has been submitted. Thank you!')
return HttpResponseRedirect(reverse('main:apply'))
else:
return HttpResponse('Application form submission is invalid.')
else:
form = ApplicationForm()
apply_text = ApplyText.objects.first()
application_content = ''
if apply_text:
if request.LANGUAGE_CODE == 'en':
application_content = apply_text.content_en
else:
application_content = apply_text.content_el
return render(request, 'main/apply.html', {
'form': form,
'application_content': application_content,
})