def teacheradmin_announcements_add_or_edit(request, course_slug, announ_id=None, announ_slug=None): if announ_id is None: announcement = None course = get_object_or_404(Course, slug=course_slug) else: announcement = get_object_or_404(Announcement, id=announ_id) course = announcement.course is_enrolled = course.students.filter(id=request.user.id).exists() if request.method == 'POST': form = AnnouncementForm(request.POST, instance=announcement) if form.is_valid(): announcement = form.save(commit=False) slug = slugify(announcement.title) max_length = announcement._meta.get_field_by_name('slug')[0].max_length if len(slug) >= max_length: slug = slug[:max_length - 1] announcement.slug = slug announcement.course = course announcement.save() if form.cleaned_data['send_email']: me = MassiveEmail.objects.create_from_announcement(announcement) me.send_in_batches(send_massive_email_task) messages.success( request, _("The email has been queued, and it will be send in batches to every student in the course."), ) return HttpResponseRedirect( reverse("teacheradmin_announcements_view", args=[course_slug, announcement.id, announcement.slug])) else: form = AnnouncementForm(instance=announcement) return render_to_response('teacheradmin/announcement_edit.html', { 'course': course, 'is_enrolled': is_enrolled, 'form': form, 'announcement': announcement, }, context_instance=RequestContext(request))
def teacheradmin_announcements_add_or_edit(request, course_slug, announ_id=None, announ_slug=None): if announ_id is None: announcement = None course = get_object_or_404(Course, slug=course_slug) else: announcement = get_object_or_404(Announcement, id=announ_id) course = announcement.course is_enrolled = course.students.filter(id=request.user.id).exists() students = course.students.count() data = None if request.method == 'POST': data = request.POST massive_emails = course.massive_emails.all() form = AnnouncementForm(data=data, instance=announcement, course=course) remain_send_emails = form.remain_send_emails(massive_emails) if form.is_valid(): announcement = form.save() messages.success(request, _("The announcement was created successfully.")) if form.cleaned_data.get('send_email', None): messages.success( request, _("The email has been queued, and it will be send in batches to every student in the course.") ) return HttpResponseRedirect( reverse("teacheradmin_announcements_view", args=[course_slug, announcement.id, announcement.slug])) return render_to_response('teacheradmin/announcement_edit.html', { 'course': course, 'is_enrolled': is_enrolled, 'form': form, 'announcement': announcement, 'remain_send_emails': remain_send_emails, 'students': students }, context_instance=RequestContext(request))
def teacheradmin_announcements_add_or_edit(request, course_slug, announ_id=None, announ_slug=None): if announ_id is None: announcement = None course = get_object_or_404(Course, slug=course_slug) else: announcement = get_object_or_404(Announcement, id=announ_id) course = announcement.course is_enrolled = course.students.filter(id=request.user.id).exists() if request.method == 'POST': form = AnnouncementForm(request.POST, instance=announcement) if form.is_valid(): announcement = form.save(commit=False) slug = slugify(announcement.title) max_length = announcement._meta.get_field_by_name( 'slug')[0].max_length if len(slug) >= max_length: slug = slug[:max_length - 1] announcement.slug = slug announcement.course = course announcement.save() if form.cleaned_data['send_email']: me = MassiveEmail.objects.create_from_announcement( announcement) me.send_in_batches(send_massive_email_task) messages.success( request, _("The email has been queued, and it will be send in batches to every student in the course." ), ) return HttpResponseRedirect( reverse("teacheradmin_announcements_view", args=[course_slug, announcement.id, announcement.slug])) else: form = AnnouncementForm(instance=announcement) return render_to_response('teacheradmin/announcement_edit.html', { 'course': course, 'is_enrolled': is_enrolled, 'form': form, 'announcement': announcement, }, context_instance=RequestContext(request))