How to overwrite js to create records with modal in django-autocomplete-light 3.2.8 - django-autocomplete-light

I need to overwrite the javascript code of the library "django-autocomplete-light" to open a modal when the user selects the option to create. The installed libraries are:
Django==1.11
django-autocomplete-light==3.2.7
And my code is:
class SupplierAutocomplete(autocomplete.Select2QuerySetView):
create_field = 'name'
def get_queryset(self):
qs = Supplier.objects.all()
if self.q:
qs = qs.filter(name__istartswith=self.q)
return qs

Related

How to update a value from a field without refreshing the page Odoo

I'm creating a new sector on the settings page of my module, where I have a value and an update icon, which aims to update the field value when I click on the icon.
But when I call the function to execute the function, the page is reloaded and I never get the value, but the value is printed in the terminal with a logger, does anyone have any suggestions?
My XML code:
<button type="object" name="refresh_credits" class="btn-link" icon="fa-refresh"/>
<span class="btn-link">Credits</span>
<field name="new_credits"/>
My python code inside a class:
class ResConfigSettings(models.TransientModel):
_inherit = 'res.config.settings'
new_credits = fields.Integer()
def refresh_credits(self):
data_details_credits = self.env['show.credits'].content_credits_info()
_logger.info(self.env['show.credits'].content_credits_info()[4])
self.new_credits = data_details_credits[4]
You have to override set_values and get_values method to update the values in settings. We cannot directly update the values in general settings.
Below is the example:
def set_values(self):
super(ResConfigSettings, self).set_values()
self.env['ir.config_parameter'].sudo().set_param('MODULE_NAME.new_credits',
self.new_credits)
#api.model
def get_values(self):
res = super(ResConfigSettings, self).get_values()
config = self.env['ir.config_parameter'].sudo()
config_key = config.get_param('MODULE_NAME.new_credits')
if config_zoom_key:
res.update(
field_name=config_key
)
return res
And also for better understanding, watch this video:
Add fields general settings

If-else and for loops not working in HTML for new XBlock in OpenEdX

I've created a new XBLock and trying to pass data from python file to HTML file, but the standard template syntax is throwing error, HTML file code below
<p>EdlyCarouselXBlock: count is now
<span class='count'>{self.count}</span> (click me to increment).
{{ self.count }}
{% if self.count%}
<p>Simple If</p>
{%endif%}
</p>
<p id="decrement">(click me to Decrement).</p>
</div>
It is supporting a single bracket to access values {self.name}, for loops & if-else it throws an error. Tried using python 3.6.5 & 3.8.0, the same issue on both versions, any help would be appreciated, same issue in the lms/cms as well.
Solve the issue, posting the solution for future references.
Default xblock is created without using Django templating so we need to configure it manually.
Following default student_view function is added
def student_view(self, context=None):
"""
The primary view of the SampleXBlock, shown to students
when viewing courses.
"""
html = self.resource_string("static/html/sample.html")
frag = Fragment(html.format(self=self))
frag.add_css(self.resource_string("static/css/sample.css"))
frag.add_javascript(self.resource_string("static/js/src/sample.js"))
frag.initialize_js('SampleXBlock')
return frag
so in order to involve Django template rendering make the following changes.
include template and context
from django.template import Context, Template
# Add this method in your xblock class
def render_template(self, template_path, context={}):
template_str = self.resource_string(template_path)
template = Template(template_str)
return template.render(Context(context))
def student_view(self, context=None):
"""
The primary view of the SampleXBlock, shown to students
when viewing courses.
"""
frag = Fragment()
html = self.render_template("static/html/sample.html", {'count': self.count})
frag.add_content(html)
frag.add_css(self.resource_string("static/css/sample.css"))
frag.add_javascript(self.resource_string("static/js/src/sample.js"))
frag.initialize_js('SampleXBlock')
return frag

Django 2.0 - Rendering wrong template (with no error)

I'm trying to load a template visit_form.html which is a DetailView with a form within it. Each time I click on a link from main.html the wrong template gets loaded -> main_detail.html. I have cleared browser cache, invalidated caches.
The goal is to have the MainVisitDisplay render the visit_form.html, but all I get is the main_detail.html. It throws an error for main_detail.html when I change the location of the main_detail.html template, and throws a "TemplateDoesNotExist" error, looking for the main_detail.html template.
My MWE is:
urls.py
from django.conf.urls import url
from . import views
from django.urls import path
urlpatterns = [
path('', views.index, name='index'),
path('main/', views.MainListView.as_view(), name='main'),
path('main/<int:pk>/', views.MainDetailView.as_view(), name='main_detail'),
path('visit/add/<int:pk>/', views.MainVisitDisplay.as_view(), name='visit_form'),
]
views.py
class MainVisitDisplay(DetailView):
model = Main
template = "visit_form.html"
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['form'] = VisitForm()
return context
class MainDetailView(generic.DetailView):
template_name = "clincher/main_detail.html"
model = Main
main.html (template) url
{% url 'clincher:visit_form' main.id %}
This was a really simple. use template_name = "template_name.html" NOT template = "template_name.html. Not sure why it kept rendering the other templates. Also, apparently, Django 2.0 does not cache templates, but feel free to confirm or deny this.

Scrapy Running Results

