def editNews(request, news_id):
  ''' Edit a news article '''
  try:
    news = News.objects.get(pk=news_id)
    if request.method == 'POST': # form submitted
      form = NewsFormAdmin(request.POST, request.FILES, instance=news) # repopulate form with edited data
      if form.is_valid(): # valid edits
        #form.save() # save and redirect back to the news page
        news.pub_date = datetime.datetime.now()
        news.title = form.cleaned_data['title']
        news.article = form.cleaned_data['article']
        if request.FILES.get('picture', False):
          (filename, content) = handle_uploaded_picture(request.FILES['picture'], MAX_IMG_SIZE, THUMB_IMG_SIZE)
          news.image.delete()
          news.image.save(filename[0], content[0])
          news.image_thumb.delete()
          news.image_thumb.save(filename[1], content[1])
        news.outside_link = form.cleaned_data['outside_link'] if form.cleaned_data['outside_link'] else None
        news.is_published = True if form.cleaned_data['is_published'] else False
        news.save()
        return HttpResponseRedirect('/admin/news/')
    else: # form not submitted, create populated form for editing
      form = NewsFormAdmin(instance=news)
  except News.DoesNotExist:
    raise Http404
  except:
    raise Http404
  return render_to_response('news/customadmin/edit.html', {'form': form, 'news_id': news_id, 'ndate': news.pub_date}, context_instance=RequestContext(request))
def createUser(request, redirectURL='/admin/accounts/'):
    if request.method == 'POST':
        form = UserFormAdmin(request.POST, request.FILES)
        if form.is_valid():
            (user, password) = form.save()
            if request.FILES.get('picture', False):
                (filename,
                 content) = handle_uploaded_picture(request.FILES['picture'],
                                                    MAX_IMG_SIZE,
                                                    THUMB_IMG_SIZE)
                userprofile = user.profile
                userprofile.picture.delete()
                userprofile.picture.save(filename[0], content[0])
                userprofile.picture_thumb.delete()
                userprofile.picture_thumb.save(filename[1], content[1])
                userprofile.save()
            user.save()
            #emailPassword(user.email, password)
            mail_send([user.email], password, 'mail/password_reset')
            return HttpResponseRedirect(redirectURL)
    else:
        form = UserFormAdmin()
    return render_to_response('accounts/customadmin/create.html',
                              {'form': form},
                              context_instance=RequestContext(request))
def editUser(request, user_id, redirectURL='/admin/accounts/'):
    user = get_object_or_404(User, pk=user_id)
    userprofile = user.get_profile()
    if request.method == 'POST':
        form = UserFormAdmin(request.POST, request.FILES)
        if form.is_valid():
            (user, userprofile) = form.update_user(user_id)
            if form.cleaned_data[
                    'reset_password'] == True and form.cleaned_data[
                        'is_active'] == True:
                password = form.do_reset_password(user_id)
                email = form.cleaned_data['email']
                mail_send([email], password, 'mail/password_reset')
            if request.FILES.get('picture', False):
                (filename,
                 content) = handle_uploaded_picture(request.FILES['picture'],
                                                    MAX_IMG_SIZE,
                                                    THUMB_IMG_SIZE)
                userprofile.picture.delete()
                userprofile.picture.save(filename[0], content[0])
                userprofile.picture_thumb.delete()
                userprofile.picture_thumb.save(filename[1], content[1])
            userprofile.save()
            user.save()
            return HttpResponseRedirect(redirectURL)
    data = {
        'username': user.username,
        'email': user.email,
        'firstname': user.first_name,
        'lastname': user.last_name,
        'is_active': user.is_active,
        'is_staff': user.is_staff,
        'is_superuser': user.is_superuser,
        'is_leadership_team': userprofile.is_leadership_team,
        'is_vizlab_staff': userprofile.is_vizlab_staff,
        'is_demo_presenter': userprofile.demo_presenter,
        'is_scheduler': userprofile.is_scheduler,
        'is_event_emails_only': userprofile.extra_get_event_emails,
        'is_force_no_emails': userprofile.force_no_emails,
        'formal_prefix': userprofile.formal_name,
        'faculty_webpage': userprofile.faculty_webpage,
        'staff_position_number': userprofile.staff_position,
        'faculty_position': userprofile.staff_faculty_position,
        'rank_order': userprofile.rank_order,
        'picture': userprofile.picture,
        'reset_password': False
    }
    form = UserFormAdmin(data)
    return render_to_response('accounts/customadmin/edit.html', {
        'form': form,
        'user_id': user_id,
        'redirectURL': redirectURL
    },
                              context_instance=RequestContext(request))
def newNews(request):
  ''' Create news article '''
  if request.method == 'POST': # form submitted
    form = NewsFormAdmin(request.POST, request.FILES)
    if form.is_valid():
      fd = form.cleaned_data
      news = News(title=fd['title'], article=fd['article'],
              outside_link=fd['outside_link'])
      news.save()
      if request.FILES.get('picture', False):
        (filename, content) = handle_uploaded_picture(request.FILES['picture'], MAX_IMG_SIZE, THUMB_IMG_SIZE)
        news.image.save(filename[0], content[0])
        news.image_thumb.save(filename[1], content[1])
      news.is_published=True
      news.save()
      return HttpResponseRedirect('/admin/news/')
  else: # form not submitted, create populated form for editing
      form = NewsFormAdmin()
  return render_to_response('news/customadmin/create.html', {'form': form}, context_instance=RequestContext(request))
