Exemplo n.º 1
0
def import_bibtex_js(request):
    response = {}
    status = 405
    if request.is_ajax() and request.method == 'POST':
        from bibliography.bib import Persons
        from bibliography.bib import BibDate
        response['errors'] = []
        response['warning'] = []
        bibs = json.loads(request.POST['bibs'])
        status = 200
        e_types = {}
        for e_type in EntryType.objects.all():
            e_types[e_type.type_name] = e_type
        e_types_alias = {}
        for e_type in EntryTypeAlias.objects.all():
            e_types_alias[e_type.type_name] = e_type
        e_fields = {}
        for e_field in EntryField.objects.all():
            e_fields[e_field.field_name] = e_field
        e_fields_alias = {}
        for e_field in EntryFieldAlias.objects.all():
            e_fields_alias[e_field.field_name] = e_field
        new_bibs = []
        response['bib_ids'] = []
        for bib_key in bibs:
            bib = bibs[bib_key]
            bib_type_name = bib['bibtype']
            #the entry type must exists
            try:
                the_type = e_types[bib_type_name]
            except KeyError:
                try:
                    type_alias = e_types_alias[bib_type_name]
                    the_type = type_alias.type_alias
                except KeyError:
                    the_type = e_types['misc']
                    response['warning'].append(
                        bib_key +
                        ' is saved as misc. Fidus Writer does not support "' +
                        bib_type_name + '"')
            inserting_obj = {
                'entry_key': bib_key,
                'entry_owner': request.user,
                'entry_type': the_type
            }
            the_fields = {}
            #save the posted values
            for key, val in bib.iteritems():
                if key in ['bibtype', 'year', 'month']:
                    #do not save the value of type, year and month
                    continue
                else:
                    try:
                        field_type = e_fields[key]
                    except KeyError:
                        try:
                            field_alias = e_fields_alias[key]
                            field_type = field_alias.field_alias
                        except KeyError:
                            response['errors'].append(
                                key + ' of ' + bib_key +
                                ' could not be saved. Fidus Writer does not support the field.'
                            )
                            continue
                    if 'l_name' == field_type.field_type:
                        #restore name list value like "author"
                        persons = Persons(val)
                        val = persons.get_names()
                    elif 'f_date' == field_type.field_type:
                        #restore date value like "date"
                        bib_date = BibDate(val)
                        val = bib_date.date
                    if isinstance(val, basestring):
                        val = val.strip("{}")
                    if isinstance(val, list):
                        val = ' and '.join(val)
                    the_fields[field_type.field_name] = val
            inserting_obj['fields'] = json.dumps(the_fields)
            the_entry = save_bib_to_db(inserting_obj)
            if the_entry != False:
                new_bibs.append(the_entry)
                response['bib_ids'].append(the_entry.id)
            response['bibs'] = serializer.serialize(
                new_bibs,
                fields=('entry_key', 'entry_owner', 'entry_type', 'entry_cat',
                        'fields'))
    return HttpResponse(json.dumps(response),
                        content_type='application/json; charset=utf8',
                        status=status)
