Add epub importer to profile
[booki.git] / lib / booki / account / views.py
blob5098f43f467079477b9ddfbf902efaf0f4ef1fcb
1 from django.shortcuts import render_to_response
2 from django.core.paginator import Paginator, InvalidPage, EmptyPage
3 from django.conf import settings
4 from django.http import Http404, HttpResponse, HttpResponseRedirect
6 from django.contrib.auth.decorators import login_required
8 from django import forms
10 # this should probahly just list all accounts
12 def view_accounts(request):
13 return HttpResponse("AJME MENI", mimetype="text/plain")
15 # signout
17 def signout(request):
18 from django.contrib import auth
20 auth.logout(request)
22 return HttpResponseRedirect("/")
24 # signin
26 def signin(request):
27 from django.contrib import auth
29 if(request.POST.has_key("username")):
30 user = auth.authenticate(username=request.POST["username"], password=request.POST["password"])
32 if user:
33 auth.login(request, user)
34 return HttpResponseRedirect("/accounts/%s/" % request.POST["username"])
36 return HttpResponseRedirect("/")
38 # register user
40 def register(request):
41 from django.contrib.auth.models import User
42 from django.contrib import auth
44 user = User.objects.create_user(username=request.POST["username"],
45 email=request.POST["email"],
46 password=request.POST["password"])
47 user.save()
49 user2 = auth.authenticate(username=request.POST["username"], password=request.POST["password"])
51 auth.login(request, user2)
53 return HttpResponseRedirect("/accounts/%s/" % request.POST["username"])
55 # project form
57 class ProjectForm(forms.Form):
58 title = forms.CharField(required=False)
59 license = forms.ChoiceField(choices=(('1', '1'), ))
61 def __init__(self, *args, **kwargs):
62 super(ProjectForm, self).__init__(*args, **kwargs)
64 from booki.editor import models
65 self.fields['license'].initial = 'Unknown'
66 self.fields['license'].choices = [ (elem.abbrevation, elem.name) for elem in models.License.objects.all().order_by("name")]
69 class ImportForm(forms.Form):
70 archive_id = forms.CharField(required=False)
73 class ImportEpubForm(forms.Form):
74 url = forms.CharField(required=False)
77 def view_profile(request, username):
78 from django.contrib.auth.models import User
79 from booki.editor import models
81 from django.template.defaultfilters import slugify
83 user = User.objects.get(username=username)
85 if request.method == 'POST':
86 project_form = ProjectForm(request.POST)
87 import_form = ImportForm(request.POST)
88 epub_form = ImportEpubForm(request.POST)
89 espri_url = "http://objavi.flossmanuals.net/espri.cgi"
91 if import_form.is_valid() and import_form.cleaned_data["archive_id"] != "":
92 from booki.editor import common
94 common.importBookFromURL(espri_url + "?mode=zip&book="+import_form.cleaned_data["archive_id"], createTOC = True)
96 if epub_form.is_valid() and epub_form.cleaned_data["url"] != "":
97 from booki.editor import common
98 common.importBookFromURL(espri_url + "?mode=zip&url="+epub_form.cleaned_data["url"], createTOC = True)
100 if project_form.is_valid() and project_form.cleaned_data["title"] != "":
101 title = project_form.cleaned_data["title"]
102 url_title = slugify(title)
103 license = project_form.cleaned_data["license"]
105 project = models.Project(url_name = url_title,
106 name = title,
107 status = 0)
108 project.save()
110 status = models.ProjectStatus(project=project, name="not published",weight=0)
111 status.save()
113 import datetime
114 # should check for errors
115 lic = models.License.objects.get(abbrevation=license)
117 book = models.Book(project = project,
118 url_title = url_title,
119 title = title,
120 status=status,
121 license=lic,
122 published = datetime.datetime.now())
123 book.save()
125 return HttpResponseRedirect("/accounts/%s/" % username)
126 else:
127 project_form = ProjectForm()
128 import_form = ImportForm()
129 epub_form = ImportEpubForm()
131 books = models.Book.objects.all()
133 return render_to_response('account/view_profile.html', {"request": request, "user": user, "project_form": project_form, "import_form": import_form, "epub_form": epub_form, "books": books})