def save(self, commit=True): obj = super(ProfileForm, self).save(commit=False) setattr(obj, field, get_profile(request)) if commit: obj.save() return obj
def save_model(self, request, post, form, change): if not change: post.created_by = get_profile(request) else: post.edited_by = get_profile(request) was_draft = True if change: was_draft = Post.objects.get(pk=post.pk).status == Post.statuses.draft if was_draft and post.is_published: post.publicate() else: post.save() form.save_m2m()
def watchlist_pad(context, post, labels=None): profile = get_profile(context['request']) action = profile.has_subscription('new_comment', post) and 'unsubs' or 'subs' return { 'action': action, 'labels': labels and labels.split('/'), 'form': SubscriptionForm(initial={'post': post.pk, 'action': action}) }
def preview(request, post_id): post = get_object_or_404(Post.objects.all(), pk=post_id) if not post.is_published\ and get_profile(request) not in (post.created_by, post.edited_by): return http.HttpResponseForbidden('Forbidden') return { "post": post, }
def __init__(self, initial=None, *args, **kwargs): if initial is None: initial = {} initial.update(get_profile(request).__dict__) super(ProfileForm, self).__init__( initial=initial, request=request, *args, **kwargs ) self.valid_openid = False
def __init__(self, *args, **kwargs): if filter_field: initial = { filter_field: get_profile(request).filter } else: initial = {} initial.update(kwargs.pop('initial', {})) super(ProfileForm, self).__init__(initial=initial, *args, **kwargs)
def index(request): profile = get_profile(request) return { 'profile': profile, 'comments_page': paginate( watchlist.get_subcription_comments(profile), request.page, settings.TURBION_BLOG_POSTS_PER_PAGE ), 'subscriptions': profile.subscriptions.filter(event__name='new_comment').\ order_by('date').distinct() }
def watchlist_action(request): if request.method != 'POST': return http.HttpResponseNotAllowed('POST request required') profile = get_profile(request) form = SubscriptionForm(user=profile, data=request.POST) if form.is_valid(): action, post = form.process() return http.HttpResponseRedirect(request.REQUEST.get('next', reverse('turbion_watchlist'))) return http.HttpResponseBadRequest('Post not found or bad action')
def tag(request, tag_slug): _tag = get_object_or_404(Tag.active, slug=tag_slug) posts = _tag.posts.all() if not get_profile(request).is_trusted(): posts = posts.filter(showing=Post.show_settings.everybody) post_page = paginate( posts, request.page, settings.TURBION_BLOG_POSTS_PER_PAGE ) return { "tag": _tag, "post_page": post_page }
def blog(request): posts = Post.published.all() if not get_profile(request).is_trusted(): posts = posts.filter(showing=Post.show_settings.everybody) post_page = paginate( posts, request.page, settings.TURBION_BLOG_POSTS_PER_PAGE ) context = { "post_page": post_page } return context
def _decor(request, *args, **kwargs): if request.user.is_authenticated(): query_set = Post.objects.all() else: query_set = Post.published.all() if not get_profile(request).is_trusted(): query_set = query_set.filter(showing=Post.show_settings.everybody) published_on = dict( published_on__year=int(kwargs.pop('year_id')), published_on__month=int(kwargs.pop('month_id')), published_on__day=int(kwargs.pop('day_id')), ) query_set = query_set.filter(slug=kwargs.pop('post_slug'), **published_on) post = get_object_or_404(query_set) return view_func(request, post=post, *args, **kwargs)
def edit_profile(request): profile = get_profile(request) updated = False if request.method == "POST": profile_form = forms.ProfileForm( data=request.POST, instance=profile ) if profile_form.is_valid(): profile_form.save() updated = True else: profile_form = forms.ProfileForm(instance=profile) return { "profile_form": profile_form, "profile": profile, "just_created": "just_created" in request.GET, "updated": updated, }
def get_user(self): from django.contrib.auth import login form_data = {'nickname': self.cleaned_data['openid']} form_data.update(extract_profile_data(request)) profile = get_profile(request) if not profile.is_authenticated(): profile = Profile.objects.create_guest_profile(**form_data) if not self.valid_openid: profile.backend = '%s.%s' % ( ModelBackend.__module__, ModelBackend.__name__ ) login(request, profile) else: self.created_profile = profile else: profile.__dict__.update(form_data) profile.save() return profile
def get_object(self, bits): if len(bits) == 1: query_set = Post.published.all() if not get_profile(self.request).is_trusted(): query_set = query_set.filter(showing=Post.show_settings.everybody) return get_object_or_404(query_set, pk=bits[0])
def items(self): posts = Post.published.all().select_related() if not get_profile(self.request).is_trusted(): posts = posts.filter(showing=Post.show_settings.everybody) return posts.order_by("-published_on")[:10]
def _do_comment(request, post, defaults={}, comment=None): profile = get_profile(request) if not post.allow_comment_from(profile): return http.HttpResponseRedirect(post.get_absolute_url())#FIXME: add message if comment and profile not in (comment.created_by, post.created_by): return http.HttpResponseRedirect(comment.get_absolute_url()) if request.method == 'POST': form = forms.CommentForm( data=request.POST, request=request, instance=comment ) antispam.process_form_init(request, form) if form.is_valid(): new_comment = form.save(False) if post: new_comment.post = post new_comment.__dict__.update(defaults) if 'view' in request.POST: comment = new_comment else: if not new_comment.created_by.trusted: new_comment.status = post.get_comment_status(new_comment) antispam.process_form_submit( request, form, new_comment, post ) new_comment.save() if new_comment.is_published: if comment: signal = signals.comment_edited else: signal = signals.comment_added signal.send( sender=Comment, comment=new_comment, instance=post ) if new_comment.status == Comment.statuses.spam: new_comment.created_by.message_set.create( message=gettext('Your comment added to moderation queue') ) else: new_comment.subscribe_author(email=False) if comment is None: # send only if created new_comment.emit_event() if form.need_auth_redirect(): return http.HttpResponseRedirect( form.auth_redirect(new_comment.get_absolute_url()) ) if new_comment.status == Comment.statuses.spam: return http.HttpResponseRedirect(post.get_absolute_url() + '#messages') else: return http.HttpResponseRedirect(new_comment.get_absolute_url()) else: form = forms.CommentForm( request=request, instance=comment ) antispam.process_form_init(request, form) return { "comment_form": form, "comment": comment }
def is_identity_profile(request): return get_profile(request).pk == int(settings.TURBION_OPENID_IDENTITY_PROFILE)
def save_model(self, request, comment, form, change): if change: comment.edited_by = get_profile(request) comment.save()
def combine_profile_form_with(form_class, request, field='created_by',\ fields=None, filter_field='text_filter'): if not get_profile(request).is_authenticated(): from turbion.bits.openid.forms import OpenidLoginForm as BaseForm class ProfileForm(form_class, BaseForm): openid = forms.CharField(label=_('Name or OpenID'), required=True) def __init__(self, initial=None, *args, **kwargs): if initial is None: initial = {} initial.update(get_profile(request).__dict__) super(ProfileForm, self).__init__( initial=initial, request=request, *args, **kwargs ) self.valid_openid = False def get_user(self): from django.contrib.auth import login form_data = {'nickname': self.cleaned_data['openid']} form_data.update(extract_profile_data(request)) profile = get_profile(request) if not profile.is_authenticated(): profile = Profile.objects.create_guest_profile(**form_data) if not self.valid_openid: profile.backend = '%s.%s' % ( ModelBackend.__module__, ModelBackend.__name__ ) login(request, profile) else: self.created_profile = profile else: profile.__dict__.update(form_data) profile.save() return profile def need_auth_redirect(self): return self.valid_openid def save(self, commit=True): obj = super(ProfileForm, self).save(False) user = self.get_user() setattr(obj, field, user) if commit: obj.save() return obj def clean_openid(self): value = self.cleaned_data["openid"] if value.startswith('http://') or value.startswith('https://'): try: value = super(ProfileForm, self).clean_openid() self.valid_openid = True except forms.ValidationError: pass return value return ProfileForm else: class ProfileForm(form_class): def __init__(self, *args, **kwargs): if filter_field: initial = { filter_field: get_profile(request).filter } else: initial = {} initial.update(kwargs.pop('initial', {})) super(ProfileForm, self).__init__(initial=initial, *args, **kwargs) def need_auth_redirect(self): return False def save(self, commit=True): obj = super(ProfileForm, self).save(commit=False) setattr(obj, field, get_profile(request)) if commit: obj.save() return obj return ProfileForm