Esempio n. 1
0
def save_org(request, org_id):
  authenticate(request, VALID_FACTORS)
  
  if request.method != "POST":
    raise Http401
  
  post_dict = request.POST
  
  name = post_dict['name']
  
  def org_with_name_exists_redirect(n):
    try:
      Organization.objects.get(name = n)
      template_args = {
        'title' : 'Error!',
        'message' : "An organization with the name `%s` already exists. Please choose another name." % n,
        'redirect' : '/webapps/organization-manager/my_organizations/'
        }
      return render_to_response('generic/alert-redirect.phtml',template_args, context_instance=RequestContext(request))
    except Organization.DoesNotExist:
      return None
  
  if (org_id != ''):
    try:
      organization = request.user.signator_set.get(id = org_id)
    except Organization.DoesNotExist:
      test = org_with_name_exists_redirect(name)
      if test != None:
        return test
      organization = Organization()
  else:
    test = org_with_name_exists_redirect(name)
    if test != None:
      return test
    organization = Organization()
  
  organization.signator = request.user
  
  for n in post_dict:
    if n in VALID_POST_FIELDS:
      organization.__setattr__(n,post_dict[n])
  organization.public_post_ok = ('on' == post_dict.get('public_post_ok','off'))
  
  organization.save()
  
  return HttpResponsePermanentRedirect('/webapps/organizations/my_organizations/')