def editUser(request, user_id, redirectURL='/admin/accounts/'):
  user = get_object_or_404(User, pk=user_id)
  userprofile = user.get_profile()
  if request.method == 'POST':
    form = UserFormAdmin(request.POST, request.FILES)
    if form.is_valid():
      (user, userprofile) = form.update_user(user_id)
      if form.cleaned_data['reset_password'] == True and form.cleaned_data['is_active'] == True:
        password = form.do_reset_password(user_id)
        email = form.cleaned_data['email']
        mail_send([email], password, 'mail/password_reset')
      if request.FILES.get('picture', False):
        (filename, content) = handle_uploaded_picture(request.FILES['picture'], MAX_IMG_SIZE, THUMB_IMG_SIZE)
        userprofile.picture.delete()
        userprofile.picture.save(filename[0], content[0])
        userprofile.picture_thumb.delete()
        userprofile.picture_thumb.save(filename[1], content[1])
      userprofile.save()
      user.save()
      return HttpResponseRedirect(redirectURL)
  data = {'username': user.username,
          'email': user.email,
          'firstname': user.first_name,
          'lastname': user.last_name,
          'is_active': user.is_active,
          'is_staff': user.is_staff,
          'is_superuser': user.is_superuser,
          'is_leadership_team': userprofile.is_leadership_team,
          'is_vizlab_staff': userprofile.is_vizlab_staff,
          'is_demo_presenter': userprofile.demo_presenter,
          'is_scheduler': userprofile.is_scheduler,
          'is_event_emails_only': userprofile.extra_get_event_emails,
          'is_force_no_emails': userprofile.force_no_emails,
          'formal_prefix': userprofile.formal_name,
          'faculty_webpage': userprofile.faculty_webpage,
          'staff_position_number': userprofile.staff_position,
          'faculty_position': userprofile.staff_faculty_position,
          'rank_order': userprofile.rank_order,
          'picture': userprofile.picture,
          'reset_password': False}
  form = UserFormAdmin(data)
  return render_to_response('accounts/customadmin/edit.html', {'form': form, 'user_id': user_id, 'redirectURL': redirectURL}, context_instance=RequestContext(request))
def createUser(request, redirectURL='/admin/accounts/'):
  if request.method == 'POST':
    form = UserFormAdmin(request.POST, request.FILES)
    if form.is_valid():
      (user, password) = form.save()
      if request.FILES.get('picture', False):
        (filename, content) = handle_uploaded_picture(request.FILES['picture'], MAX_IMG_SIZE, THUMB_IMG_SIZE)
        userprofile = user.profile
        userprofile.picture.delete()
        userprofile.picture.save(filename[0], content[0])
        userprofile.picture_thumb.delete()
        userprofile.picture_thumb.save(filename[1], content[1])
        userprofile.save()
      user.save()
      #emailPassword(user.email, password)
      mail_send([user.email], password, 'mail/password_reset')
      return HttpResponseRedirect(redirectURL)
  else:
    form = UserFormAdmin()
  return render_to_response('accounts/customadmin/create.html', {'form': form}, context_instance=RequestContext(request))
def editNews(request, news_id):
    ''' Edit a news article '''
    try:
        news = News.objects.get(pk=news_id)
        if request.method == 'POST':  # form submitted
            form = NewsFormAdmin(
                request.POST, request.FILES,
                instance=news)  # repopulate form with edited data
            if form.is_valid():  # valid edits
                #form.save() # save and redirect back to the news page
                news.pub_date = datetime.datetime.now()
                news.title = form.cleaned_data['title']
                news.article = form.cleaned_data['article']
                if request.FILES.get('picture', False):
                    (filename, content) = handle_uploaded_picture(
                        request.FILES['picture'], MAX_IMG_SIZE, THUMB_IMG_SIZE)
                    news.image.delete()
                    news.image.save(filename[0], content[0])
                    news.image_thumb.delete()
                    news.image_thumb.save(filename[1], content[1])
                news.outside_link = form.cleaned_data[
                    'outside_link'] if form.cleaned_data[
                        'outside_link'] else None
                news.is_published = True if form.cleaned_data[
                    'is_published'] else False
                news.save()
                return HttpResponseRedirect('/admin/news/')
        else:  # form not submitted, create populated form for editing
            form = NewsFormAdmin(instance=news)
    except News.DoesNotExist:
        raise Http404
    except:
        raise Http404
    return render_to_response('news/customadmin/edit.html', {
        'form': form,
        'news_id': news_id,
        'ndate': news.pub_date
    },
                              context_instance=RequestContext(request))
def newNews(request):
    ''' Create news article '''
    if request.method == 'POST':  # form submitted
        form = NewsFormAdmin(request.POST, request.FILES)
        if form.is_valid():
            fd = form.cleaned_data
            news = News(title=fd['title'],
                        article=fd['article'],
                        outside_link=fd['outside_link'])
            news.save()
            if request.FILES.get('picture', False):
                (filename,
                 content) = handle_uploaded_picture(request.FILES['picture'],
                                                    MAX_IMG_SIZE,
                                                    THUMB_IMG_SIZE)
                news.image.save(filename[0], content[0])
                news.image_thumb.save(filename[1], content[1])
            news.is_published = True
            news.save()
            return HttpResponseRedirect('/admin/news/')
    else:  # form not submitted, create populated form for editing
        form = NewsFormAdmin()
    return render_to_response('news/customadmin/create.html', {'form': form},
                              context_instance=RequestContext(request))