示例#1
0
文件: views.py 项目: DAIEtsinf/webdai
def createArea_view(request):
    if request.method == 'POST':
        # Si el method es post, obtenemos los datos del formulario
        form = AreaForm(request.POST)

        # Comprobamos si el formulario es valido
        if form.is_valid():
            # En caso de ser valido, obtenemos los datos del formulario.
            # form.cleaned_data obtiene los datos limpios y los pone en un
            # diccionario con pares clave/valor, donde clave es el nombre del campo
            # del formulario y el valor es el valor si existe.
            cleaned_data = form.cleaned_data
            nombre_data = cleaned_data.get('nombre')
            # E instanciamos un objeto Area
            area_model = Area()
            area_model.nombre = nombre_data
            area_model.save()
            # Ahora, redireccionamos a la pagina accounts/gracias.html
            # Pero lo hacemos con un redirect.
            return redirect(reverse('web.areasAdmin'))
    else:
        # Si el mthod es GET, instanciamos un objeto RegistroUserForm vacio
        form = AreaForm()
    # Creamos el contexto
    context = {'form': form}
    # Y mostramos los datos
    return render(request, 'noticias/createArea.html', context)
示例#2
0
    def post(self):
        """
        Features
        Method to create a new Feature
        ---
        tags:
          - Features
        parameters:
          - in: body
            name: body
            schema:
              $ref: '#/definitions/FeatureToUpdate'
        responses:
          201:
            description: The updated feature
            schema:
              $ref: '#/definitions/Feature'              
        """
        request_dict = request.get_json()
        if not request_dict:
            response = {'message': 'No input data provided'}
            return response, status.HTTP_400_BAD_REQUEST
        errors = feature_schema.validate(request_dict)
        if errors:
            return errors, status.HTTP_400_BAD_REQUEST
        try:
            # check the client and the area, if we receive values that doesn't exists, create them in the db
            client_name = request_dict['client']['name']
            client = Client.query.filter_by(name=client_name).first()
            if client is None:
                client = Client(name=client_name)
                db.session.add(client)

            area_name = request_dict['area']['name']
            area = Area.query.filter_by(name=area_name).first()
            if area is None:
                area = Area(name=area_name)
                db.session.add(area)

            feature = Feature(title=request_dict['title'],
                              description=request_dict['description'],
                              target_date=request_dict['target_date'],
                              client_priority=request_dict['client_priority'],
                              client=client,
                              area=area)

            # check that no features of the same client have the same priority, when we add the new feature, the session will commit
            other_features = Feature.query.filter_by(client_id=client.id).all()
            feature.adjust_features_priority(other_features)
            feature.add(feature)

            # query this feature from the database
            query = Feature.query.get(feature.id)
            result = feature_schema.dump(query).data
            return result, status.HTTP_201_CREATED
        except SQLAlchemyError as e:
            db.session.rollback()
            resp = {"error": str(e)}
            return resp, status.HTTP_400_BAD_REQUEST
示例#3
0
 def patch(self, id):
     """
     Areas
     Method to update a single Area
     ---
     tags:
       - Areas
     parameters:
       - name: id
         in: path
         type: int
         required: true
         description: The area Id
       - in: body
         name: body
         schema:
           id: AreaToUpdate
           properties:
             name:
               type: string
               description: The name of the Area
               default: "Area Name String"
     responses:
       200:
         description: The updated area
         schema:
           $ref: '#/definitions/Area'              
     """
     area = Area.query.get_or_404(id)
     area_dict = request.get_json()
     if not area_dict:
         resp = {'message': 'No input data provided'}
         return resp, status.HTTP_400_BAD_REQUEST
     errors = area_schema.validate(area_dict)
     if errors:
         return errors, status.HTTP_400_BAD_REQUEST
     try:
         if 'name' in area_dict:
             area_name = area_dict['name']
             if Area.is_unique(id=id, name=area_name):
                 area.name = area_name
             else:
                 response = {
                     'error': 'A area with the same name already exists'
                 }
                 return response, status.HTTP_400_BAD_REQUEST
         area.update()
         return self.get(id)
     except SQLAlchemyError as e:
         db.session.rollback()
         resp = {"error": str(e)}
         return resp, status.HTTP_400_BAD_REQUEST
示例#4
0
def createArea_view(request):
    if request.method == 'POST':
        # Si el method es post, obtenemos los datos del formulario
        form = AreaForm(request.POST)

        # Comprobamos si el formulario es valido
        if form.is_valid():
            cleaned_data = form.cleaned_data
            nombre_data = cleaned_data.get('nombre')
            area_model = Area()
            area_model.nombre = nombre_data
            area_model.save()
            # Ahora, redireccionamos a la pagina accounts/gracias.html
            # Pero lo hacemos con un redirect.
            return redirect(reverse('accounts.areasAdmin'))
    else:
        # Si el mthod es GET, instanciamos un objeto RegistroUserForm vacio
        form = AreaForm()
    # Creamos el contexto
    context = {'form': form}
    # Y mostramos los datos
    return render(request, 'accounts/createArea.html', context)
示例#5
0
 def post(self):
     """
     Areas
     Method to create a new Area
     ---
     tags:
       - Areas
     parameters:
       - in: body
         name: body
         schema:
           $ref: '#/definitions/AreaToUpdate'
     responses:
       201:
         description: The updated area
         schema:
           $ref: '#/definitions/Area'              
     """
     request_dict = request.get_json()
     if not request_dict:
         resp = {'message': 'No input data provided'}
         return resp, status.HTTP_400_BAD_REQUEST
     errors = area_schema.validate(request_dict)
     if errors:
         return errors, status.HTTP_400_BAD_REQUEST
     area_name = request_dict['name']
     if not Area.is_unique(id=0, name=area_name):
         response = {'error': 'A area with the same name already exists'}
         return response, status.HTTP_400_BAD_REQUEST
     try:
         area = Area(area_name)
         area.add(area)
         query = Area.query.get(area.id)
         result = area_schema.dump(query).data
         return result, status.HTTP_201_CREATED
     except SQLAlchemyError as e:
         db.session.rollback()
         resp = {"error": str(e)}
         return resp, status.HTTP_400_BAD_REQUEST
 def create_areas(self):
     for a in areas:
         if Area.is_unique(id=0, name=a):
             area = Area(a)
             area.add(area)