示例#1
0
def record_load(request,bot_name):
    """Record load view displays the `MARCRecordUploadForm` for a
    particular MARC record load.

    :param bot_name: Name of bot, required"""
    bot_names = [bot.__name__ for bot in active_bots]
    is_active = bot_names.count(bot_name)
    if is_active < 1:
        raise Http404
    marc_form = RecordLoadLogForm()
    note_form = NotesForm()
    for bot in active_bots:
        if bot.__name__ == bot_name and hasattr(bot,'DATABASES'):
            bot_choices = []
            for k,v in getattr(bot,'DATABASES').iteritems():
                bot_choices.append((v['code'],k))
            marc_form.fields['databases']=forms.ChoiceField(label='Select database',
                                                            required=False,
                                                            choices=bot_choices)
           
    return direct_to_template(request,
                              'marc/index.html',
                              {'active_bots':[bot.__name__ for bot in active_bots],
                               'live_bot':bot_name,
                               'download':None,
                               'history':RecordLoadLog.objects.all(),
                               'marc_form':marc_form,
                               'note_form':note_form})
示例#2
0
def process(request):
    """Takes form submission and runs bots on uploaded 
    form."""
    if request.method != 'POST':
        raise Http404
    else:
        record_log_form = RecordLoadLogForm(request.POST,request.FILES)
    bot_name,bot = request.POST['bot'],None
    for active_bot in active_bots:
        if active_bot.__name__ == bot_name:
            if request.POST.has_key('databases'):
                bot = active_bot(marc_file=request.FILES['original_file'],
                                 type_of=request.POST['databases'])
            else:
                bot = active_bot(marc_file=request.FILES['original_file'])
    if not bot:
        raise Http404
    bot.load()
    if not record_log_form.is_valid():
        logging.error("Errors = %s" % record_log_form.errors)
    record_log = record_log_form.save()
    record_log.process_id = bot_name
    record_log.save()
    
    mod_filename = '%s-%s.mrc' % (datetime.datetime.today().strftime("%Y-%m-%d"),
                                  record_log.process_id.replace('Bot',''))
    #record_log.modified_file_content = File(mod_filename)
    #record_log.modified_file_content.write(bot.to_text())
    record_log.modified_file.save(mod_filename,ContentFile(bot.to_text()))
    request.session['log_pk'] = record_log.pk
    note_form = NotesForm(request.POST)
    note_form.record_load_log_id = record_log.pk
    if note_form.is_valid():
        note_form.save()
    else:
        logging.error("Note form is not valid %s" % note_form.errors)
    return HttpResponseRedirect("/marc/update")