Ejemplo n.º 1
0
def add_concept(request):
    new_con = Concept()

    new_con.title = request.POST['title']
    new_con.description = request.POST['description']
    new_con.pub_date = datetime.datetime.now()
    new_con.date_modified = new_con.pub_date
    new_con.id = bson.ObjectId()
    new_con.author = request.user

    cat_assoc = CategoryAssociation()
    cat = Category.objects.get(id=request.POST['category_id'])
    cat_assoc.category = cat
    new_con.categories.append(cat_assoc)

    form_properties = get_new_properties(request, True)

    for form_property in form_properties:
        #ugly upsert (in case its not in db)
        Feature.objects(
            title=form_property.title,
            is_property=True,
            property_type=form_property.property_type
        ).update_one(
            set__title=form_property.title,
            set__is_property=True,
            set__property_type=form_property.property_type,
            upsert=True)

        new_feature = Feature.objects.filter(
            title=form_property.title,
            is_property=True,
            property_type=form_property.property_type
        )[0]

        new_property = Property(
            feature=new_feature,
            property_id=bson.ObjectId(),
            value=form_property.value)
        new_con.properties.append(new_property)

    new_con.save()

    return HttpResponseRedirect(reverse('concepts:conceptdetail', args=(new_con.id,)))