I have just finished a Django application, and I would like to get from you some code reviews related to my code.
Application flow:
Basically, my application is accessing a specific URL (always the same), gets all the links from a specific div
, enters each of those URLs and on each oh those links in entering another link from a specific location (that location is always in the same place). After accessing that page, is filling out two input fields (name
, email
) and press a button (be aware of the fact that every page that contains such inputs and buttons are different one from eachother - the HTML structure differs).
main-script.py
# -*- coding: utf-8 -*-
from selenium.webdriver.common.keys import Keys
from pyvirtualdisplay import Display
from selenium import webdriver
import lxml.html
import urlparse
import time
import re
def subscribe(email, name):
display = Display(visible=0, size=(800, 600))
dom = lxml.html.parse('http://muncheye.com')
url = dom.docinfo.URL
# not_found = False
# name_required = True
# email_required = True
# button_required = True
driver = webdriver.Firefox()
failed_urls = []
for link in dom.xpath('//div[@id="right-column"]//a/@href'):
FINAL_URL = urlparse.urljoin(url, link)
dom1 = lxml.html.parse(FINAL_URL)
for link1 in dom1.xpath('//div[@class="product_info"]//table//tr[7]//td[2]//a/@href'):
not_found = False
name_required = True
email_required = True
button_required = True
if re.match('http[s]?://[^\s<>"]+|www\.[^\s<>"]+', link1) is not None:
try:
driver.get(link1)
except:
failed_urls.append(link1)
time.sleep(2)
try:
name_box = driver.find_element_by_xpath("//input[@*[contains(translate(., 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), 'name')]]")
name_box.click()
name_box.clear()
name_box.send_keys(email)
except:
not_found = True
try:
email_box = driver.find_element_by_xpath("//input[@*[contains(translate(., 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), 'email')]]")
email_box.click()
email_box.clear()
email_box.send_keys(email)
except:
not_found = True
if not_found:
print "here"
for element in driver.find_elements_by_xpath("//input[@type='text']"):
if name_required:
try:
name_box = element.find_element_by_xpath(".[@*[contains(translate(., 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), 'name')]]")
name_box.click()
name_box.clear()
name_box.send_keys(name)
name_required = False
continue
except:
pass
if email_required:
try:
email_box = element.find_element_by_xpath(".[@*[contains(translate(., 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), 'email')]]")
email_box.click()
email_box.clear()
email_box.send_keys(email)
email_required = False
break
except:
pass
if (not name_required) and (not email_required) and (not button_required):
failed_urls.append(link1)
break
for element1 in driver.find_elements_by_xpath("//*[@type[translate(., 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz') = 'submit']][preceding::*[@name[translate(., 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz') ='email' or translate(., 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz') ='name']]]"):
if button_required:
try:
button = element1.find_element_by_xpath("//*[@type[translate(., 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz') = 'submit']][preceding::*[@name[translate(., 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz') ='email' or translate(., 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz') ='name']]]").click()
element1.click()
element1.send_keys(Keys.ENTER)
button_required = False
continue
except:
try:
element1.find_element_by_xpath("//*[@name='email' or @name='name']//following::*[@type='submit']/a").click()
element1.click()
element1.send_keys(Keys.ENTER)
button_required = False
except:
pass
time.sleep(5)
print button_required
return failed_urls
forms.py
from django import forms
from django.contrib.auth.models import User
from django.utils.translation import ugettext_lazy as _
class RegistrationForm(forms.Form):
username = forms.RegexField(regex=r'^\w+$', widget=forms.TextInput(attrs=dict(required=True, max_length=30)), label=_("Username"), error_messages={ 'invalid': _("This value must contain only letters, numbers and underscores.") })
email = forms.EmailField(widget=forms.TextInput(attrs=dict(required=True, max_length=30)), label=_("Email address"))
password1 = forms.CharField(widget=forms.PasswordInput(attrs=dict(required=True, max_length=30, render_value=False)), label=_("Password"))
password2 = forms.CharField(widget=forms.PasswordInput(attrs=dict(required=True, max_length=30, render_value=False)), label=_("Password (again)"))
def clean_username(self):
try:
user = User.objects.get(username__iexact=self.cleaned_data['username'])
except User.DoesNotExist:
return self.cleaned_data['username']
raise forms.ValidationError(_("The username already exists. Please try another one."))
def clean(self):
if 'password1' in self.cleaned_data and 'password2' in self.cleaned_data:
if self.cleaned_data['password1'] != self.cleaned_data['password2']:
raise forms.ValidationError(_("Password fields didn't match."))
return self.cleaned_data
class HomeForm(forms.Form):
username = forms.CharField()
email = forms.CharField()
views.py
from django.shortcuts import render
from django.views.generic.edit import FormView
from emailsproject.forms import *
from django.contrib.auth.decorators import login_required
from django.contrib.auth import logout
from django.views.decorators.csrf import csrf_protect
from django.shortcuts import render_to_response
from django.http import HttpResponseRedirect
from django.template import RequestContext
from emailsproject.muncheye import subscribe
@csrf_protect
def register(request):
if request.method == 'POST':
form = RegistrationForm(request.POST)
if form.is_valid():
user = User.objects.create_user(
username=form.cleaned_data['username'],
password=form.cleaned_data['password1'],
email=form.cleaned_data['email'],
)
user.is_active = False
user.save()
return HttpResponseRedirect('/register/success/')
else:
form = RegistrationForm()
variables = RequestContext(request, {
'form': form
})
return render_to_response(
'registration/register.html',
variables,
)
def register_success(request):
return render_to_response(
'registration/success.html',
)
def logout_page(request):
logout(request)
return HttpResponseRedirect('/')
@login_required
def home(request):
return render_to_response(
'home.html', {
'user': request.user
}
)
class HomeView(FormView):
template_name = 'home.html'
form_class = HomeForm
def form_valid(self, form):
context = {}
name = form.cleaned_data['username']
email = form.cleaned_data['email']
failed_urls = subscribe(email, name)
context['failed_urls'] = failed_urls
return render(
self.request,
'subscribe_success.html',
context
)
home_view = HomeView.as_view()
For now, I would just like to get some code reviews and improvements / best practices if possible.
settings.py
file :) – Alexander Sep 1 at 8:40