Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Processing batch passwords now a cron job

  • Loading branch information...
commit 97f898e2e68fa58d072bcf6048d553fbe931d74b 1 parent 27f7716
@Smotko authored
View
12 templates/admin/web/password/404.html
@@ -0,0 +1,12 @@
+{% extends "admin/base_site.html" %}
+{% load i18n %}
+
+{% block title %}{% trans 'Page not found' %}{% endblock %}
+
+{% block content %}
+
+<h2>{% trans 'Page not found' %}</h2>
+
+<p>{% trans "We're sorry, but the requested page could not be found." %}</p>
+
+{% endblock %}
View
12 templates/admin/web/password/500.html
@@ -0,0 +1,12 @@
+{% extends "admin/base_site.html" %}
+{% load i18n %}
+
+{% block breadcrumbs %}<div class="breadcrumbs"><a href="/">{% trans "Home" %}</a> &rsaquo; {% trans "Server error" %}</div>{% endblock %}
+
+{% block title %}{% trans 'Server error (500)' %}{% endblock %}
+
+{% block content %}
+<h1>{% trans 'Server Error <em>(500)</em>' %}</h1>
+<p>{% trans "There's been an error. It's been reported to the site administrators via e-mail and should be fixed shortly. Thanks for your patience." %}</p>
+
+{% endblock %}
View
2  templates/admin/web/password/batch.html
@@ -35,7 +35,7 @@
{% endif %}{% endif %}
{% endblock %}
<h1>Add lots of passwords</h1>
-<p>I don't really know how large the file can be.</p>
+<p>Number of unprocessed in queue: <strong>{{num_unprocessed}}</strong></p>
<form enctype="multipart/form-data" action="" method="post" id="batch_form">{% csrf_token %}{% block form_top %}{% endblock %}
<div>
{{ form }}
View
2  web/forms.py
@@ -4,6 +4,6 @@
class PasswordsForm(forms.ModelForm):
class Meta:
model = Password
- exclude = ('unique','password')
+ exclude = ('unique','password', 'processed')
passwords = forms.FileField()
View
0  web/management/__init__.py
No changes.
View
0  web/management/commands/__init__.py
No changes.
View
23 web/management/commands/process.py
@@ -0,0 +1,23 @@
+from django.core.management.base import BaseCommand, CommandError
+from web.models import Password
+from django.db import transaction
+#from web.models import PasswordUnique
+
+
+class Command(BaseCommand):
+ args = ''
+ help = 'Processes passwords in queue'
+
+ def handle(self, *args, **options):
+
+ transaction.enter_transaction_management()
+ transaction.managed(True)
+
+ unprocessed = Password.objects.all().filter(processed = False)[:1000]
+ for p in unprocessed:
+ p.process_unique()
+ p.save()
+
+ transaction.commit()
+
+
View
65 web/models.py
@@ -58,7 +58,6 @@ def pass_strength(self):
if len([x for x in self.unique if not x.isalnum()]) > 0:
conditions_met += 1
self.strength.add(self.STRENGTH_CHARS)
-
return conditions_met
class Password(models.Model):
@@ -66,7 +65,8 @@ class Password(models.Model):
password = models.CharField(max_length = 1000)
source = models.ForeignKey(Source)
- unique = models.ForeignKey(PasswordUnique)
+ unique = models.ForeignKey(PasswordUnique, blank = True, null = True)
+ processed = models.BooleanField(default = False)
def __unicode__(self):
@@ -75,38 +75,39 @@ def __unicode__(self):
def save(self, *args, **kwargs):
if self.pk is None:
- # Increment the number of passwords in the source:
- # [this could be slow for batch insert]
-
- if 'batch' not in kwargs.keys():
-
- self.source.count += 1
- self.source.save()
- else:
- del kwargs['batch']
-
- # Insert/increment PasswordUnique:
+
- # If we are saving a record:
- try:
- # We are checking if the password was already entered:
-
- pu = PasswordUnique.objects.get(unique = self.password)
- pu.count += 1
- pu.save()
- self.unique = pu
-
-
- except PasswordUnique.DoesNotExist:
- # If the password is does not yet exist we insert it:
- pu = PasswordUnique()
- pu.unique = self.password
- pu.save()
- pu.pass_strength()
- self.unique = pu
+ if 'batch' in kwargs.keys():
+ del kwargs['batch']
+ # We just save the password... unique processing will be don by a cron job
+ super(Password, self).save(*args, **kwargs)
+ return
- super(Password, self).save(*args, **kwargs)
+ # Increment the number of passwords in the source:
+ self.source.count += 1
+ self.source.save()
+
+ self.process_unique()
+
+ super(Password, self).save(*args, **kwargs)
+
+ def process_unique(self):
+
+ # We are checking if the password was already entered:
+ if(PasswordUnique.objects.all().filter(unique = self.password).count > 0):
+
+ pu = PasswordUnique()
+ pu.unique = self.password
+ pu.save()
+ pu.pass_strength()
+ self.unique = pu
+
+ else:
+ pu = PasswordUnique.objects.get(unique = self.password)
+ pu.count += 1
+ pu.save()
+ self.unique = pu
- # Editing passwords is currently not supported
+ self.processed = True
View
4 web/views_admin.py
@@ -15,6 +15,7 @@ def batch(request):
# Insert the passwords:
for chunk in request.FILES['passwords']:
+
ps = Password(password=chunk.rstrip('\r\n'), source_id=request.POST['source'])
ps.save(batch = True)
@@ -29,7 +30,8 @@ def batch(request):
return HttpResponseRedirect('/admin/web/password')
- return render_to_response('admin/web/password/batch.html', {'form': PasswordsForm(),},
+ return render_to_response('admin/web/password/batch.html', {'form': PasswordsForm(),
+ 'num_unprocessed': Password.objects.all().filter(processed = False).count()},
context_instance=RequestContext(request))
Please sign in to comment.
Something went wrong with that request. Please try again.