Ejemplo n.º 1
0
def save(request):
    response = {}
    bibs = json.loads(request.POST["bibs"])
    status = 200
    response["id_translations"] = []
    for b_id in list(bibs.keys()):
        bib = bibs[b_id]
        if request.POST["is_new"] == "true":
            inserting_obj = {
                "entry_owner_id": request.user.id,
                "entry_key": bib["entry_key"][-64:],
                "bib_type": bib["bib_type"],
                "cats": bib["cats"],
                "fields": bib["fields"],
            }
            similar = Entry.objects.filter(**inserting_obj).first()
            if similar:
                response["id_translations"].append([b_id, similar.id])
            else:
                the_entry = Entry(**inserting_obj)
                the_entry.save()
                response["id_translations"].append([b_id, the_entry.id])

        else:
            the_entry = Entry.objects.get(id=b_id)
            the_entry.entry_key = bib["entry_key"][-64:]
            the_entry.bib_type = bib["bib_type"]
            the_entry.cats = bib["cats"]
            the_entry.fields = bib["fields"]
            the_entry.save()
            response["id_translations"].append([b_id, the_entry.id])
    return JsonResponse(response, status=status)
Ejemplo n.º 2
0
def save(request):
    response = {}
    bibs = json.loads(request.POST['bibs'])
    status = 200
    response['id_translations'] = []
    for b_id in list(bibs.keys()):
        bib = bibs[b_id]
        if request.POST['is_new'] == 'true':
            inserting_obj = {
                'entry_owner_id': request.user.id,
                'entry_key': bib['entry_key'][-64:],
                'bib_type': bib['bib_type'],
                'entry_cat': bib['entry_cat'],
                'fields': bib['fields']
            }
            similar = Entry.objects.filter(**inserting_obj).first()
            if similar:
                response['id_translations'].append([b_id, similar.id])
            else:
                the_entry = Entry(**inserting_obj)
                the_entry.save()
                response['id_translations'].append([b_id, the_entry.id])

        else:
            the_entry = Entry.objects.get(id=b_id)
            the_entry.entry_key = bib['entry_key'][-64:]
            the_entry.bib_type = bib['bib_type']
            the_entry.entry_cat = bib['entry_cat']
            the_entry.fields = bib['fields']
            the_entry.save()
            response['id_translations'].append([b_id, the_entry.id])
    return JsonResponse(response, status=status)
Ejemplo n.º 3
0
def save_bib_to_db(inserting_obj):
    try:
        the_entry = Entry(**inserting_obj)
        the_entry.save()
        return the_entry
    except IntegrityError:
        similar = Entry.objects.filter(**inserting_obj)
        if (len(similar) == 0):
            inserting_obj['entry_key'] = inserting_obj['entry_key'] + '+'
            return save_bib_to_db(inserting_obj)
        else:
            return False
Ejemplo n.º 4
0
def save_bib_to_db(inserting_obj):
    try:
        the_entry = Entry(**inserting_obj)
        the_entry.save()
        return the_entry
    except IntegrityError:
        similar = Entry.objects.filter(**inserting_obj)
        if (len(similar) == 0):
            inserting_obj['entry_key'] = inserting_obj['entry_key']+ '+'
            return save_bib_to_db(inserting_obj)
        else:
            return False
Ejemplo n.º 5
0
def save_js(request):
    response = {}
    status = 405
    if request.is_ajax() and request.method == 'POST':
        bibs = json.loads(request.POST['bibs'])
        status = 200
        owner_id = request.user.id
        if 'owner_id' in request.POST:
            requested_owner_id = int(request.POST['owner_id'])
            # If the user has write access to at least one document of another
            # user, we allow him to add new and edit bibliography entries of
            # this user.
            if len(
                    AccessRight.objects.filter(
                        document__owner=requested_owner_id,
                        user=request.user.id,
                        rights='w')) > 0:
                owner_id = requested_owner_id
        response['id_translations'] = []
        for b_id in bibs.keys():
            bib = bibs[b_id]
            if request.POST['is_new'] == 'true':
                inserting_obj = {
                    'entry_owner_id': owner_id,
                    'entry_key': bib['entry_key'],
                    'bib_type': bib['bib_type'],
                    'entry_cat': bib['entry_cat'],
                    'fields': bib['fields']
                }
                similar = Entry.objects.filter(**inserting_obj)
                if len(similar) == 0:
                    the_entry = Entry(**inserting_obj)
                    the_entry.save()
                    response['id_translations'].append([b_id, the_entry.id])
                else:
                    response['id_translations'].append([b_id, similar[0].id])
            else:
                the_entry = Entry.objects.get(id=b_id)
                the_entry.entry_key = bib['entry_key']
                the_entry.bib_type = bib['bib_type']
                the_entry.entry_cat = bib['entry_cat']
                the_entry.fields = bib['fields']
                the_entry.save()
                response['id_translations'].append([b_id, the_entry.id])
    return JsonResponse(response, status=status)
