def login_view(request): username = request.POST['username'] password = request.POST['password'] remember = request.POST.get("remember_me", False) next_url = request.POST.get("next", None) form = PreRegistrationForm() user = authenticate(username=username, password=password) if user is not None: if user.is_active: login(request, user) setup_session(request) if next_url: return HttpResponseRedirect("/") return redirect(reverse( "dashboard-index", args=[])) else: return rr("index.html", {"regform": form, "msgclass": "error", "next": next_url, "msg": _("Your account is disabled.")}, context_instance=RequestContext(request)) else: return rr("index.html", {"regform": form, "msgclass": "error", "next": next_url, "msg": _("Username or Password is invalid.")}, context_instance=RequestContext(request))
def change_avatar(req): msg = '' if req.method == 'GET': avatar_form = new_avatar_form() try: avatar_form.instance = req.user.get_profile() except: pass elif req.method == 'POST': avatar_form = new_avatar_form(req.POST) if avatar.objects.filter(user=req.user): _a = avatar.objects.filter(user=req.user)[0] else : _a = avatar() _a.user = req.user if req.FILES.has_key('avatar'): _img = req.FILES['avatar'] if _img.size < 50*1024: _a.avatar = upload_and_replace('avatar',_img,str(_a.user.id)) else: msg = "图片大小要小于50kb!" return rr('user/change_avatar.htm', {'avatar_form':avatar_form,'msg':msg, }, context_instance = RequestContext(req)) _a.desc = req.POST['desc'] _a.save() return rd('/') return rr('user/change_avatar.htm', {'avatar_form':avatar_form,'msg':msg, }, context_instance = RequestContext(req))
def edit_category(request, id): """ Edit a post. """ site = get_site(request) try: instance = Category.objects.get(site=site, id=id) except Category.DoesNotExist: raise Http404() form = CategoryForm(site, request.POST, instance=instance) if form.is_valid(): from django.db import IntegrityError try: m = form.save() except IntegrityError: return rr("ublog/dashboard/form.html", {"url": reverse("edit-category", args=[id]), "title": _("Edit Category"), "form": form, "msg": _("A category with this title already exists."), "msgclass": "error"}, context_instance=RequestContext(request)) return HttpResponseRedirect(reverse("categories", args=[])) else: form = CategoryForm(site, instance=instance) return rr("ublog/dashboard/form.html", {"url": reverse("edit-category", args=[id]), "title": _("Edit Category"), "form": form}, context_instance=RequestContext(request))
def post_register(request): """ Complete the registeration by asking user to fill extra information. """ user = None if "user" in request.session: user = request.session["user"] else: return HttpResponseForbidden() if request.method == "POST": form = PostRegistrationForm(request.POST) if form.is_valid(): try: form.save(user) except form.PasswordError, e: form.errors["password1"] = unicode(e) form.errors["password2"] = unicode(e) return rr("post_registeration.html", {"form": form}, context_instance=RequestContext(request)) user = authenticate(username=user.username, password=form.cleaned_data["password1"]) login(request, user) return HttpResponseRedirect(reverse("auth.views.profile", args=[])) else: return rr("post_registeration.html", {"form": form}, context_instance=RequestContext(request))
def login_view(request): if request.method == "POST": form = LoginForm(request.POST) next_ = request.POST.get("next", "/") if form.is_valid(): username = form.cleaned_data['username'] password = form.cleaned_data['password'] user = authenticate(username=username, password=password) if user is not None: if user.is_active: login(request, user) return HttpResponseRedirect(next_) else: return rr("registration/login.html", {"msg": _("Your account is not active.")}, context_instance=RequestContext(request)) else: return rr("registration/login.html", {"msg": _("username or password is incorrect.")}, context_instance=RequestContext(request)) else: next_ = request.GET.get("next", "/") if request.user.is_authenticated(): return HttpResponseRedirect(next_) return rr("registration/login.html", {"next": next_}, context_instance=RequestContext(request))
def new(req): #new user msg = '' if req.method == 'GET': avatar_form = new_avatar_form() userAddForm = UserCreationForm() elif req.method == 'POST': userAddForm = UserCreationForm(req.POST) avatar_form = new_avatar_form(req.POST) if userAddForm.is_valid(): _a = avatar() _a.desc = req.POST['desc'] if req.FILES.has_key('avatar'): # 如果有上传图像 _img = req.FILES['avatar'] if _img.size < 50*1024: # 图片大小合适 userAddForm.save() _a.avatar = upload_and_replace('avatar',_img,str(userAddForm.instance.id)) else: # 图片过大 msg = "图片大小要小于50kb!" return rr('user/new.htm', {'userAddForm':userAddForm, 'avatar_form':avatar_form,'msg':msg, }, context_instance = RequestContext(req)) else: #没有上传图像,保存新建用户的请求,保存avatar。 userAddForm.save() _a.user = userAddForm.instance _a.save() return rd('/') return rr('user/new.htm', {'userAddForm':userAddForm, 'avatar_form':avatar_form,'msg':msg, }, context_instance = RequestContext(req))
def add_category(request): """ Add view. """ site = get_site(request) if request.method == "POST": form = CategoryForm(site, request.POST) if form.is_valid(): from django.db import IntegrityError try: m = form.save() except IntegrityError: return rr("ublog/dashboard/form.html", {"url": reverse("add-category", args=[]), "title": _("Add Category"), "form": form, "msg": _("A category with this title already exists."), "msgclass": "error"}, context_instance=RequestContext(request)) return HttpResponseRedirect(reverse("categories", args=[])) else: form = CategoryForm(site) return rr("ublog/dashboard/form.html", {"url": reverse("add-category", args=[]), "title": _("Add Category"), "form": form}, context_instance=RequestContext(request))
def pre_register(request): """ First step of registeration process. In this step we process just username and email address and send a verification mail. """ if request.method == "POST": form = PreRegistrationForm(request.POST) if form.is_valid(): data = form.cleaned_data # collect queries in a single transaction with transaction.commit_on_success(): email = User.objects.filter(email=data["email"]) user = User.objects.filter(username=data["username"]) if email or user: # returning suitable error if email or user already registered if email: form.errors["email"] = ( _("This Email already registered."), ) if user: form.errors["usernmae"] = ( _("This Username already registered."), ) return rr("pre_registeration.html", {"form": form}, context_instance=RequestContext(request)) else: # Create a user and send the verification mail user = User(username=data["username"], email=data["email"], is_active=False) user.save() # create verification code and save it in DB verification_code = Verification(user=user) code = verification_code.create_verification_code() vmail = VerificationMail(user, code, request.META["HTTP_HOST"]) vmail.send() return rr("verification_sent.html") else: return rr("pre_registeration.html", {"form": form}, context_instance=RequestContext(request)) else: form = PreRegistrationForm() return rr("pre_registeration.html", {"form": form}, context_instance=RequestContext(request))
def verificate_email(request, code): """ Get the verification code and verify the user mail. """ # Look for given verification code try: verification = Verification.objects.get(code=code) except Verification.DoesNotExist: # always riase a 404 status code for invalid code raise Http404() # if verification code sent ins last 48 hours if verification.is_valid(): # Activating user user = verification.user user.is_active = True user.save() request.session["user"] = user verification.delete() form = PostRegistrationForm() return rr("post_registeration.html", {"form": form}, context_instance=RequestContext(request)) else: # If code expired. verification.delete() raise Http404()
def ajax(request): """ load news with ajax """ lang = translation.get_language() news = News.objects.filter(lang=lang).order_by('-date') try: page = int(request.GET.get('page', '1')) except ValueError: page = 1 try: count = int(request.GET.get('count', '10')) except ValueError: count = 10 if not 1 < count < 20: count = 10 p = Paginator(news, count) try: return rr('ajax.html', {'news_list': p.page(page)}, context_instance=RequestContext(request)) except (EmptyPage, InvalidPage): return HttpResponse('')
def erb(request, path): """ Serv the erb templates for backbone """ template = "erb/%s" % path.replace("..", "") return rr(template, {}, context_instance=RequestContext(request))
def view_post(request, slug): """ View a single post. """ domain = request.get_host() try: site = Site.objects.get(domain=domain) except Site.DoesNotExist: raise Http404() post = get_object_or_404(Post, slug=slug) from django.contrib.comments.models import Comment comments = Comment.objects.filter(site=site, content_type__name='Post', content_type__app_label="ultra_blog", object_pk=post.pk, is_public=True) return rr("ublog/view_post.html", {"post": post, "comments_list": comments}, context_instance=RequestContext(request))
def view_category(request, category): """ Render the lastest blog entries in specific category. """ ppp = Setting.get_setting("post_per_page") try: cat = Category.objects.get(slug=category) except Category.DoesNotExist: raise Http404() post_list = cat.ultra_blog_posts.all() paginator = Paginator(post_list, ppp) try: page = int(request.GET.get('page', '1')) except ValueError: page = 1 try: posts = paginator.page(page) except (EmptyPage, InvalidPage): postss = paginator.page(paginator.num_pages) return rr('ublog/index.html', {"posts": posts, "category_name": category, "types": post_types.get_types_complex(), "rssfeed": "/blog/feed/category/%s/" % category}, context_instance=RequestContext(request))
def view_tag(request, tag): """ Render the lastest blog entries with specifc tag. """ from tagging.models import Tag, TaggedItem ppp = Setting.get_setting("post_per_page") try: tagobj = Tag.objects.get(name=tag) except Tag.DoesNotExist: raise Http404() post_list = TaggedItem.objects.get_by_model(Post, tagobj) paginator = Paginator(post_list, ppp) try: page = int(request.GET.get('page', '1')) except ValueError: page = 1 try: posts = paginator.page(page) except (EmptyPage, InvalidPage): postss = paginator.page(paginator.num_pages) return rr('ublog/index.html', {"posts": posts, "types": post_types.get_types_complex(), "rssfeed": "/blog/feed/"}, context_instance=RequestContext(request))
def books_index(request): """ Main index of registered books. """ books = Book.objects.all().order_by("weight") book_per_page = 4 try: book_per_page = settings.BOOK_IN_PAGE except AttributeError: pass paginator = Paginator(books, book_per_page) try: page = int(request.GET.get('page', '1')) except ValueError: page = 1 try: books_list = paginator.page(page) except (EmptyPage, InvalidPage): # if provided page value in GET was out of range books_list = paginator.page(paginator.num_pages) return rr("books.html", {"books": books_list}, context_instance=RequestContext(request))
def index(request): """ Temprorary index page. """ return rr("index.html", {}, context_instance=RequestContext(request))
def widgets_list(self, request): widgets = list(self.dashboard.widgets) #widgets = [i.name for i in widgets] return rr("dashboard/widgets/add_widgets.html", {"widgets": widgets, "self": self}, context_instance=RequestContext(request))
def edit(self, request): """ Edit view for basic user profile. """ from daarmaan.server.forms import EditBasicProfile if not request.user.is_authenticated(): return HttpResponseRedirect('/?next=%s' % reverse(self.edit, args=[])) user = request.user profile = self._get_user_profile(user) if request.method == "POST": form = EditBasicProfile(user, instance=profile, data=request.POST) if form.is_valid(): form.save() return HttpResponseRedirect(reverse("profile-home", args=[])) else: form = EditBasicProfile(user, instance=profile) return rr(self.edit_profile_template, {"form": form, "user": user}, context_instance=RequestContext(request))
def user_homepage(request,user): contexts = context.user_post(request.user) return rr('home.html', {'now':now, 'contexts':contexts, 'user':user}, context_instance=RequestContext(request))
def posts(request): """ Posts index. """ return rr("ublog/dashboard/grid.html", {"url": reverse("ultra_blog.dashboard.views.posts_json", args=[])}, context_instance=RequestContext(request))
def render(self, request): """ Render the final template. """ context = self._prepare_context() context.update(self.extra_context) return rr(self.template, context, context_instance=RequestContext(request))
def config_form(self, request): if request.method == "POST": pass else: form = self.form() return rr(form.template, {"form": form, "self": self}, context_instance=RequestContext(request))
def index(request): """ show the whole index """ request.session['django_language'] faqs = FAQ.objects.all() return rr("faq.html", {"faq": faqs, }, context_instance=RequestContext(request))
def index(request): """ show the whole index """ faqs = FAQ.objects.all() return rr("faq.html", { "faq": faqs, }, context_instance=RequestContext(request))
def ajax_js(request): """ Return a suitable javascript code for given url. """ url = request.GET.get("validator", None) if url: return rr("validator.js", {"url": url}) else: raise Http404()
def projects_index(request): """ project index page """ projects = Project.objects.all().order_by("weight") # god damn b3hnam's server projects = [(i + 1, j) for i, j in enumerate(projects)] # --- return rr("projects.html", {"projects": projects}, context_instance=RequestContext(request))
def book_view(request, slug): """ View of each Book """ try: book = Book.objects.get(slug=slug) except Book.DoesNotExist: raise Http404() return rr("book_view.html", {"book": book}, context_instance=RequestContext(request))
def members_index(request): """ Main index of members. """ members = Member.objects.all().order_by("weight") # god damn b3hnam's server members = [(i + 1, j) for i, j in enumerate(members)] # --- return rr("members.html", {"members": members}, context_instance=RequestContext(request))
def index(request): """ show the whole index """ request.session['django_language'] faqs = FAQ.objects.all() return rr("faq.html", { "faq": faqs, }, context_instance=RequestContext(request))
def index(request): """ index view of tehpug.ir """ lang = translation.get_language() try: page = FirstPage.objects.filter(lang=lang).latest("date") except FirstPage.DoesNotExist: page = None return rr("index.html", {"page": page}, context_instance=RequestContext(request))
def home(req): # _as = article.objects.order_by('-time')[:6] _cats = category.objects.all() _cats_dict = [] for _cat in _cats: _threads = [x._t for x in category_thread.objects.order_by('-_t__time').filter(_c = _cat)[:6]] _cats_dict.append([_cat, _threads]) return rr('home.html', {#'articles':_as, 'categories':_cats_dict,}, context_instance = RequestContext(req))
def on_invalid_form(form): rendered_form = rs("ublog/dashboard/forms.html", {"action": reverse("ultra_blog.dashboard.views.new_post", args=[]), "form": form}, context_instance=RequestContext(request)) return rr("ublog/dashboard/page.html", {"content": rendered_form, "title": _("New Post")}, context_instance=RequestContext(request))
def index(request): """ index view of karajlug.org """ news = News.objects.all().order_by("-date")[:settings.NEWS_LIMIT] try: page = FirstPage.objects.latest("date") except FirstPage.DoesNotExist: page = None return rr("index.html", { "news_list": news, "page": page, }, context_instance=RequestContext(request))
def show_page(request, slug): """ show the page with the given slug """ try: page = Page.objects.get(slug=slug, publish=True) except Page.DoesNotExist: raise Http404() return rr("page.html", { "page": page, "title": "%s | %s" % (_("Karajlug"), page.title) }, context_instance=RequestContext(request))
def news_entry(request, id_=None): """ Show a single news entry. """ if not id_: raise Http404() try: news_ent = News.objects.get(id=id_) except News.DoesNotExist: raise Http404() return rr('news_view.html', {'news': news_ent}, context_instance=RequestContext(request))
def member_view(request, id): """ View each member details. """ from models import MemberDetail try: member = Member.objects.get(id=id) except Member.DoesNotExist: raise Http404() # TODO: lang code here details = MemberDetail.objects.filter(member=member).order_by("weight") return rr("member_view.html", { "member": member, "details": details }, context_instance=RequestContext(request))
def index(request): """ show all the news with pagination. """ news = News.objects.all().order_by('-date') p = Paginator(news, settings.NEWS_LIMIT) try: page = int(request.GET.get('page', '1')) except ValueError: page = 1 try: news_page = p.page(page) except (EmptyPage, InvalidPage): news_page = p.page(p.num_pages) return rr('news_list.html', {'news': news_page}, context_instance=RequestContext(request))
def project_view(request, slug): """ Project view """ try: project = Project.objects.get(slug=slug) except Project.DoesNotExist: raise Http404() if project.vcs: vcsdict = dict(Project.VCS) project.vcs = vcsdict[project.vcs] repos = Repository.objects.filter(project=project).order_by("weight") return rr("project_view.html", { "project": project, "repositories": repos }, context_instance=RequestContext(request))
def index(request): return rr('donate.html', {}, context_instance=RequestContext(request))
def contact(request): return rr("contact.html", context_instance=RequestContext(request))