Esempio n. 1
0
def _get_adaptor(request, method='GET'):
    request_params = getattr(request, method)
    field_name = request_params.get('field_name', None)
    obj_id = request_params.get('obj_id', None)

    app_label = request_params.get('app_label', None)
    model_name = request_params.get('model_name', None)

    if not field_name or not obj_id or not app_label and module_name:
        return None

    contenttype = ContentType.objects.get(app_label=app_label,
                                          model=model_name)

    model_class = contenttype.model_class()
    obj = get_object_or_404(model_class, pk=obj_id)
    adaptor = request_params.get('adaptor', None)
    class_adaptor = get_adaptor_class(adaptor, obj, field_name)

    filters_to_show = request_params.get('filters_to_show', None)

    kwargs = _convert_params_in_config(
        request_params, ('field_name', 'obj_id', 'app_label', 'model_name',
                         'filters_to_show', 'adaptor'))
    config = class_adaptor.get_config(request, **kwargs)
    adaptor_field = class_adaptor(request, obj, field_name, filters_to_show,
                                  config)
    return adaptor_field
Esempio n. 2
0
def _get_adaptor(request, method='GET'):
    request_params = getattr(request, method)
    field_name = request_params.get('field_name', None)
    obj_id = request_params.get('obj_id', None)

    app_label = request_params.get('app_label', None)
    module_name = request_params.get('module_name', None)

    if not field_name or not obj_id or not app_label and module_name:
        return None

    contenttype = ContentType.objects.get(app_label=app_label,
                                          model=module_name)

    model_class = contenttype.model_class()
    obj = get_object_or_404(model_class,
                            pk=obj_id)
    adaptor = request_params.get('adaptor', None)
    class_adaptor = get_adaptor_class(adaptor, obj, field_name)

    filters_to_show = request_params.get('filters_to_show', None)

    kwargs = _convert_params_in_config(request_params, ('field_name',
                                                        'obj_id',
                                                        'app_label',
                                                        'module_name',
                                                        'filters_to_show',
                                                        'adaptor'))
    config = class_adaptor.get_config(request, **kwargs)
    adaptor_field = class_adaptor(request, obj, field_name,
                                  filters_to_show,
                                  config)
    return adaptor_field
Esempio n. 3
0
    def prepare_context(self, args, kwargs, context):
        expression_to_show = args[0]
        tokens_to_show = expression_to_show.split('|')
        obj_field_name, filters_to_show = tokens_to_show[0], '|'.join(tokens_to_show[1:])
        obj_field_name_split = obj_field_name.split(".")
        obj_context = '.'.join(obj_field_name_split[:-1])
        field_name = obj_field_name_split[-1]
        obj = Variable(obj_context).resolve(context)
        adaptor = kwargs.get('adaptor', None)
        class_adaptor = get_adaptor_class(adaptor, obj, field_name)
        request = context.get('request')

       #print 'expression_to_show: %s' % expression_to_show
       #print 'tokens_to_show: %s' % tokens_to_show
       #print 'obj_field_name: %s' % obj_field_name
       #print 'obj_field_name_split: %s' % obj_field_name_split
       #print 'obj_context: %s' % obj_context
       #print 'field_name: %s' % field_name
       #print 'obj: %s' % obj
       #print 'adaptor: %s' % adaptor
       #print 'class_adaptor: %s' % class_adaptor

        config = class_adaptor.get_config(**kwargs)
        adaptor_field = class_adaptor(request, obj, field_name,
                                               filters_to_show,
                                               config)

        context = {
            'adaptor_field': adaptor_field,
        }
        return context
