Ejemplo n.º 1
0
def create_adventure(request):

  message = ''
  user = request.user
  userProfile = user.get_profile()
  
  adventureForm = AdventureForm()

  adventure = Adventure()

  ImageFormSet = modelformset_factory(Image, form=ImageForm, extra=1)

  ImageFormSet.form = staticmethod(curry(ImageForm, adventure))
          
  formset = ImageFormSet(queryset=Image.objects.none())

  adventure_pictures = ''

    
  if request.method == "POST":
    adventureForm = AdventureForm(request.POST)
    print adventureForm
    if adventureForm.is_valid():
        cost = 0.0
        try:
          adventureForm.user = userProfile
          if request.POST.get('cost'):
            cost = Decimal(request.POST['cost'])

          location_formatted_address = request.POST.get('location_formatted_address')
          location_lat = request.POST.get('location_lat')
          location_lon = request.POST.get('location_lon')

                         
          adventure = Adventure.objects.create(name = request.POST['name'],
                                                 cost =  cost,
                                                 user = userProfile,
                                                 tips = request.POST['tips'],
                                                 location_formatted_address = location_formatted_address,
                                                 location_lat = location_lat,
                                                 location_lon = location_lon)

          ImageFormSet.form = staticmethod(curry(ImageForm, adventure))
          
          formset = ImageFormSet(request.POST, request.FILES, queryset=Image.objects.none())
          
          if formset.is_valid():
            instances = formset.save(commit=False)
            for image in instances:
              try:
                image.adventure = adventure
                image.save()
                
              except Image.DoesNotExist:
                message = 'Image does not exit'

          message = 'success'

          return HttpResponseRedirect(adventure.get_absolute_url())
            
        except Exception, e:    
          message = e
        # Do something.
    else:
      message = 'Invalid form data'
Ejemplo n.º 2
0
  def create(self, request):
    # This option is for when the input comes
    # structured like in JSON format for example    
    if request.content_type:

      print request

      adventureForm = AdventureForm()
      
      adventure = Adventure()

      ImageFormSet = modelformset_factory(Image, form=ImageForm, extra=1)

      ImageFormSet.form = staticmethod(curry(ImageForm, adventure))
            
      formset = ImageFormSet(queryset=Image.objects.none())

      adventure_pictures = ''
      
      userProfile = get_object_or_404(UserProfile, pk=request.POST['userprofile_id'])

      #tools = simplejson.loads(request.POST['tools'])

      if request.method == "POST":
        adventureForm = AdventureForm(request.POST)

        print adventureForm
        
        if adventureForm.is_valid():
          cost = 0.0
          try:
            adventureForm.user = userProfile
            if request.POST.get('cost'):
              cost = Decimal(request.POST['cost'])

              location_formatted_address = request.POST.get('location_formatted_address')
              location_lat = request.POST.get('location_lat')
              location_lon = request.POST.get('location_lon')
                         
              adventure = Adventure.objects.create(name = request.POST['name'],
                                                   cost =  cost,
                                                   user = userProfile,
                                                   tips = request.POST['tips'],
                                                   location_formatted_address = location_formatted_address,
                                                   location_lat = location_lat,
                                                   location_lon = location_lon)

              ImageFormSet.form = staticmethod(curry(ImageForm, adventure))
              request.POST['form-TOTAL_FORMS']=1
              request.POST['form-INITIAL_FORMS']=0
              
              formset = ImageFormSet(request.POST, request.FILES, queryset=Image.objects.none())
              
              if formset.is_valid():
                instances = formset.save(commit=False)
                for image in instances:
                  try:
                    image.adventure = adventure
                    image.save()
                    
                  except Image.DoesNotExist:
                    message = 'Image does not exit'
                    return rc.BAD_REQUEST

              else:
                message = "Form is not valid"
                return rc.BAD_REQUEST

          except Exception, e:    
            message = e
            return rc.BAD_REQUEST
               
      return rc.CREATED