Exemplo n.º 1
0
 def test_collection_form(self):
     post_data = {'field_data': '[{"field_type":"T","label":"","sort_order":0,"display":true},{"field_type":"I","label":"","sort_order":1,"display":true},{"field_type":"T","label":"","sort_order":2,"display":false}]', 'csrfmiddlewaretoken': '38vLxTwts8C4pUcFqoOgQAq3eXgAdpro', 'field_type1': 'text', 'description': 'lots of math', 'title': 'math 454'}
     # testing the form
     form = CollectionForm(post_data)
     self.assertEqual(form.is_valid(), True)
     form1 = CollectionForm({})
     self.assertEqual(form1.is_valid(), False)
Exemplo n.º 2
0
def create(request, collection_id=None):
    # is it a post?
    message = '';
    if request.method == 'POST':

        #for key in request.POST:
        #    value = request.POST[key]
        #    message += "{0} => {1}<br>".format(key, value)
        #return HttpResponse(message)
        
        if 'collection_id' in request.POST:
            collection = Collection.objects.get(id=request.POST['collection_id'])
            collectionForm = CollectionForm(request.POST, instance=collection)
            
        else:
            collectionForm = CollectionForm(request.POST)

        if collectionForm.is_valid():
            collection = collectionForm.save()

            # create the formset from the base fieldform
            #FieldFormSet = formset_factory(FieldForm)
            # decode json
            data = json.loads(request.POST['field_data'])            
            #return HttpResponse(repr(data))

            # is it an edit?
            # get all ids from data
            editList = []
            for d in data:
                if 'id' in d:
                    editList.append(d['id']);
            if len(editList):
                # then run through all ids in the db
                # if they are not in edit list, delete them
                existingFields = Field.objects.filter(collection=collection.id)
                for ef in existingFields:
                    if ef.id not in editList:
                        ef.delete()
                

            # run through field_data
            for d in data:
                if 'id' in d:
                    field = Field.objects.get(id=d['id'])
                    fieldForm = FieldForm(d, instance=field)
                else:
                    fieldForm = FieldForm(d)
                
                f = fieldForm.save(commit=False)
                # this is how relationships have to be done -- forms cannot handle this
                # so you have to do it directly at the model
                f.collection = collection
                f.save()
                
                

            return redirect(index)
        else:
            return render(request, 'collections/create.html')
    
    # is it an edit?
    elif collection_id:
        collection = Collection.objects.get(id=collection_id)
        fields = collection.field_set.all().order_by('sort_order')
        if collection:
            return render(request, 'collections/create.html', {"collection": collection, "fields": fields})
        else:
            raise ViewDoesNotExist("Course does not exist.")
    
            
    else:
        return render(request, 'collections/create.html')