Esempio n. 4
0
def _get_adaptor(request, method="GET"):
    request_params = getattr(request, method)
    field_name = request_params.get("field_name", None)
    obj_id = request_params.get("obj_id", None)

    app_label = request_params.get("app_label", None)
    module_name = request_params.get("module_name", None)

    if not field_name or not obj_id or not app_label and module_name:
        return None

    contenttype = ContentType.objects.get(app_label=app_label, model=module_name)

    model_class = contenttype.model_class()
    obj = get_object_or_404(model_class, pk=obj_id)
    adaptor = request_params.get("adaptor", None)
    class_adaptor = get_adaptor_class(adaptor, obj, field_name)

    filters_to_show = request_params.get("filters_to_show", None)

    kwargs = _convert_params_in_config(
        request_params, ("field_name", "obj_id", "app_label", "module_name", "filters_to_show", "adaptor")
    )
    config = class_adaptor.get_config(**kwargs)
    adaptor_field = class_adaptor(request, obj, field_name, filters_to_show, config)
    return adaptor_field
 def _test_save_fields(self, model, field_names=None):
     client = self.__client_login()
     obj = model.objects.all()[0]
     module_name = model._meta.module_name
     app_label = model._meta.app_label
     field_names = field_names or model._meta.get_all_field_names()
     for field_name in field_names:
         if field_name in transmeta.get_all_translatable_fields(model):
             field = model._meta.get_field_by_name(transmeta.get_fallback_fieldname(field_name))[0]
         else:
             field = model._meta.get_field_by_name(field_name)[0]
         if field_name == 'id' or field_name.endswith('_id'):  # id or id fk
             continue
         if isinstance(field, models.FileField):
             continue
         url = reverse('inplace_save')
         value = getattr(obj, field_name)
         if isinstance(value, datetime.datetime):
             value = '"1982-11-14 03:13:12"'
         elif isinstance(value, datetime.date):
             value = '"1982-11-14"'
         elif isinstance(value, datetime.time):
             value = '"03:13:12"'
         else:
             if hasattr(value, 'all'):
                 value = [str(obj_rel.pk) for obj_rel in value.model.objects.all()]
             elif isinstance(value, decimal.Decimal):
                 value = float(value) + 10
             elif (isinstance(value, string) and
                   isinstance(field, models.CharField) and
                   not isinstance(field, models.CommaSeparatedIntegerField) and
                   not isinstance(field, models.EmailField)):
                 if field.choices:
                     value = field.choices[0][0]
                 else:
                     value += '_save'
             elif isinstance(field, models.CommaSeparatedIntegerField):
                 value += ',44'
             elif isinstance(field, models.EmailField):
                 value = '*****@*****.**'
             elif isinstance(field, models.ForeignKey):
                 value = field.rel.to.objects.all()[0].pk
             elif isinstance(field, bool):
                 value = not value
             value = json.dumps(value)
         adaptor = get_adaptor_class(obj=obj, field_name=field_name)(None, obj, field_name).name
         data = {'app_label': app_label,
                 'module_name': module_name,
                 'field_name': field_name,
                 'value': value,
                 'obj_id': obj.pk,
                 'adaptor': adaptor}
         response = client.post(url, data)
         self.assertEqual(response.status_code, 200)
         self.assertEqual(json.loads(response.content.decode('utf-8')).get('errors', None), False)
Esempio n. 6
0
    def prepare_context(self, args, kwargs, context):
        expression_to_show = args[0]
        tokens_to_show = expression_to_show.split('|')
        obj_field_name, filters_to_show = tokens_to_show[0], '|'.join(tokens_to_show[1:])
        obj_field_name_split = obj_field_name.split(".")
        obj_context = '.'.join(obj_field_name_split[:-1])
        field_name = obj_field_name_split[-1]
        obj = Variable(obj_context).resolve(context)
        adaptor = kwargs.get('adaptor', None)
        class_adaptor = get_adaptor_class(adaptor, obj, field_name)
        request = context.get('request')

        config = class_adaptor.get_config(**kwargs)
        adaptor_field = class_adaptor(request, obj, field_name,
                                               filters_to_show,
                                               config)

        context = {
            'adaptor_field': adaptor_field,
        }
        return context