Ejemplo n.º 6
0
def save_js(request):
    response = {}
    status = 405
    if request.is_ajax() and request.method == 'POST':
        bibs = json.loads(request.POST['bibs'])
        status = 200
        owner_id = request.user.id
        if 'owner_id' in request.POST:
            requested_owner_id = int(request.POST['owner_id'])
            # If the user has write access to at least one document of the
            # document owning user, we allow him to add new and edit
            # bibliography entries of the document owner.
            if AccessRight.objects.filter(
                document__owner_id=requested_owner_id,
                user_id=request.user.id,
                rights__in=CAN_UPDATE_DOCUMENT
            ).count() > 0:
                owner_id = requested_owner_id
        response['id_translations'] = []
        for b_id in bibs.keys():
            bib = bibs[b_id]
            if request.POST['is_new'] == 'true':
                inserting_obj = {
                    'entry_owner_id': owner_id,
                    'entry_key': bib['entry_key'][-64:],
                    'bib_type': bib['bib_type'],
                    'entry_cat': bib['entry_cat'],
                    'fields': bib['fields']
                }
                similar = Entry.objects.filter(**inserting_obj)
                if len(similar) == 0:
                    the_entry = Entry(**inserting_obj)
                    the_entry.save()
                    response['id_translations'].append([b_id, the_entry.id])
                else:
                    response['id_translations'].append([b_id, similar[0].id])
            else:
                the_entry = Entry.objects.get(id=b_id)
                the_entry.entry_key = bib['entry_key'][-64:]
                the_entry.bib_type = bib['bib_type']
                the_entry.entry_cat = bib['entry_cat']
                the_entry.fields = bib['fields']
                the_entry.save()
                response['id_translations'].append([b_id, the_entry.id])
    return JsonResponse(
        response,
        status=status
    )
Ejemplo n.º 7
0
def save_bib_to_db(inserting_obj, suffix):
    try:
        the_entry = Entry(**inserting_obj)
        the_entry.save()
        return the_entry
    except IntegrityError:
        similar = Entry.objects.filter(**inserting_obj)
        if (len(similar) == 0):
            old_entry_key = inserting_obj['entry_key']
            new_suffix = suffix + 1
            if suffix == 0:
                new_entry_key = old_entry_key + '_1'
            else:
                new_entry_key = old_entry_key[
                    :-(len(str(suffix)) + 1)] + '_' + str(new_suffix)
            inserting_obj['entry_key'] = new_entry_key
            return save_bib_to_db(inserting_obj, new_suffix)
        else:
            return False
Ejemplo n.º 8
0
def save_bib_to_db(inserting_obj, suffix):
    try:
        the_entry = Entry(**inserting_obj)
        the_entry.save()
        return the_entry
    except IntegrityError:
        similar = Entry.objects.filter(**inserting_obj)
        if (len(similar) == 0):
            old_entry_key = inserting_obj['entry_key']
            new_suffix = suffix + 1
            if suffix == 0:
                new_entry_key = old_entry_key + '_1'
            else:
                new_entry_key = old_entry_key[
                    :-(len(str(suffix)) + 1)] + '_' + str(new_suffix)
            inserting_obj['entry_key'] = new_entry_key
            return save_bib_to_db(inserting_obj, new_suffix)
        else:
            # At least one similar entry exists. Return the first match.
            # This is important for BibTranslationTable on doc import
            return similar[0]
