Changeset 403 in main


Ignore:
Timestamp:
10/18/11 13:10:03 (8 years ago)
Author:
pcosquer
Message:

new feature: import part and document from a csv

Location:
trunk/openPLM
Files:
7 added
8 edited

Legend:

Unmodified
Added
Removed
  • trunk/openPLM/media/css/openplm.css

    r393 r403  
    210210{ 
    211211    display: none; 
     212    font-size: 14px; 
    212213    /*float: left;*/ 
    213214    width: 100%; 
  • trunk/openPLM/plmapp/forms.py

    r398 r403  
    2727from django import forms 
    2828from django.conf import settings 
    29 from django.forms.formsets import formset_factory 
     29from django.forms.formsets import formset_factory, BaseFormSet 
    3030from django.forms.models import modelform_factory, modelformset_factory 
    3131from django.contrib.auth.models import User, Group 
     
    3838from openPLM.plmapp.controllers.user import UserController 
    3939from openPLM.plmapp.widgets import JQueryAutoComplete 
     40from openPLM.plmapp.encoding import ENCODINGS 
     41import openPLM.plmapp.csvimport as csvimport 
    4042 
    4143class PLMObjectForm(forms.Form): 
     
    120122 
    121123class TypeForm(forms.Form): 
    122     LISTE = m.get_all_users_and_plmobjects_with_level() 
    123     type = forms.TypedChoiceField(choices=LISTE) 
     124    LIST = m.get_all_users_and_plmobjects_with_level() 
     125    type = forms.TypedChoiceField(choices=LIST) 
    124126 
    125127class TypeFormWithoutUser(forms.Form): 
    126     LISTE_WO_USER = m.get_all_plmobjects_with_level() 
    127     type = forms.TypedChoiceField(choices=LISTE_WO_USER) 
     128    LIST_WO_USER = m.get_all_plmobjects_with_level() 
     129    type = forms.TypedChoiceField(choices=LIST_WO_USER) 
    128130 
    129131class TypeSearchForm(TypeForm): 
     
    458460            required=True, widget=forms.HiddenInput()) 
    459461 
    460  
     462class CSVForm(forms.Form): 
     463    file = forms.FileField() 
     464    encoding = forms.TypedChoiceField(initial="utf_8", choices=ENCODINGS) 
     465 
     466 
     467class CSVHeaderForm(forms.Form): 
     468    header = forms.TypedChoiceField(choices=zip(csvimport.HEADERS, 
     469        csvimport.HEADERS), required=False) 
     470 
     471 
     472class BaseHeadersFormset(BaseFormSet): 
     473 
     474    def clean(self): 
     475        if any(self.errors): 
     476            return 
     477        headers = [] 
     478        for form in self.forms: 
     479            header = form.cleaned_data['header'] 
     480            if header and header in headers: 
     481                raise forms.ValidationError(_("Columns must have distict headers.")) 
     482            headers.append(header)  
     483        for field in csvimport.REQUIRED_FIELDS: 
     484            if field not in headers: 
     485                raise forms.ValidationError(csvimport.MISSING_HEARDERS_MSG) 
     486        self.headers = headers 
     487 
     488 
     489HeadersFormset = formset_factory(CSVHeaderForm, extra=0, 
     490        formset=BaseHeadersFormset) 
     491 
  • trunk/openPLM/plmapp/tests/__init__.py

    r376 r403  
    2727from openPLM.plmapp.tests.lifecycle import * 
    2828from openPLM.plmapp.tests.views import * 
     29from openPLM.plmapp.tests.csvimport import * 
    2930#from openPLM.plmapp.tests.closure import * 
    3031 
  • trunk/openPLM/plmapp/views/main.py

    r392 r403  
    4646""" 
    4747 
     48import os 
     49import csv 
    4850import datetime 
     51import tempfile 
     52import itertools 
    4953from operator import attrgetter 
    5054from mimetypes import guess_type 
     
    6771from openPLM.plmapp.base_views import get_obj, get_obj_from_form, \ 
    6872    get_obj_by_id, handle_errors, get_generic_data, get_navigate_data 
    69  
     73import openPLM.plmapp.csvimport as csvimport 
    7074 
    7175def r2r(template, dictionary, request): 
     
    10491053            return HttpResponseRedirect("..") 
    10501054    else: 
    1051         form = SponsorForm(initial={"sponsor":obj.object}, sponsor=obj.id) 
     1055        form = SponsorForm(initial={"sponsor":obj.id}, sponsor=obj.id) 
    10521056    ctx["sponsor_form"] = form 
    10531057    ctx['current_page'] = 'delegation'  
     
    11001104    return r2r("groups/refuse_invitation.htm", ctx, request) 
    11011105 
    1102             
    1103  
     1106@handle_errors 
     1107def import_csv_init(request): 
     1108    obj, ctx = get_generic_data(request) 
     1109    if request.method == "POST": 
     1110        csv_form = CSVForm(request.POST, request.FILES) 
     1111        if csv_form.is_valid(): 
     1112            f = request.FILES["file"] 
     1113            prefix = "openplmcsv" 
     1114            tmp = tempfile.NamedTemporaryFile(prefix=prefix, delete=False) 
     1115            for chunk in f.chunks(): 
     1116                tmp.write(chunk) 
     1117            name = os.path.split(tmp.name)[1][len(prefix):] 
     1118            tmp.close() 
     1119            encoding = csv_form.cleaned_data["encoding"] 
     1120            return HttpResponseRedirect("/import/csv/%s/%s/" % (name, encoding)) 
     1121    else: 
     1122        csv_form = CSVForm() 
     1123    ctx["csv_form"] = csv_form 
     1124    ctx["step"] = 1 
     1125    return r2r("import/csv.htm", ctx, request) 
     1126 
     1127@handle_errors 
     1128def import_csv_apply(request, filename, encoding): 
     1129    obj, ctx = get_generic_data(request) 
     1130    ctx["encoding_error"] = False 
     1131    ctx["io_error"] = False 
     1132    try: 
     1133        path = os.path.join(tempfile.gettempdir(), "openplmcsv" +  filename) 
     1134        with open(path, "rb") as csv_file: 
     1135            preview = csvimport.CSVPreview(csv_file, encoding) 
     1136        if request.method == "POST": 
     1137            headers_formset = forms.HeadersFormset(request.POST) 
     1138            if headers_formset.is_valid(): 
     1139                headers = headers_formset.headers 
     1140                try: 
     1141                    with open(path, "rb") as csv_file: 
     1142                        csvimport.import_csv(csv_file, headers, request.user, 
     1143                                             encoding) 
     1144                except csvimport.CSVImportError as exc: 
     1145                    ctx["errors"] = exc.errors.iteritems() 
     1146                else: 
     1147                    os.remove(path) 
     1148                    return HttpResponseRedirect("/import/done/") 
     1149        else: 
     1150            initial = [{"header": header} for header in preview.guessed_headers] 
     1151            headers_formset = forms.HeadersFormset(initial=initial) 
     1152        ctx.update({ 
     1153            "preview" :  preview, 
     1154            "preview_data" : itertools.izip((f["header"] for f in headers_formset.forms), 
     1155                preview.headers, *preview.rows), 
     1156            "headers_formset" : headers_formset, 
     1157        }) 
     1158    except UnicodeError: 
     1159        ctx["encoding_error"] = True 
     1160    except (IOError, csv.Error): 
     1161        ctx["io_error"] = True 
     1162    ctx["has_critical_error"] = ctx["io_error"] or ctx["encoding_error"] \ 
     1163            or "errors" in ctx 
     1164    ctx["csv_form"] = CSVForm(initial={"encoding" : encoding}) 
     1165    ctx["step"] = 2 
     1166    return r2r("import/csv.htm", ctx, request) 
     1167 
     1168 
     1169@handle_errors 
     1170def import_csv_done(request): 
     1171    obj, ctx = get_generic_data(request) 
     1172    return r2r("import/done.htm", ctx, request) 
     1173 
  • trunk/openPLM/templates/DisplayFileAdd.htm

    r349 r403  
    77    {% trans "Add new file / Check-in file :" %} 
    88    {% with add_file_form as form %} 
    9         {% with  "enctype='multipart/form-data'" as form_attributes %} 
    10             {% include "snippets/undo_form.htm" %} 
    11         {% endwith %} 
     9        {% include "snippets/undo_form.htm" %} 
    1210    {% endwith %} 
    1311</form> 
  • trunk/openPLM/templates/DisplayHomePage.htm

    r369 r403  
    120120            <input type="submit" value="{% trans "CREATE !" %}"/> 
    121121            </li> 
     122            <li class="ui-button ui-button-text  ui-button-text-only ui-widget ui-state-default ui-corner-all"  id="ImportButton"> 
     123                <a href="/import/csv/"> 
     124                    {% trans "Import" %} 
     125                </a> 
     126             </li> 
    122127        </ul> 
    123128    </form> 
  • trunk/openPLM/templates/snippets/undo_form.htm

    r349 r403  
    11{% load i18n %} 
    2  
    3 <form method="POST" action="." {{ form_attributes }}> 
     2{% if form.is_multipart %} 
     3<form enctype="multipart/form-data" method="post" action="{{action|default:"."}}" {{ form_attributes }}> 
     4{% else %} 
     5    <form method="post" action="{{action|default:"."}}"  {{ form_attributes }}> 
     6{% endif %} 
    47    <table class="Content"> 
    58        {{ form }} 
  • trunk/openPLM/urls.py

    r380 r403  
    7777    (r'^object/create/$', create_object), 
    7878    (r'^comments/', include('django.contrib.comments.urls')), 
     79    ('^import/csv/$', import_csv_init), 
     80    ('^import/csv/(?P<filename>[\w]+)/(?P<encoding>[\w]+)/$', import_csv_apply), 
     81    ('^import/done/$', import_csv_done), 
    7982    ) 
    8083 
Note: See TracChangeset for help on using the changeset viewer.