Beispiel #1
0
def RegisterFile(Service=None, FileName=None, ProvisionedSpace="10G"):

    if Service  is None:
	raise StorageError('RegisterFile(): Service can not be None')

    if FileName is None:
	raise StorageError('RegisterFile(): FileName can not be None')

    vfilespace = StringSizeToBytes(ProvisionedSpace)
    
    if Service.freespace - vfilespace > 0:
	NewFile = File()
	NewFile.vfilename 	= FileName
        NewFile.ufid		= GetUniqueFileID(FileName)
	NewFile.pfilesize	= 0
        NewFile.vfilesize	= vfilespace
	NewFile.service		= Service
	NewFile.pfilename	= GetPhysicalFileName(Service.localpath, FileName)
	NewFile.status		= 'O'
	NewFile.save()
	
	SFreeSpace = CalculateFreeSpace(Service)
	Service.freespace = SFreeSpace
	Service.save()

	return NewFile
    else:
	raise StorageError('RegisterFile(): No have left space')
Beispiel #2
0
def TakeOwnership(Service=None, FileName=None):
    if Service is None:
	raise StorageError('TakeOwnership(): Service can not be None')

    if FileName is None:
	raise StorageError('TakeOwnership(): FileName can not be None')

    if FileExist(Service.localpath,FileName,True):
	NewFile = File()
	NewFile.vfilename	= FileName
	NewFile.pfilename	= FileName
	NewFile.ufid		= GetUniqueFileID(FileName)
	NewFile.service		= Service
	NewFile.status		= 'O'
	NewFile.vfilesize	= 0
	NewFile.pfilesize	= 0
	NewFile.save()
	CloseFile(NewFile.ufid)
	return NewFile
    else:
	raise StorageError('TakeOwnership(): File not exit [%s%s]' % (Service.localpath+FileName))
Beispiel #3
0
def csvUpload(request):
    global messages
    messages = []
    try:
        template = 'fileupload.html'
        form = SimpleFileForm()
     
        if request.method == 'POST':
            if 'file' in request.FILES:


                #
                # Final counters
                #
                numberWeeks = 0
                numberTeamMembers = 0
                numberActuals = 0

                #
                # Preprossesing file 
                #
                csvfile = request.FILES['file']

                fileName = csvfile.name
                fileSize = csvfile.size
                rawData = csvfile.read()
                csvfile.seek(0)
                csvData = csv.reader(csvfile)
                fileDate = datetime.datetime.now()

                #
                # create file record
                #

                try:
                    f = File(name=fileName, uploadDate=fileDate, data=rawData, 
                        status="This message means an internal error...")
                    f.save()
                    messages.append('Loaded file: "%s" [%d] on %s '% (fileName, fileSize, fileDate))
                except:
                    messages.append('Error while saving csv file')
                    raise
                #
                # parse csv data
                #

                teamHead = []
                teamData = {}

                teamMemberHead = []

                for row in csvData:
                    if not teamData:
                        if not teamHead:
                            #
                            # Working on Team and Metric Header
                            #

                            teamHead = row
                            
                            #
                            # Testing team data 
                            #
                            if len(teamHead)<2:
                                messages.append('Team Header row 1 has less than  expected data')

                            if teamHead[teamName] != "Team Name":
                                messages.append('Expected Team Name Header not found')

                            if teamHead[metricName] != "Metric Name":
                                messages.append('Expected Metric Name Header not found')


                        else:
                            #
                            # Working on Team and Metric names
                            #

                            #
                            # testing team data 
                            #
                            if len(row)<2:
                                messages.append('Error, Team Data has less than expected data')
                                raise 
                            #
                            # Get Team and Metric objects
                            #

                            try:
                                team, created = Team.objects.get_or_create(name=row[0])
                                #team.save()
                            except:
                                messages.append('Error with team information')
                                raise
                            try:
                                metric, created = Metric.objects.get_or_create(team=team, name=row[1])
                                #metric.save()
                            except:
                                messages.append('Error with metric information')
                                raise

                            teamData['team'] = team
                            teamData['metric'] = metric
                    else:
                        #
                        # Working on member an metrics data
                        #

                        if not teamMemberHead:
                            #
                            # Testing second Header
                            #

                            teamMemberHead = row

                            if len(teamMemberHead)<dateStart+1:
                                messages.append('Team Member row 3 has less than expected data')

                            if teamMemberHead[oDeskName] != "oDesk ID":
                                messages.append('Expected oDesk ID Header not found')

                            if teamMemberHead[memberName] != "Member Name":
                                messages.append('Expected Member Name Header not found')

                            #
                            # Processing dates from header
                            #

                            try:
                                metricDates = map(datetime_from_str, teamMemberHead[dateStart:])
                                numberWeeks = len(metricDates)
                            except:
                                messages.append('Some dates could not be parsed or are not sundays')
                                raise
                        else:
                            # 
                            # Processing each memeber data
                            #

                            numberTeamMembers += 1
                            try:
                                teamMember, created = TeamMember.objects.get_or_create(team=team, oId = row[oDeskName], 
                                    defaults={'name': row[memberName]})
                            except:
                                messages.append('Erro while getting Team member.')
                                raise

                            #
                            # Processing members metrics
                            #

                            try:
                                for metricDate, metricValue in zip(metricDates, row[dateStart:]):
                                    metdef ={'fileLoaded':f}
                                    if not metricValue:
                                        metdef['value'] = 0
                                        metdef['status'] = False
                                    else:
                                        metdef['value'] = int(metricValue)
                                    MetricData.objects.get_or_create(member=teamMember, metric=metric, metricDate=metricDate,
                                        defaults=metdef)
                                    numberActuals +=1
                            except:
                                messages.append('Erro while inserting Metric.')
                                raise


                #
                # Saving final file status 
                #
                messages.append("team='%s' metric='%s' processed %d actuals. %d members by %d weeks."%
                    (team.name, metric.name, numberActuals, numberTeamMembers, numberWeeks))
                f.status = "\n".join(messages)
                f.save()
            else:

                messages.append('No file attached')
     
            return render_to_response(template, RequestContext(request, {
                'form': form,
                'processMessages': messages,
                }))
        else:
            #
            # Display the form
            #

            return render_to_response(template, RequestContext(request, {
                'form': form,
                'processMessages': messages,
                }))
    except:
        #
        # Here we hold every problem genenrated on parsing
        #
        try:
            if f:
                f.status = "\n".join(messages)
                f.save()
        except:
            #
            #  Let's display them......
            #
            pass
        return render_to_response(template, RequestContext(request, {
            'form': form,
            'processMessages': messages,
            }))