Ejemplo n.º 9
0
def save_js(request):
    response = {}
    status = 405
    if request.is_ajax() and request.method == 'POST':
        bibs = json.loads(request.POST['bibs'])
        status = 200
        response['id_translations'] = []
        for b_id in list(bibs.keys()):
            bib = bibs[b_id]
            if request.POST['is_new'] == 'true':
                inserting_obj = {
                    'entry_owner_id': request.user.id,
                    'entry_key': bib['entry_key'][-64:],
                    'bib_type': bib['bib_type'],
                    'entry_cat': bib['entry_cat'],
                    'fields': bib['fields']
                }
                similar = Entry.objects.filter(**inserting_obj).first()
                if similar:
                    response['id_translations'].append([b_id, similar.id])
                else:
                    the_entry = Entry(**inserting_obj)
                    the_entry.save()
                    response['id_translations'].append([b_id, the_entry.id])

            else:
                the_entry = Entry.objects.get(id=b_id)
                the_entry.entry_key = bib['entry_key'][-64:]
                the_entry.bib_type = bib['bib_type']
                the_entry.entry_cat = bib['entry_cat']
                the_entry.fields = bib['fields']
                the_entry.save()
                response['id_translations'].append([b_id, the_entry.id])
    return JsonResponse(
        response,
        status=status
    )
Ejemplo n.º 10
0
                    elif f_type.field_type in ['l_name', 'l_literal', 'l_key']:
                        if isinstance(val, list):
                            val = ' and '.join(val)

                    the_fields[f_type.field_name] = val
                    #setattr(the_entry, f_type.field_name, val)

            if 0 == len(response['errormsg']):
                if 0 < the_id:  #saving changes
                    the_entry = Entry.objects.get(pk=the_id,
                                                  entry_owner=owner_id)
                    the_entry.entry_type = the_type
                else:  #creating a new entry
                    status = 201
                    the_entry = Entry(entry_key='tmp_key',
                                      entry_owner_id=owner_id,
                                      entry_type=the_type)
                    the_entry.save()
                    the_entry.entry_key = 'Fidusbibliography_' + str(
                        the_entry.id)
                #clear categories of the entry to restore them new
                the_entry.entry_cat = the_cat
                the_entry.fields = json.dumps(the_fields)
                the_entry.save()
                response['values'] = serializer.serialize(
                    [the_entry],
                    fields=('entry_key', 'entry_owner', 'entry_type',
                            'entry_cat', 'fields'))
        else:
            #if the entry type doesn't exist
            status = 202