Exemplo n.º 2
0
def import_bibtex_js(request):
    response = {}
    status = 405
    if request.is_ajax() and request.method == 'POST':
        response['errors'] = []
        response['warning'] = []
        bibs = json.loads(request.POST['bibs'])
        status = 200
        e_types = {}
        for e_type in EntryType.objects.all():
            e_types[e_type.type_name] = e_type
        e_types_alias = {}
        for e_type in EntryTypeAlias.objects.all():
            e_types_alias[e_type.type_name] = e_type
        e_fields = {}
        for e_field in EntryField.objects.all():
            e_fields[e_field.field_name] = e_field
        e_fields_alias = {}
        for e_field in EntryFieldAlias.objects.all():
            e_fields_alias[e_field.field_name] = e_field
        new_bibs = []
        response['key_translations'] = {}
        for bib_key in bibs:
            bib = bibs[bib_key]
            bib_type_name = bib['bibtype']
            # the entry type must exists
            if bib_type_name in e_types:
                the_type = e_types[bib_type_name]
            elif bib_type_name in e_types_alias:
                type_alias = e_types_alias[bib_type_name]
                the_type = type_alias.type_alias
            else:
                the_type = e_types['misc']
                response['warning'].append(
                    bib_key +
                    ' is saved as misc. Fidus Writer does not support "' +
                    bib_type_name +
                    '"')

            inserting_obj = {
                'entry_key': bib_key,
                'entry_owner': request.user,
                'entry_type': the_type
            }
            the_fields = {}
            # save the posted values
            for key, val in bib.iteritems():
                if key in ['bibtype', 'year', 'month']:
                    # do not save the value of type, year and month
                    continue
                elif key in e_fields:
                    field_type = e_fields[key]
                elif key in e_fields_alias:
                    field_alias = e_fields_alias[key]
                    field_type = field_alias.field_alias
                else:
                    response['errors'].append(
                        key +
                        ' of ' +
                        bib_key +
                        (' could not be saved. Fidus Writer does not support '
                         'the field.')
                    )
                    continue

                if 'l_name' == field_type.field_type:
                    # restore name list value like "author"
                    persons = Persons(val)
                    val = persons.get_names()
                elif 'f_date' == field_type.field_type:
                    # restore date value like "date"
                    bib_date = BibDate(val)
                    val = bib_date.date
                if isinstance(val, list):
                    val = ' and '.join(val)
                the_fields[field_type.field_name] = val
            inserting_obj['fields'] = json.dumps(the_fields)
            old_key = inserting_obj['entry_key']
            the_entry = save_bib_to_db(inserting_obj, 0)
            if the_entry:
                new_bibs.append(the_entry)
                response['key_translations'][old_key] = the_entry.entry_key
            response['bibs'] = serializer.serialize(
                new_bibs,
                fields=(
                    'entry_key',
                    'entry_owner',
                    'entry_type',
                    'entry_cat',
                    'fields'))
    return JsonResponse(
        response,
        status=status
    )
Exemplo n.º 3
0
def import_bibtex_js(request):
    response = {}
    status = 405
    if request.is_ajax() and request.method == 'POST' :
        from bibliography.bib import Persons
        from bibliography.bib import BibDate
        response['errors'] = []
        response['warning'] = []
        bibs = simplejson.loads(request.POST['bibs'])
        status = 200
        e_types = {}
        for e_type in EntryType.objects.all():
            e_types[e_type.type_name] = e_type
        e_types_alias = {}
        for e_type in EntryTypeAlias.objects.all():
            e_types_alias[e_type.type_name] = e_type
        e_fields = {}
        for e_field in EntryField.objects.all():
            e_fields[e_field.field_name] = e_field
        e_fields_alias = {}
        for e_field in EntryFieldAlias.objects.all():
            e_fields_alias[e_field.field_name] = e_field
        new_bibs = []
        response['bib_ids']=[]
        for bib_key in bibs :
            bib = bibs[bib_key]
            bib_type_name = bib['bibtype']
            #the entry type must exists
            try:
                the_type = e_types[bib_type_name]
            except KeyError:
                try:
                    type_alias = e_types_alias[bib_type_name]
                    the_type = type_alias.type_alias
                except KeyError:
                    the_type  = e_types['misc']
                    response['warning'].append(bib_key + ' is saved as misc. Fidus Writer does not support "' + bib_type_name + '"')
            inserting_obj = {
                'entry_key': bib_key,
                'entry_owner': request.user,
                'entry_type': the_type
            }
            the_fields = {}
            #save the posted values
            for key, val in bib.iteritems() :
                if key in ['bibtype', 'year', 'month'] :
                    #do not save the value of type, year and month
                    continue
                else :
                    try:
                        field_type = e_fields[key]
                    except KeyError:
                        try:
                            field_alias = e_fields_alias[key]
                            field_type = field_alias.field_alias
                        except KeyError:
                            response['errors'].append(key + ' of ' + bib_key + ' could not be saved. Fidus Writer does not support the field.')
                            continue
                    if 'l_name' == field_type.field_type :
                        #restore name list value like "author"
                        persons = Persons(val)
                        val = persons.get_names()
                    elif 'f_date' == field_type.field_type :
                        #restore date value like "date"
                        bib_date = BibDate(val)
                        val = bib_date.date
                    if isinstance(val, basestring) :
                        val = val.strip("{}")
                    if isinstance(val, list) :
                        val = ' and '.join(val)
                    the_fields[field_type.field_name] = val
            inserting_obj['fields'] = simplejson.dumps(the_fields)        
            the_entry = save_bib_to_db(inserting_obj)
            if the_entry != False:
                new_bibs.append(the_entry)
                response['bib_ids'].append(the_entry.id)
            response['bibs'] = serializer.serialize(new_bibs, fields=('entry_key', 'entry_owner', 'entry_type', 'entry_cat', 'fields'))
    return HttpResponse(
        simplejson.dumps(response),
        content_type = 'application/json; charset=utf8',
        status=status
    )