Esempio n. 7
0
 def _test_save_fields(self,
                       model,
                       obj=None,
                       field_names=None,
                       client=None,
                       has_error=False):
     client = client or self.__client_login()
     obj = obj or model.objects.all()[0]
     self.assertEqual(model, obj.__class__)
     module_name = get_module_name(model)
     app_label = model._meta.app_label
     field_names = field_names or model._meta.get_all_field_names()
     for field_name in field_names:
         extra_data = {}
         if field_name in transmeta.get_all_translatable_fields(model):
             field = model._meta.get_field_by_name(
                 transmeta.get_fallback_fieldname(field_name))[0]
         else:
             field = model._meta.get_field_by_name(field_name)[0]
         if field_name == 'id' or field_name.endswith('_id'):  # id or id fk
             continue
         url = reverse('inplace_save')
         value = getattr(obj, field_name)
         if isinstance(field, models.FileField):
             value = '"example1.jpg"'
             file_path = os.path.join(settings.MEDIA_ROOT, 'images',
                                      'example1.jpg')
             extra_data['attachment'] = open(file_path, 'rb')
         elif isinstance(value, datetime.datetime):
             value = '"1982-11-14 03:13:12"'
         elif isinstance(value, datetime.date):
             value = '"1982-11-14"'
         elif isinstance(value, datetime.time):
             value = '"03:13:12"'
         else:
             if hasattr(value, 'all'):
                 value = [
                     str(obj_rel.pk)
                     for obj_rel in value.model.objects.all()
                 ]
             elif isinstance(value, decimal.Decimal):
                 value = float(value) + 10
             elif (isinstance(value, string)
                   and isinstance(field, models.CharField) and
                   not isinstance(field, models.CommaSeparatedIntegerField)
                   and not isinstance(field, models.EmailField)):
                 if field.choices:
                     value = field.choices[0][0]
                 else:
                     value += '_save'
             elif isinstance(field, models.CommaSeparatedIntegerField):
                 value += ',44'
             elif isinstance(field, models.EmailField):
                 value = '*****@*****.**'
             elif isinstance(field, models.ForeignKey):
                 value = field.rel.to.objects.all()[0].pk
             elif isinstance(field, bool):
                 value = not value
             value = json.dumps(value)
         adaptor = get_adaptor_class(obj=obj,
                                     field_name=field_name)(None, obj,
                                                            field_name).name
         data = {
             'app_label': app_label,
             'module_name': module_name,
             'field_name': field_name,
             'value': value,
             'obj_id': obj.pk,
             'adaptor': adaptor
         }
         data.update(extra_data)
         response = client.post(url, data)
         self.assertEqual(response.status_code, 200)
         self.assertEqual(
             bool(
                 json.loads(response.content.decode('utf-8')).get(
                     'errors', None)), has_error)
Esempio n. 8
0
 def _test_save_fields(self, model, obj=None, field_names=None, client=None, has_error=False):
     client = client or self.__client_login()
     obj = obj or model.objects.all()[0]
     self.assertEqual(model, obj.__class__)
     module_name = model._meta.module_name
     app_label = model._meta.app_label
     field_names = field_names or model._meta.get_all_field_names()
     for field_name in field_names:
         extra_data = {}
         if field_name in transmeta.get_all_translatable_fields(model):
             field = model._meta.get_field_by_name(transmeta.get_fallback_fieldname(field_name))[0]
         else:
             field = model._meta.get_field_by_name(field_name)[0]
         if field_name == "id" or field_name.endswith("_id"):  # id or id fk
             continue
         url = reverse("inplace_save")
         value = getattr(obj, field_name)
         if isinstance(field, models.FileField):
             value = '"example1.jpg"'
             file_path = os.path.join(settings.MEDIA_ROOT, "images", "example1.jpg")
             extra_data["attachment"] = open(file_path, "rb")
         elif isinstance(value, datetime.datetime):
             value = '"1982-11-14 03:13:12"'
         elif isinstance(value, datetime.date):
             value = '"1982-11-14"'
         elif isinstance(value, datetime.time):
             value = '"03:13:12"'
         else:
             if hasattr(value, "all"):
                 value = [str(obj_rel.pk) for obj_rel in value.model.objects.all()]
             elif isinstance(value, decimal.Decimal):
                 value = float(value) + 10
             elif (
                 isinstance(value, string)
                 and isinstance(field, models.CharField)
                 and not isinstance(field, models.CommaSeparatedIntegerField)
                 and not isinstance(field, models.EmailField)
             ):
                 if field.choices:
                     value = field.choices[0][0]
                 else:
                     value += "_save"
             elif isinstance(field, models.CommaSeparatedIntegerField):
                 value += ",44"
             elif isinstance(field, models.EmailField):
                 value = "*****@*****.**"
             elif isinstance(field, models.ForeignKey):
                 value = field.rel.to.objects.all()[0].pk
             elif isinstance(field, bool):
                 value = not value
             value = json.dumps(value)
         adaptor = get_adaptor_class(obj=obj, field_name=field_name)(None, obj, field_name).name
         data = {
             "app_label": app_label,
             "module_name": module_name,
             "field_name": field_name,
             "value": value,
             "obj_id": obj.pk,
             "adaptor": adaptor,
         }
         data.update(extra_data)
         response = client.post(url, data)
         self.assertEqual(response.status_code, 200)
         self.assertEqual(bool(json.loads(response.content.decode("utf-8")).get("errors", None)), has_error)