Ejemplo n.º 11
0
                         response['errormsg']['eField' + key] = 'Value must be number'
                         continue
                 elif f_type.field_type in ['l_name', 'l_literal', 'l_key'] :
                     if isinstance(val, list) :
                         val = ' and '.join(val);
                         
                 the_fields[f_type.field_name] = val     
                 #setattr(the_entry, f_type.field_name, val)
                 
         if 0 == len(response['errormsg']) :
             if 0 < the_id : #saving changes
                 the_entry = Entry.objects.get(pk=the_id)
                 the_entry.entry_type = the_type
             else : #creating a new entry
                 status = 201
                 the_entry = Entry(entry_key = 'tmp_key', entry_owner = request.user, entry_type = the_type)
                 the_entry.save()
                 the_entry.entry_key = 'Fidusbibliography_' + str(the_entry.id)
             #clear categories of the entry to restore them new
             the_entry.entry_cat = the_cat
             the_entry.fields = simplejson.dumps(the_fields)
             the_entry.save()
             response['values']  = serializer.serialize([the_entry], fields=('entry_key', 'entry_owner', 'entry_type', 'entry_cat', 'fields'))
     else :
         #if the entry type doesn't exist
         status = 202
         response['errormsg']['error'] = 'this type of entry does not exist.'
         
 return HttpResponse(
     simplejson.dumps(response),
     content_type = 'application/json; charset=utf8',
Ejemplo n.º 12
0
def save_js(request):
    response = {}
    response['errormsg'] = {}
    status = 403
    if request.is_ajax() and request.method == 'POST':
        owner_id = request.user.id
        if 'owner_id' in request.POST:
            requested_owner_id = int(request.POST['owner_id'])
            # If the user has write access to at least one document of another
            # user, we allow him to add new and edit bibliography entries of
            # this user.
            if len(AccessRight.objects.filter(
                    document__owner=requested_owner_id,
                    user=request.user.id, rights='w')) > 0:
                owner_id = requested_owner_id
        status = 200
        the_id = int(request.POST['id'])
        the_type = EntryType.objects.filter(pk=int(request.POST['entrytype']))
        # the entry type must exists
        if the_type.exists():
            the_type = the_type[0]
            the_fields = {}
            the_cat = ''
            # save the posted values
            for key, val in request.POST.iteritems():
                if 'id' == key or 'entrytype' == key:
                    # do nothing, if it is the ID or EntryType
                    continue
                elif 'entryCat[]' == key:
                    # categories are given as Array
                    # store them with loop
                    val = request.POST.getlist(key)
                    the_cat = ','.join(val)
                else:
                    # store other values into EntryValues
                    if 0 < key.find('[]'):
                        val = request.POST.getlist(key)
                    key = key[6:]
                    # key should be formed like "eField" + name of the value
                    # type
                    key = key.replace('[]', '')
                    f_type = EntryField.objects.filter(field_name=key)
                    if f_type.exists():
                        f_type = f_type[0]
                    else:
                        continue

                    if '' == val:
                        pass
                    elif 'null' == val:
                        # empty value not allowed
                        response['errormsg'][
                            'eField' + key] = 'Value must not be empty'
                        continue
                    elif f_type.field_type == 'f_date':
                        # reform date field
                        dates = val.split('-')
                        new_value = []
                        i = 0
                        for each_date in dates:
                            date_parts = each_date.split('/')
                            new_value.append('')
                            if date_parts[0].isdigit():
                                new_value[i] += date_parts[0]
                            else:
                                new_value[i] += 'AA'
                            if (2 <= len(date_parts) and
                                    date_parts[1].isdigit()):
                                new_value[i] += '-' + date_parts[1]
                            else:
                                new_value[i] += '-AA'
                            if (3 <= len(date_parts) and
                                    date_parts[2].isdigit()):
                                new_value[i] += '-' + date_parts[2]
                            else:
                                new_value[i] += '-AA'
                            i += 1
                        if 1 == len(new_value):
                            val = new_value[0]
                        else:
                            val = new_value[0] + '/' + new_value[1]
                    elif f_type.field_type == 'f_integer':
                        # must be int
                        try:
                            val = int(val, 10)
                        except ValueError:
                            response['errormsg'][
                                'eField' + key] = 'Value must be number'
                            continue
                    elif f_type.field_type in ['l_name', 'l_literal', 'l_key']:
                        if isinstance(val, list):
                            val = ' and '.join(val)

                    the_fields[f_type.field_name] = val
                    # setattr(the_entry, f_type.field_name, val)

            if 0 == len(response['errormsg']):
                if 0 < the_id:  # saving changes
                    the_entry = Entry.objects.get(
                        pk=the_id,
                        entry_owner=owner_id
                    )
                    the_entry.entry_type = the_type
                else:  # creating a new entry
                    status = 201
                    the_entry = Entry(
                        entry_key='tmp_key',
                        entry_owner_id=owner_id,
                        entry_type=the_type
                    )
                    the_entry.save()
                    the_entry.entry_key = 'Fidusbibliography_' + str(
                        the_entry.id
                    )
                # clear categories of the entry to restore them new
                the_entry.entry_cat = the_cat
                the_entry.fields = json.dumps(the_fields)
                the_entry.save()
                response['values'] = serializer.serialize(
                    [the_entry],
                    fields=(
                        'entry_key',
                        'entry_owner',
                        'entry_type',
                        'entry_cat',
                        'fields'
                    )
                )
        else:
            # if the entry type doesn't exist
            status = 202
            errormsg = 'this type of entry does not exist.'
            response['errormsg']['error'] = errormsg

    return JsonResponse(
        response,
        status=status
    )
Ejemplo n.º 13
0
                            response["errormsg"]["eField" + key] = "Value must be number"
                            continue
                    elif f_type.field_type in ["l_name", "l_literal", "l_key"]:
                        if isinstance(val, list):
                            val = " and ".join(val)

                    the_fields[f_type.field_name] = val
                    # setattr(the_entry, f_type.field_name, val)

            if 0 == len(response["errormsg"]):
                if 0 < the_id:  # saving changes
                    the_entry = Entry.objects.get(pk=the_id)
                    the_entry.entry_type = the_type
                else:  # creating a new entry
                    status = 201
                    the_entry = Entry(entry_key="tmp_key", entry_owner=request.user, entry_type=the_type)
                    the_entry.save()
                    the_entry.entry_key = "Fidusbibliography_" + str(the_entry.id)
                # clear categories of the entry to restore them new
                the_entry.entry_cat = the_cat
                the_entry.fields = json.dumps(the_fields)
                the_entry.save()
                response["values"] = serializer.serialize(
                    [the_entry], fields=("entry_key", "entry_owner", "entry_type", "entry_cat", "fields")
                )
        else:
            # if the entry type doesn't exist
            status = 202
            response["errormsg"]["error"] = "this type of entry does not exist."

    return HttpResponse(json.dumps(response), content_type="application/json; charset=utf8", status=status)
Ejemplo n.º 14
0
def save_js(request):
    response = {}
    response['errormsg'] = {}
    status = 403
    if request.is_ajax() and request.method == 'POST':
        owner_id = request.user.id
        if 'owner_id' in request.POST:
            requested_owner_id = int(request.POST['owner_id'])
            # If the user has write access to at least one document of another
            # user, we allow him to add new and edit bibliography entries of
            # this user.
            if len(AccessRight.objects.filter(
                    document__owner=requested_owner_id,
                    user=request.user.id, rights='w')) > 0:
                owner_id = requested_owner_id
        status = 200
        the_id = int(request.POST['id'])
        the_type = EntryType.objects.filter(pk=int(request.POST['entrytype']))
        # the entry type must exists
        if the_type.exists():
            the_type = the_type[0]
            the_fields = {}
            the_cat = ''
            # save the posted values
            for key, val in request.POST.iteritems():
                if 'id' == key or 'entrytype' == key:
                    # do nothing, if it is the ID or EntryType
                    continue
                elif 'entryCat[]' == key:
                    # categories are given as Array
                    # store them with loop
                    val = request.POST.getlist(key)
                    the_cat = ','.join(val)
                else:
                    # store other values into EntryValues
                    if 0 < key.find('[]'):
                        val = request.POST.getlist(key)
                    key = key[6:]
                    # key should be formed like "eField" + name of the value
                    # type
                    key = key.replace('[]', '')
                    f_type = EntryField.objects.filter(field_name=key)
                    if f_type.exists():
                        f_type = f_type[0]
                    else:
                        continue

                    if '' == val:
                        pass
                    elif 'null' == val:
                        # empty value not allowed
                        response['errormsg'][
                            'eField' + key] = 'Value must not be empty'
                        continue
                    elif f_type.field_type == 'f_date':
                        # reform date field
                        dates = val.split('-')
                        new_value = []
                        i = 0
                        for each_date in dates:
                            date_parts = each_date.split('/')
                            new_value.append('')
                            if date_parts[0].isdigit():
                                new_value[i] += date_parts[0]
                            else:
                                new_value[i] += 'AA'
                            if (2 <= len(date_parts) and
                                    date_parts[1].isdigit()):
                                new_value[i] += '-' + date_parts[1]
                            else:
                                new_value[i] += '-AA'
                            if (3 <= len(date_parts) and
                                    date_parts[2].isdigit()):
                                new_value[i] += '-' + date_parts[2]
                            else:
                                new_value[i] += '-AA'
                            i += 1
                        if 1 == len(new_value):
                            val = new_value[0]
                        else:
                            val = new_value[0] + '/' + new_value[1]
                    elif f_type.field_type == 'f_integer':
                        # must be int
                        try:
                            val = int(val, 10)
                        except ValueError:
                            response['errormsg'][
                                'eField' + key] = 'Value must be number'
                            continue
                    elif f_type.field_type in ['l_name', 'l_literal', 'l_key']:
                        if isinstance(val, list):
                            val = ' and '.join(val)

                    the_fields[f_type.field_name] = val
                    # setattr(the_entry, f_type.field_name, val)

            if 0 == len(response['errormsg']):
                if 0 < the_id:  # saving changes
                    the_entry = Entry.objects.get(
                        pk=the_id,
                        entry_owner=owner_id
                    )
                    the_entry.entry_type = the_type
                else:  # creating a new entry
                    status = 201
                    the_entry = Entry(
                        entry_key='tmp_key',
                        entry_owner_id=owner_id,
                        entry_type=the_type
                    )
                    the_entry.save()
                    the_entry.entry_key = 'Fidusbibliography_' + str(
                        the_entry.id
                    )
                # clear categories of the entry to restore them new
                the_entry.entry_cat = the_cat
                the_entry.fields = json.dumps(the_fields)
                the_entry.save()
                response['values'] = serializer.serialize(
                    [the_entry],
                    fields=(
                        'entry_key',
                        'entry_owner',
                        'entry_type',
                        'entry_cat',
                        'fields'
                    )
                )
        else:
            # if the entry type doesn't exist
            status = 202
            errormsg = 'this type of entry does not exist.'
            response['errormsg']['error'] = errormsg

    return JsonResponse(
        response,
        status=status
    )