Just getting started with Scrapy, I'm hoping for a nudge in the right direction.
I want to scrape data from here:
https://www.sportstats.ca/display-results.xhtml?raceid=29360
This is what I have so far:
import scrapy
import re
class BlogSpider(scrapy.Spider):
name = 'sportstats'
start_urls = ['https://www.sportstats.ca/display-results.xhtml?raceid=29360']
def parse(self, response):
headings = []
results = []
tables = response.xpath('//table')
headings = list(tables[0].xpath('thead/tr/th/span/span/text()').extract())
rows = tables[0].xpath('tbody/tr[contains(#class, "ui-widget-content ui-datatable")]')
for row in rows:
result = []
tds = row.xpath('td')
for td in enumerate(tds):
if headings[td[0]].lower() == 'comp.':
content = None
elif headings[td[0]].lower() == 'view':
content = None
elif headings[td[0]].lower() == 'name':
content = td[1].xpath('span/a/text()').extract()[0]
else:
try:
content = td[1].xpath('span/text()').extract()[0]
except:
content = None
result.append(content)
results.append(result)
for result in results:
print(result)
Now I need to move on to the next page, which I can do in a browser by clicking the "right arrow" at the bottom, which I believe is the following li:
<li><a id="mainForm:j_idt369" href="#" class="ui-commandlink ui-widget fa fa-angle-right" onclick="PrimeFaces.ab({s:"mainForm:j_idt369",p:"mainForm",u:"mainForm:result_table mainForm:pageNav mainForm:eventAthleteDetailsDialog",onco:function(xhr,status,args){hideDetails('athlete-popup');showDetails('event-popup');scrollToTopOfElement('mainForm\\:result_table');;}});return false;"></a>
How can I get scrapy to follow that?
If you open the url in a browser without javascript you won't be able to move to the next page. As you can see inside the li tag, there is some javascript to be executed in order to get the next page.
Yo get around this, the first option is usually try to identify the request generated by javascript. In your case, it should be easy: just analyze the java script code and replicate it with python in your spider. If you can do that, you can send the same request from scrapy. If you can't do it, the next option is usually to use some package with javascript/browser emulation or someting like that. Something like ScrapyJS or Scrapy + Selenium.
You're going to need to perform a callback. Generate the url from the xpath from the 'next page' button. So url = response.xpath(xpath to next_page_button) and then when you're finished scraping that page you'll do yield scrapy.Request(url, callback=self.parse_next_page). Finally you create a new function called def parse_next_page(self, response):.
A final, final note is if it happens to be in Javascript (and you can't scrape it even if you're sure you're using the correct xpath) check out my repo in using splash with scrapy https://github.com/Liamhanninen/Scrape

Is there any solutions to add captcha to Django-allauth?

Is there any solutions to use captcha with django-allauth?
I want to use captcha on registration form for standard email+password registrations.
I too needed to do this with django-allauth and found that implementing the django-recaptcha package to be relatively simple.
Configure django-recaptcha
Sign up for a recaptcha account.
Plug your settings in
# settings.py
RECAPTCHA_PUBLIC_KEY = 'xyz'
RECAPTCHA_PRIVATE_KEY = 'xyz'
RECAPTCHA_USE_SSL = True # Defaults to False
Customize SignupForm
After installing django-recaptcha, I followed someguidelines for customizing the SignupForm.
from django import forms
from captcha.fields import ReCaptchaField
class AllAuthSignupForm(forms.Form):
captcha = ReCaptchaField()
def save(self, request, user):
user = super(AllAuthSignupForm, self).save(request)
return user
You also need to tell allauth to inherit from this form in settings.py
ACCOUNT_SIGNUP_FORM_CLASS = 'myapp.forms.AllAuthSignupForm'
Wire up signup form template
{{ form.captcha }} and {{ form.captcha.errors }} should be available on the signup template context at this point.
That was it! Seems like all the validation logic is tucked into the ReCaptchaField.
To get the ReCaptcha field to the bottom of the form, simply add the other fields before the captcha field.
So what was user, email, captcha, password1, password2 becomes user, email, password1, password2, captcha with this form:
from allauth.account.forms import SignupForm, PasswordField
from django.utils.translation import ugettext_lazy as _
from captcha.fields import ReCaptchaField
class UpdatedSignUpForm(SignupForm):
password1 = PasswordField(label=_("Password"))
password2 = PasswordField(label=_("Password (again)"))
captcha = ReCaptchaField()
def save(self, request):
user = super(UpdatedSignUpForm, self).save(request)
return user
You then just need to add this form into the settings.py file as described in the previous answer.
You can also take a look at Form.field_order.
So a simple sign up form using django-allauth, with captcha and the fields ordered as you wish, would look like this:
from allauth.account.forms import SignupForm
from captcha.fields import ReCaptchaField
class MyCustomSignupForm(SignupForm):
captcha = ReCaptchaField()
field_order = ['email', 'password1', 'captcha']
In this case, the captcha will be at the very end.
The accepted answer is mostly fine, but produced this error for me when submitting the sign up form:
save() missing 1 required positional argument: 'user'
To fix this, your custom form could look like this
class AllAuthSignupForm(forms.Form):
captcha = ReCaptchaField()
def signup(self, request, user):
pass
NOTE: django-allauth also warns if the custom signup form does not have signup method.