Created
May 7, 2013 17:42
-
-
Save JerzySpendel/5534549 to your computer and use it in GitHub Desktop.
GPGHost
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Create your views here. | |
from django.http import HttpResponse | |
from forms.register import RegisterForm | |
from forms.login import LoginForm | |
from django.shortcuts import render,redirect | |
from main.models import User,Key | |
import hashlib, os | |
from django.template import RequestContext | |
from forms.GPGForm import GPGFormNew | |
import threading | |
def prepareInputData(key,gpg): | |
result = gpg.gen_key_input(key_type=key.key_type,key_length=key.key_length,name_real=key.key_real,name_comment=key.key_comment,name_email=key.key_email,passphrase=key.key_password) | |
return result | |
class GenerateKey(threading.Thread): #Wątek, w którym będzie generowany klucz GPG, proces wymaga czasu i entropii bajtów. | |
def __init__(self,zadanie, argument): | |
threading.Thread.__init__(self) | |
self.zadanie = zadanie | |
self.argument = argument | |
def run(self): | |
self.zadanie(self.argument) | |
def manage(request): | |
if request.method == 'POST': | |
form = GPGFormNew(request.POST) | |
key_to_save = form.save(commit=False) | |
key_to_save.ext_id = request.session['user'] | |
key_to_save.key_type = "RSA" | |
key_to_save.key_length = 2048 | |
gpg = RequestContext(request)['gpg'] | |
input = prepareInputData(key_to_save,gpg) | |
proces = GenerateKey(gpg.gen_key,input) | |
proces.start() | |
key_to_save.save() | |
return HttpResponse("OK.") | |
else: | |
a = Key(key_email=request.session['user'].email,ext_id=request.session['user'],key_length=2048,key_real="Name and surname",key_comment="Additional description for this key",key_password="password") | |
form = GPGFormNew(instance=a) | |
return render(request,'manage.html',{'form':form}) | |
def logout(request): | |
if request.session.get('logged',False): | |
request.session['logged'] = False | |
request.session['user'] = None | |
return render(request,'base.html',{'message_m':'Have a nice day'}) | |
else: | |
return render(request,'base.html',{'message_m':"You're not logged in"}) | |
def index(request): | |
return render(request,"index.html",{}) | |
def login(request): | |
if request.method == 'POST': | |
form = LoginForm(request.POST) | |
if form.is_valid(): | |
password = hashlib.md5() | |
password.update(form.cleaned_data['password'].encode('utf-8')) | |
password = str(password.hexdigest()) | |
login = form.cleaned_data['login'] | |
users = User.objects.filter(login=login) | |
if users.count() > 1 or users.count() == 0: | |
return HttpResponse("Error, more than one or 0 users with such a username") | |
user = User.objects.get(login=login) | |
if user: | |
if password == user.password: | |
request.session.set_expiry(60*60*60) | |
request.session['logged'] = True | |
request.session['user'] = user | |
return redirect('/main') | |
else: | |
return HttpResponse("Bad password or login") | |
else: | |
return HttpResponse("There's no such a user") | |
else: | |
return HttpResponse("Bad password") | |
else: | |
return render(request,"login.html",{'form':LoginForm}) | |
def register(request): | |
if request.method == 'POST': | |
form = RegisterForm(request.POST) | |
if form.is_valid(): | |
password = hashlib.md5() | |
password.update(form.cleaned_data['password'].encode('utf-8')) | |
password = str(password.hexdigest()) | |
login = form.cleaned_data['login'] | |
email = form.cleaned_data['email'] | |
if not User.objects.filter(login=login).count(): | |
user = User.objects.create(login=login,password=password,email=email) | |
user.save() | |
os.mkdir('/home/jurek/PycharmProjects/gpghosting/files/'+login) | |
return render(request,'gratz.html',{'login':login,'gratulowac':True}) | |
else: | |
return render(request,'gratz.html',{'gratulowac':False}) | |
else: | |
return render(request,'register.html',{'form':RegisterForm}) | |
def main(request): | |
return render(request,"main.html") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment