Changeset 1790 for pykota/trunk

Show
Ignore:
Timestamp:
10/07/04 23:14:28 (20 years ago)
Author:
jalet
Message:

Hopefully final fix for data encoding to and from the database

Location:
pykota/trunk
Files:
5 modified

Legend:

Unmodified
Added
Removed
  • pykota/trunk/bin/dumpykota

    r1789 r1790  
    2424# 
    2525# $Log$ 
     26# Revision 1.12  2004/10/07 21:14:28  jalet 
     27# Hopefully final fix for data encoding to and from the database 
     28# 
    2629# Revision 1.11  2004/10/07 14:35:40  jalet 
    2730# Now edpykota refuses to launch if the user is not a PyKota administrator. 
     
    234237            x.entry() 
    235238            for i in range(len(entry)) : 
    236                 value = entry[i] 
    237                 strvalue = unicode(str(value), self.getCharset()).encode("UTF-8") 
    238                 x.attribute(strvalue, type=type(value).__name__, name=headers[i]) 
     239                value = str(entry[i]) 
     240                try : 
     241                    value = unicode(value, self.getCharset()).encode("UTF-8") 
     242                except UnicodeError :     
     243                    pass 
     244                x.attribute(value, type=type(value).__name__, name=headers[i]) 
    239245            x._pop()     
    240246        x._output(self.outfile) 
  • pykota/trunk/NEWS

    r1789 r1790  
    2424    - 1.20alpha22 : 
    2525     
     26        - Hopefully final fix for data encoding charset. 
     27         
    2628        - edpykota now refuse to work if the user is not a PyKota admin. 
    2729         
  • pykota/trunk/pykota/storage.py

    r1755 r1790  
    2222# 
    2323# $Log$ 
     24# Revision 1.63  2004/10/07 21:14:28  jalet 
     25# Hopefully final fix for data encoding to and from the database 
     26# 
    2427# Revision 1.62  2004/09/28 17:45:31  jalet 
    2528# Added the --hardreset command line option to edpykota 
     
    685688        return gpquotas         
    686689         
     690    def databaseToUserCharset(self, text) : 
     691        """Converts from database format (UTF-8) to user's charset.""" 
     692        if text is not None : 
     693            try : 
     694                return unicode(text, "UTF-8").encode(self.tool.getCharset())  
     695            except UnicodeError :     
     696                pass 
     697        return text 
     698         
     699    def userCharsetToDatabase(self, text) : 
     700        """Converts from user's charset to database format (UTF-8).""" 
     701        if text is not None : 
     702            try : 
     703                return unicode(text, self.tool.getCharset()).encode("UTF-8")  
     704            except UnicodeError :     
     705                pass 
     706        return text 
     707         
    687708def openConnection(pykotatool) : 
    688709    """Returns a connection handle to the appropriate Quota Storage Database.""" 
  • pykota/trunk/pykota/storages/ldapstorage.py

    r1777 r1790  
    2222# 
    2323# $Log$ 
     24# Revision 1.83  2004/10/07 21:14:28  jalet 
     25# Hopefully final fix for data encoding to and from the database 
     26# 
    2427# Revision 1.82  2004/10/05 09:59:20  jalet 
    2528# Restore compatibility with Python 2.1 
     
    592595            printer.PricePerPage = float(fields.get("pykotaPricePerPage", [0.0])[0] or 0.0) 
    593596            printer.uniqueMember = fields.get("uniqueMember", []) 
    594             printer.Description = unicode(fields.get("description", [""])[0], "UTF-8").encode(self.tool.getCharset())  
     597            printer.Description = self.databaseToUserCharset(fields.get("description", [""])[0])  
    595598            printer.Exists = 1 
    596599        return printer     
     
    693696                    lastjob.JobPrice = None 
    694697                lastjob.JobAction = fields.get("pykotaAction", [""])[0] 
    695                 lastjob.JobFileName = unicode(fields.get("pykotaFileName", [""])[0], "UTF-8").encode(self.tool.getCharset())  
    696                 lastjob.JobTitle = unicode(fields.get("pykotaTitle", [""])[0], "UTF-8").encode(self.tool.getCharset())  
     698                lastjob.JobFileName = self.databaseToUserCharset(fields.get("pykotaFileName", [""])[0])  
     699                lastjob.JobTitle = self.databaseToUserCharset(fields.get("pykotaTitle", [""])[0])  
    697700                lastjob.JobCopies = int(fields.get("pykotaCopies", [0])[0]) 
    698                 lastjob.JobOptions = unicode(fields.get("pykotaOptions", [""])[0], "UTF-8").encode(self.tool.getCharset())  
     701                lastjob.JobOptions = self.databaseToUserCharset(fields.get("pykotaOptions", [""])[0])  
    699702                lastjob.JobHostName = fields.get("pykotaHostName", [""])[0] 
    700703                lastjob.JobSizeBytes = fields.get("pykotaJobSizeBytes", [0L])[0] 
     
    769772                printer.PricePerPage = float(fields.get("pykotaPricePerPage", [0.0])[0] or 0.0) 
    770773                printer.uniqueMember = fields.get("uniqueMember", []) 
    771                 printer.Description = unicode(fields.get("description", [""])[0], "UTF-8").encode(self.tool.getCharset())  
     774                printer.Description = self.databaseToUserCharset(fields.get("description", [""])[0])  
    772775                printer.Exists = 1 
    773776                printers.append(printer) 
     
    981984        """Write the printer's description back into the storage.""" 
    982985        fields = { 
    983                    "description" : unicode(str(printer.Description), self.tool.getCharset()).encode("UTF-8"),  
     986                   "description" : self.userCharsetToDatabase(str(printer.Description)),  
    984987                 } 
    985988        self.doModify(printer.ident, fields) 
     
    10801083                   "pykotaPrinterPageCounter" : str(pagecounter), 
    10811084                   "pykotaAction" : action, 
    1082                    "pykotaFileName" : ((filename is None) and "None") or unicode(filename, self.tool.getCharset()).encode("UTF-8"),  
    1083                    "pykotaTitle" : ((title is None) and "None") or unicode(title, self.tool.getCharset()).encode("UTF-8"),  
     1085                   "pykotaFileName" : ((filename is None) and "None") or self.userCharsetToDatabase(filename),  
     1086                   "pykotaTitle" : ((title is None) and "None") or self.userCharsetToDatabase(title),  
    10841087                   "pykotaCopies" : str(copies),  
    1085                    "pykotaOptions" : ((options is None) and "None") or unicode(options, self.tool.getCharset()).encode("UTF-8"),  
     1088                   "pykotaOptions" : ((options is None) and "None") or self.userCharsetToDatabase(options),  
    10861089                   "pykotaHostName" : str(clienthost),  
    10871090                   "pykotaJobSizeBytes" : str(jobsizebytes), 
     
    11821185                    job.JobPrice = None 
    11831186                job.JobAction = fields.get("pykotaAction", [""])[0] 
    1184                 job.JobFileName = unicode(fields.get("pykotaFileName", [""])[0], "UTF-8").encode(self.tool.getCharset())  
    1185                 job.JobTitle = unicode(fields.get("pykotaTitle", [""])[0], "UTF-8").encode(self.tool.getCharset())  
     1187                job.JobFileName = self.databaseToUserCharset(fields.get("pykotaFileName", [""])[0])  
     1188                job.JobTitle = self.databaseToUserCharset(fields.get("pykotaTitle", [""])[0])  
    11861189                job.JobCopies = int(fields.get("pykotaCopies", [0])[0]) 
    1187                 job.JobOptions = unicode(fields.get("pykotaOptions", [""])[0], "UTF-8").encode(self.tool.getCharset())  
     1190                job.JobOptions = self.databaseToUserCharset(fields.get("pykotaOptions", [""])[0])  
    11881191                job.JobHostName = fields.get("pykotaHostName", [""])[0] 
    11891192                job.JobSizeBytes = fields.get("pykotaJobSizeBytes", [0L])[0] 
  • pykota/trunk/pykota/storages/sql.py

    r1787 r1790  
    2222# 
    2323# $Log$ 
     24# Revision 1.56  2004/10/07 21:14:28  jalet 
     25# Hopefully final fix for data encoding to and from the database 
     26# 
    2427# Revision 1.55  2004/10/07 09:37:53  jalet 
    2528# Fixes recently introduced bug wrt users groups (was it three days ago ?) 
     
    114117                    field = fields[j] 
    115118                    if type(field) == StringType : 
    116                         try : 
    117                             fields[j] = unicode(field, "UTF-8").encode(self.tool.getCharset())  
    118                         except UnicodeError : # takes care of old jobs in history not stored as UTF-8     
    119                             pass 
     119                        fields[j] = self.databaseToUserCharset(field)  
    120120                entries[i] = tuple(fields)     
    121121            return entries 
     
    224224            printer.PricePerJob = fields.get("priceperjob") or 0.0 
    225225            printer.PricePerPage = fields.get("priceperpage") or 0.0 
    226             printer.Description = unicode((fields.get("description") or ""), "UTF-8").encode(self.tool.getCharset())  
     226            printer.Description = self.databaseToUserCharset(fields.get("description") or "") 
    227227            printer.Exists = 1 
    228228        return printer     
     
    276276            lastjob.JobPrice = fields.get("jobprice") 
    277277            lastjob.JobAction = fields.get("action") 
    278             lastjob.JobFileName = unicode((fields.get("filename") or ""), "UTF-8").encode(self.tool.getCharset())  
    279             lastjob.JobTitle = unicode((fields.get("title") or ""), "UTF-8").encode(self.tool.getCharset())  
     278            lastjob.JobFileName = self.databaseToUserCharset(fields.get("filename") or "")  
     279            lastjob.JobTitle = self.databaseToUserCharset(fields.get("title") or "")  
    280280            lastjob.JobCopies = fields.get("copies") 
    281             lastjob.JobOptions = unicode((fields.get("options") or ""), "UTF-8").encode(self.tool.getCharset())  
     281            lastjob.JobOptions = self.databaseToUserCharset(fields.get("options") or "")  
    282282            lastjob.JobDate = fields.get("jobdate") 
    283283            lastjob.JobHostName = fields.get("hostname") 
     
    338338                    printer.PricePerJob = record.get("priceperjob") or 0.0 
    339339                    printer.PricePerPage = record.get("priceperpage") or 0.0 
    340                     printer.Description = unicode((record.get("description") or ""), "UTF-8").encode(self.tool.getCharset())  
     340                    printer.Description = self.databaseToUserCharset(record.get("description") or "")  
    341341                    printer.Exists = 1 
    342342                    printers.append(printer) 
     
    424424    def writePrinterDescription(self, printer) :     
    425425        """Write the printer's description back into the storage.""" 
    426         description = printer.Description 
    427         if description is not None : 
    428             description = unicode(printer.Description, self.tool.getCharset()).encode("UTF-8"),  
     426        description = self.userCharsetToDatabase(printer.Description) 
    429427        self.doModify("UPDATE printers SET description=%s WHERE id=%s" % (self.doQuote(description), self.doQuote(printer.ident))) 
    430428         
     
    474472    def writeJobNew(self, printer, user, jobid, pagecounter, action, jobsize=None, jobprice=None, filename=None, title=None, copies=None, options=None, clienthost=None, jobsizebytes=None) : 
    475473        """Adds a job in a printer's history.""" 
    476         if filename is not None : 
    477             filename = unicode(filename, self.tool.getCharset()).encode("UTF-8") 
    478         if title is not None : 
    479             title = unicode(title, self.tool.getCharset()).encode("UTF-8") 
    480         if options is not None : 
    481             options = unicode(options, self.tool.getCharset()).encode("UTF-8") 
     474        filename = self.userCharsetToDatabase(filename) 
     475        title = self.userCharsetToDatabase(title) 
     476        options = self.userCharsetToDatabase(options) 
    482477        if (not self.disablehistory) or (not printer.LastJob.Exists) : 
    483478            if jobsize is not None : 
     
    539534                job.JobPrice = fields.get("jobprice") 
    540535                job.JobAction = fields.get("action") 
    541                 job.JobFileName = unicode((fields.get("filename") or ""), "UTF-8").encode(self.tool.getCharset())  
    542                 job.JobTitle = unicode((fields.get("title") or ""), "UTF-8").encode(self.tool.getCharset())  
     536                job.JobFileName = self.databaseToUserCharset(fields.get("filename") or "")  
     537                job.JobTitle = self.databaseToUserCharset(fields.get("title") or "")  
    543538                job.JobCopies = fields.get("copies") 
    544                 job.JobOptions = unicode((fields.get("options") or ""), "UTF-8").encode(self.tool.getCharset())  
     539                job.JobOptions = self.databaseToUserCharset(fields.get("options") or "")  
    545540                job.JobDate = fields.get("jobdate") 
    546541                job.JobHostName = fields.get("hostname")