def experiment(request, experiment_name_url): # Change underscores in the experiment name to spaces. experiment_name = eye_safe(experiment_name_url) experiment = get_object_or_404(Experiment, title=experiment_name) # materials: each unique kind of material necessary for the experiment. materials = experiment.materials.all() tags = experiment.tags.all().exclude(name="") # material_locations: dict with entries {material: [instance1, instance2...]}. material_locations = {} for m in materials: material_locations[m] = Material.objects.filter(name=m) for t in tags: t.url = url_safe(t.name) context_dict = { 'experiment': experiment, 'materials': materials, 'material_locations': material_locations, 'tags': tags, 'procedure': clean_html(experiment.procedure), 'images': Image.objects.filter(experiment=experiment), 'resources': Resource.objects.filter(experiment=experiment), 'links': Link.objects.filter(experiment=experiment) } # Go render the response and return it to the client. return render(request, 'inventory/experiment.html', context_dict)
def add_comment(request): """ Function uses ajax requests to add comments. """ if not request.user.is_authenticated(): return HttpResponse(status=307) if request.is_ajax(): resource_id = request.POST.get('r_id', '') type_id = request.POST.get('t_id', '') cleaned_comment = django_wysiwyg.clean_html(request.POST['comment']) date = timezone.now() # UTC format to save in database mimetype = 'application/json' model = None type_name = TypeTable.objects.get(id=type_id).type_name if type_name == 'learn_links' or type_name == 'check_links': model = Links elif type_name == 'note_notes': model = Notes if model: model.objects.get(id=resource_id) Comments(user=request.user, comment=cleaned_comment, resource_id=resource_id, type_id=type_id, date=date).save() date = timezone.localtime(date).strftime('%b %d, %Y %H:%M:%S') # timezone format to send in ajax request data = json.dumps({"username": request.user.username, "date": date}) return HttpResponse(data, mimetype)
def add_comment(request): """ Function uses ajax requests to add comments. """ if not request.user.is_authenticated(): return HttpResponse(status=307) if request.is_ajax(): resource_id = request.POST.get('r_id', '') type_id = request.POST.get('t_id', '') cleaned_comment = django_wysiwyg.clean_html(request.POST['comment']) date = timezone.now() # UTC format to save in database mimetype = 'application/json' model = None type_name = TypeTable.objects.get(id=type_id).type_name if type_name == 'learn_links' or type_name == 'check_links': model = Links elif type_name == 'note_notes': model = Notes if model: model.objects.get(id=resource_id) Comments(user=request.user, comment=cleaned_comment, resource_id=resource_id, type_id=type_id, date=date).save() date = timezone.localtime(date).strftime( '%b %d, %Y %H:%M:%S') # timezone format to send in ajax request data = json.dumps({"username": request.user.username, "date": date}) return HttpResponse(data, mimetype)
def form_valid(self, form): form.instance.author = self.request.user logger.debug(form.instance.body_text) form.instance.body_text = django_wysiwyg.clean_html(form.instance.body_text) logger.debug("Body_text cleaned version:") logger.debug(form.instance.body_text) return super(EntryCreate, self).form_valid(form)
def edit_note(request): """ Function to edit existing note. Checks user rights before editing. """ note_id = request.GET.get('id', '') note = Notes.objects.get(id=note_id) error = '' user = User.objects.get(id=request.user.id) if note.user_id != user.id: # additional check in case of cheating error = 'This note is private, only author can edit this note.' if request.method == 'POST': cleaned_body = django_wysiwyg.clean_html(request.POST['body']) form = NoteForm(request.POST, instance=note) form.instance.user = user form.instance.body = cleaned_body form.instance.date = timezone.now() if form.is_valid(): form.save() return HttpResponseRedirect('/show_note/?id={}'.format(note_id), RequestContext(request)) else: form = NoteForm(instance=note) return render_to_response('notes/edit_note.html', { 'form': form, 'note_id': note_id, 'error': error }, RequestContext(request))
def save(self, *args, **kwargs): #saving company slug self.company_slug = slugify(self.company) #saving job url if not self.ad_url: location = '' if listings_settings.LISTINGS_LOCATION_IN_URL: location = '-' + slugify(self.get_location()) self.ad_url = slugify(self.title) + location + '-' + ''.join(random.choice('gqwcz') for x in range(2)) self.description_text = strip_tags(self.description) # when saving with textile if listings_settings.LISTINGS_MARKUP_LANGUAGE == 'textile': import textile self.description = mark_safe( force_unicode( textile.textile( smart_str(self.description)))) # or markdown elif listings_settings.LISTINGS_MARKUP_LANGUAGE == 'markdown': import markdown self.description = mark_safe( force_unicode( markdown.markdown( smart_str(self.description)))) # or wysiwyg elif listings_settings.LISTINGS_MARKUP_LANGUAGE == 'html': import django_wysiwyg self.description = mark_safe( force_unicode( django_wysiwyg.clean_html(self.description))) # or else, disallow all markup else: self.description = self.description_text self.description = strip_disallowed_tags(self.description) super(Job, self).save(*args, **kwargs) current_site = Site.objects.get(pk=django_settings.SITE_ID) if current_site not in self.sites.all(): self.sites.add(current_site)
def add_note(request): """ Function to add new note. """ redirect_to = request.GET.get('next', '/home/') if request.method == 'POST': user = User.objects.get(id=request.user.id) cleaned_body = django_wysiwyg.clean_html(request.POST['body']) form = NoteForm(request.POST) form.instance.user = user form.instance.body = cleaned_body if form.is_valid(): form.save() messages.info(request, "Your note was successfully saved.") return HttpResponseRedirect(redirect_to, RequestContext(request)) else: form = NoteForm() return render_to_response('notes/add_note.html', {'form': form, 'redirect_to': redirect_to}, RequestContext(request))
def add_note(request): """ Function to add new note. """ redirect_to = request.GET.get('next', '/home/') if request.method == 'POST': user = User.objects.get(id=request.user.id) cleaned_body = django_wysiwyg.clean_html(request.POST['body']) form = NoteForm(request.POST) form.instance.user = user form.instance.body = cleaned_body if form.is_valid(): form.save() messages.info(request, "Your note was successfully saved.") return HttpResponseRedirect(redirect_to, RequestContext(request)) else: form = NoteForm() return render_to_response('notes/add_note.html', { 'form': form, 'redirect_to': redirect_to }, RequestContext(request))
def post(self, request, *args, **kwargs): data = request.POST.copy() data['sites'] = 1 data['content'] = django_wysiwyg.clean_html(data['content']) data['flatPage'] = ExtendedFlatPage form = self.form(data) if form.is_valid(): try: form.save() messages.success(request, _('New site page has been added')) return redirect('backoffice_manage_flatpages') except Exception as e: messages.error(request, _('An error has occurred')) else: # error = get_form_error_messages(form) messages.error(request, _('Please correct the errors, and resubmit the form.'), extra_tags='form_error') self.info['form'] = form return render(request, self.template_name, self.info)
def edit_note(request): """ Function to edit existing note. Checks user rights before editing. """ note_id = request.GET.get('id', '') note = Notes.objects.get(id=note_id) error = '' user = User.objects.get(id=request.user.id) if note.user_id != user.id: # additional check in case of cheating error = 'This note is private, only author can edit this note.' if request.method == 'POST': cleaned_body = django_wysiwyg.clean_html(request.POST['body']) form = NoteForm(request.POST, instance=note) form.instance.user = user form.instance.body = cleaned_body form.instance.date = timezone.now() if form.is_valid(): form.save() return HttpResponseRedirect('/show_note/?id={}'.format(note_id), RequestContext(request)) else: form = NoteForm(instance=note) return render_to_response('notes/edit_note.html', {'form': form, 'note_id': note_id, 'error': error}, RequestContext(request))
def editor(request): writer_articles_list = Articles.objects.all() paginator = Paginator(writer_articles_list, 25) page = request.GET.get('page') try: articles = paginator.page(page) except PageNotAnInteger: # If page is not an integer, deliver first page. articles = paginator.page(1) except EmptyPage: # If page is out of range (e.g. 9999), deliver last page of results. articles = paginator.page(paginator.num_pages) if request.method == 'POST': form = ArticleForm(request.POST) if form.is_valid(): article_title = form.cleaned_data['title'] article_content = form.cleaned_data['content'] user, created = Users.objects.get_or_create( username=request.user.username, ) # If user updates article try: article = Articles.objects.get(author=user, title=article_title) article.content = article_content # Parse article paragraph count article.num_para = _get_paragraph_count(article_content) article.save() except Articles.DoesNotExist: Articles.objects.get_or_create( author=user, title=article_title, content=article_content, num_para=_get_paragraph_count(article_content)) writer_articles_list = Articles.objects.all() paginator = Paginator(writer_articles_list, 25) page = request.GET.get('page') try: articles = paginator.page(page) except PageNotAnInteger: # If page is not an integer, deliver first page. articles = paginator.page(1) except EmptyPage: # If page is out of range (e.g. 9999), deliver last page of results. articles = paginator.page(paginator.num_pages) return { 'form': form, 'article_content': clean_html(article_content), 'article_title': clean_html(article_title), 'articles_list': articles, 'logged_in': _logged_in(request) } else: form = ArticleForm() return { 'form': form, 'articles_list': articles, 'logged_in': _logged_in(request) }
def editor(request): writer_articles_list = Articles.objects.all() paginator = Paginator(writer_articles_list, 25) page = request.GET.get('page') try: articles = paginator.page(page) except PageNotAnInteger: # If page is not an integer, deliver first page. articles = paginator.page(1) except EmptyPage: # If page is out of range (e.g. 9999), deliver last page of results. articles = paginator.page(paginator.num_pages) if request.method == 'POST': form = ArticleForm(request.POST) if form.is_valid(): article_title = form.cleaned_data['title'] article_content = form.cleaned_data['content'] user, created = Users.objects.get_or_create( username = request.user.username, ) # If user updates article try: article = Articles.objects.get( author = user, title = article_title ) article.content = article_content # Parse article paragraph count article.num_para = _get_paragraph_count(article_content) article.save() except Articles.DoesNotExist: Articles.objects.get_or_create( author = user, title = article_title, content = article_content, num_para = _get_paragraph_count(article_content) ) writer_articles_list = Articles.objects.all() paginator = Paginator(writer_articles_list, 25) page = request.GET.get('page') try: articles = paginator.page(page) except PageNotAnInteger: # If page is not an integer, deliver first page. articles = paginator.page(1) except EmptyPage: # If page is out of range (e.g. 9999), deliver last page of results. articles = paginator.page(paginator.num_pages) return { 'form': form, 'article_content': clean_html(article_content), 'article_title': clean_html(article_title), 'articles_list': articles, 'logged_in': _logged_in(request) } else: form = ArticleForm() return { 'form': form, 'articles_list': articles , 'logged_in': _logged_in(request) }
def clean_content(self): return clean_html(self.cleaned_data['content'])
def pre_save_interview_receiver(sender, instance, *args, **kwargs): if not instance.slug: instance.slug = create_slug(instance) if instance.content: instance.read_time = get_read_time(clean_html(instance.content))