Exemplo n.º 4
0
def import_bibtex_js(request):
    response = {}
    status = 405
    if request.is_ajax() and request.method == "POST":
        from bibliography.bib import Persons
        from bibliography.bib import BibDate

        response["errors"] = []
        response["warning"] = []
        bibs = json.loads(request.POST["bibs"])
        status = 200
        e_types = {}
        for e_type in EntryType.objects.all():
            e_types[e_type.type_name] = e_type
        e_types_alias = {}
        for e_type in EntryTypeAlias.objects.all():
            e_types_alias[e_type.type_name] = e_type
        e_fields = {}
        for e_field in EntryField.objects.all():
            e_fields[e_field.field_name] = e_field
        e_fields_alias = {}
        for e_field in EntryFieldAlias.objects.all():
            e_fields_alias[e_field.field_name] = e_field
        new_bibs = []
        response["bib_ids"] = []
        for bib_key in bibs:
            bib = bibs[bib_key]
            bib_type_name = bib["bibtype"]
            # the entry type must exists
            try:
                the_type = e_types[bib_type_name]
            except KeyError:
                try:
                    type_alias = e_types_alias[bib_type_name]
                    the_type = type_alias.type_alias
                except KeyError:
                    the_type = e_types["misc"]
                    response["warning"].append(
                        bib_key + ' is saved as misc. Fidus Writer does not support "' + bib_type_name + '"'
                    )
            inserting_obj = {"entry_key": bib_key, "entry_owner": request.user, "entry_type": the_type}
            the_fields = {}
            # save the posted values
            for key, val in bib.iteritems():
                if key in ["bibtype", "year", "month"]:
                    # do not save the value of type, year and month
                    continue
                else:
                    try:
                        field_type = e_fields[key]
                    except KeyError:
                        try:
                            field_alias = e_fields_alias[key]
                            field_type = field_alias.field_alias
                        except KeyError:
                            response["errors"].append(
                                key + " of " + bib_key + " could not be saved. Fidus Writer does not support the field."
                            )
                            continue
                    if "l_name" == field_type.field_type:
                        # restore name list value like "author"
                        persons = Persons(val)
                        val = persons.get_names()
                    elif "f_date" == field_type.field_type:
                        # restore date value like "date"
                        bib_date = BibDate(val)
                        val = bib_date.date
                    if isinstance(val, basestring):
                        val = val.strip("{}")
                    if isinstance(val, list):
                        val = " and ".join(val)
                    the_fields[field_type.field_name] = val
            inserting_obj["fields"] = json.dumps(the_fields)
            the_entry = save_bib_to_db(inserting_obj)
            if the_entry != False:
                new_bibs.append(the_entry)
                response["bib_ids"].append(the_entry.id)
            response["bibs"] = serializer.serialize(
                new_bibs, fields=("entry_key", "entry_owner", "entry_type", "entry_cat", "fields")
            )
    return HttpResponse(json.dumps(response), content_type="application/json; charset=utf8", status=status)