Beispiel #1
0
def workdays(start, end):
    """Calculates the number of working days (Mo-Fr) between two given dates.

    Whereas the workdays are calculated siilar to Python slice notation: [start : end[
    Example:
    >>> workdays(datetime.date(2007, 1, 26), datetime.date(2007,  1,  27)) # Fr - Sa
    1
    >>> workdays(datetime.date(2007, 1, 28), datetime.date(2007,  1,  29)) # Su - Mo
    0
    """
    start = convert_to_date(start)
    end = convert_to_date(end)
    if start > end:
        return -1 * _workdays(end, start)
    else:
        return _workdays(start, end)
Beispiel #2
0
def workdays(start, end):
    """Calculates the number of working days (Mo-Fr) between two given dates.

    Whereas the workdays are calculated siilar to Python slice notation: [start : end[
    Example:
    >>> workdays(datetime.date(2007, 1, 26), datetime.date(2007,  1,  27)) # Fr - Sa
    1
    >>> workdays(datetime.date(2007, 1, 28), datetime.date(2007,  1,  29)) # Su - Mo
    0
    """
    start = convert_to_date(start)
    end = convert_to_date(end)
    if start > end:
        return -1 * _workdays(end, start)
    else:
        return _workdays(start, end)
Beispiel #3
0
def create_instance(klass, data):
    """Erzeuge eine Instanz eines Models aus den übergebenen Daten"""

    # Falls das Model eine Methode 'create' besitzt, rufe diese auf.
    if hasattr(klass, 'create'):
        return klass.create(data)

    # Ansonsten wird ein generischer Ansatz verfolgt:
    tmp = {}
    props = klass.properties()

    for attr in data:
        if attr not in props:
            continue

        value = data[attr]
        prop = props[attr]

        if isinstance(prop, (db.StringProperty, db.TextProperty)):
            pass
        elif isinstance(prop, db.LinkProperty):
            pass
        elif isinstance(prop, db.DateProperty):
            value = convert_to_date(value)
        elif isinstance(prop, db.DateTimeProperty):
            value = convert_to_datetime(value)
        elif isinstance(prop, db.BooleanProperty):
            pass
        else:
            raise ValueError(u'Unknown property: %s', prop)

        tmp[attr.encode('ascii', 'replace')] = value

    return klass(**tmp)
Beispiel #4
0
def date2softm(date):
    """Wandelt ein datetime Objekt oder String in das von SoftM verwendete Format.

    >>> date2softm(datetime.date(2000, 01, 01))
    '1000101'
    >>> date2softm(datetime.date(1901, 06, 05))
    '010605'
    >>> date2softm('2001-12-13')
    '1011213'
    """

    date = convert_to_date(date)
    if not date:
        return ''
    if date.year > 1999:
        return date.strftime('1%y%m%d')
    return date.strftime('%y%m%d')
Beispiel #5
0
    def get(self):
        u"""Regelmäßig von Cron aufzurufen."""
        bucketpath = '/'.join(
            (replication_config.GS_BUCKET, get_application_id())) + '/'
        logging.info(u'searching backups in %r', bucketpath)

        objs = cloudstorage.listbucket(bucketpath, delimiter='/')
        subdirs = sorted((obj.filename for obj in objs if obj.is_dir),
                         reverse=True)
        # Find Path of newest available backup
        # typical path:
        # '/appengine-backups-eu-nearline/hudoraexpress/2017-05-02/ag9...EM.ArtikelBild.backup_info'
        dirs = dict()
        for subdir in subdirs:
            datepart = subdir.rstrip('/').split('/')[-1]
            try:
                datum = convert_to_date(subdir.rstrip('/').split('/')[-1])
            except ValueError:
                continue
            else:
                dirs[datum] = subdir

        if not dirs:
            raise HTTP500_ServerError(u'No Datastore Backup found in %r' %
                                      bucketpath)

        datum = max(dirs)
        if datum < datetime.date.today() - datetime.timedelta(days=14):
            raise HTTP500_ServerError(
                u'Latest Datastore Backup in %r is way too old!' % bucketpath)

        countdown = 1
        subdir = dirs[datum]
        logging.info(u'Uploading Backup %s from directory %s', datum, subdir)
        regexp = re.compile(subdir + r'([\w-]+)\.(\w+)\.backup_info')
        for obj in cloudstorage.listbucket(subdir):
            if regexp.match(obj.filename):
                taskqueue.add(
                    url=self.request.path,
                    params={'filename': obj.filename},
                    queue_name=replication_config.BIGQUERY_QUEUE_NAME,
                    countdown=countdown)
                countdown += 2
        self.response.write('ok, countdown=%d\n' % countdown)
Beispiel #6
0
    def post(self, tenant):
        user = self.login_required()
        tenant = user.tenant
        if not hasattr(self.request.POST['pdfdata'], 'file'):
            logging.debug(dir(self.request))
            logging.debug(vars(self.request))
            HTTP400_BadRequest('pdfdata not a file')
        pdfdata = self.request.POST['pdfdata'].file.read()
        if len(pdfdata) > 900000:
            raise HTTP413_TooLarge('PDF bigger than 900k')
        ref = self.request.POST['ref']
        typ = self.request.POST['type']
        refs = ref.split()
        if refs:
            akte_designator = refs[0]
        else:
            handmade_key = db.Key.from_path('Akte', 1)
            akte_designator = "ablage%s" % (db.allocate_ids(handmade_key,
                                                            1)[0])
        pdf_id = str(
            base64.b32encode(hashlib.sha1(pdfdata).digest()).rstrip('='))
        key_name = "%s-%s" % (tenant, pdf_id)

        # do we already have that document - ignore designator given by uploader
        doc = Dokument.get_by_key_name(key_name)
        if doc and doc.akte.designator != akte_designator:
            ref = ' '.join(list(set([akte_designator] + ref.split())))
            akte_designator = doc.akte.designator
        akte_key = "%s-%s" % (tenant, akte_designator)

        akteargs = dict(type=typ,
                        designator=akte_designator,
                        datum=convert_to_date(self.request.POST.get('datum')))
        for key in ('name1', 'name2', 'name3', 'strasse', 'land', 'plz', 'ort',
                    'email', 'ref_url', 'datum'):
            if self.request.POST.get(key):
                akteargs[key] = self.request.POST.get(key)
            if self.request.POST.get('akte_%s' % key):
                akteargs[key] = self.request.POST.get('akte_%s' % key)
            if key == 'datum' and 'datum' in akteargs:
                akteargs[key] = convert_to_date(akteargs[key])
        akte = Akte.get_or_insert(akte_key, tenant=tenant, **akteargs)
        oldref = akte.ref
        newref = list(set(oldref + self.request.POST.get('ref', '').split()))
        newseit = oldseit = akte.seit
        if self.request.POST.get('datum'):
            postseit = convert_to_date(self.request.POST.get('datum'))
            if postseit and postseit and (postseit < postseit):
                newseit = postseit
        if (newref != oldref) or (newseit != oldseit):
            akte.seit = newseit
            akte.ref = newref
            akte.put()

        docargs = dict(type=typ,
                       datum=datetime.date.today(),
                       file_length=len(pdfdata))
        for key in ('name1', 'name2', 'name3', 'strasse', 'land', 'plz', 'ort',
                    'email', 'ref_url', 'datum'
                    'quelle'):
            if self.request.POST.get(key):
                docargs[key] = self.request.POST.get(key)
        if 'datum' in docargs:
            docargs['datum'] = convert_to_date(docargs['datum'])
        dokument = Dokument.get_or_insert(key_name,
                                          designator=pdf_id,
                                          akte=akte,
                                          tenant=tenant,
                                          **docargs)
        # resave if it existed but something had changed
        for key in docargs.keys():
            if getattr(dokument, key) != docargs[key]:
                logging.debug('%s: key %s has changed', pdf_id, key)
                dokument.put()
                break
        DokumentFile.get_or_insert(
            key_name,
            dokument=dokument,
            akte=akte,
            data=pdfdata,
            tenant=tenant,
            mimetype='application/pdf',
            filename=self.request.POST['pdfdata'].filename)
        oldref = dokument.ref
        newref = list(set(oldref + self.request.POST.get('ref', '').split()))
        if newref != oldref:
            dokument.ref = newref
            dokument.put()
        self.redirect(dokument.get_url())
        self.response.set_status(201)
        self.response.headers["Content-Type"] = 'text/plain'
        self.response.out.write('ok:%s\n' % dokument.designator)
def filter_dateformat(value, formatstring='%Y-%m-%d'):
    """Formates a date"""

    from huTools.calendar.formats import convert_to_date
    return convert_to_date(value).strftime(formatstring)
def filter_to_date(_context, value):
    """Wandelt ein Date oder Datetime-Objekt in einen Dat-Objekt"""
    from huTools.calendar.formats import convert_to_date
    return convert_to_date(value)
def filter_dateformat(value, formatstring='%Y-%m-%d'):
    """Formates a date."""
    from huTools.calendar.formats import convert_to_date
    if not value:
        return ''
    return Markup(convert_to_date(value).strftime(formatstring).replace('-', '&#8209;'))
def filter_to_date(value):
    """Wandelt ein Date oder Datetime-Objekt in einen Date-Objekt."""
    from huTools.calendar.formats import convert_to_date
    if not value:
        return ''
    return convert_to_date(value)
Beispiel #11
0
    def post(self, tenant):
        user = self.login_required()
        tenant = user.tenant
        if not hasattr(self.request.POST['pdfdata'], 'file'):
            logging.debug(dir(self.request))
            logging.debug(vars(self.request))
            HTTP400_BadRequest('pdfdata not a file')
        pdfdata = self.request.POST['pdfdata'].file.read()
        if len(pdfdata) > 900000:
            raise HTTP413_TooLarge('PDF bigger than 900k')
        ref = self.request.POST['ref']
        typ = self.request.POST['type']
        refs = ref.split()
        if refs:
            akte_designator = refs[0]
        else:
            handmade_key = db.Key.from_path('Akte', 1)
            akte_designator = "ablage%s" % (db.allocate_ids(handmade_key, 1)[0])
        pdf_id = str(base64.b32encode(hashlib.sha1(pdfdata).digest()).rstrip('='))
        key_name = "%s-%s" % (tenant, pdf_id)
        
        # do we already have that document - ignore designator given by uploader
        doc = Dokument.get_by_key_name(key_name)
        if doc and doc.akte.designator != akte_designator:
            ref = ' '.join(list(set([akte_designator] + ref.split())))
            akte_designator = doc.akte.designator
        akte_key = "%s-%s" % (tenant, akte_designator)

        akteargs = dict(type=typ, designator=akte_designator,
                        datum=convert_to_date(self.request.POST.get('datum')))
        for key in ('name1', 'name2', 'name3', 'strasse', 'land', 'plz', 'ort', 'email', 'ref_url',
                    'datum'):
            if self.request.POST.get(key):
                akteargs[key] = self.request.POST.get(key)
            if self.request.POST.get('akte_%s' % key):
                akteargs[key] = self.request.POST.get('akte_%s' % key)
            if key == 'datum' and 'datum' in akteargs:
                akteargs[key] = convert_to_date(akteargs[key])
        akte = Akte.get_or_insert(akte_key, tenant=tenant, **akteargs)
        oldref = akte.ref
        newref = list(set(oldref + self.request.POST.get('ref', '').split()))
        newseit = oldseit = akte.seit
        if self.request.POST.get('datum'):
            postseit = convert_to_date(self.request.POST.get('datum'))
            if postseit and postseit and (postseit < postseit):
                newseit = postseit
        if (newref != oldref) or (newseit != oldseit):
            akte.seit = newseit
            akte.ref = newref
            akte.put()

        docargs = dict(type=typ, datum=datetime.date.today(), file_length=len(pdfdata))
        for key in ('name1', 'name2', 'name3', 'strasse', 'land', 'plz', 'ort', 'email', 'ref_url',
                    'datum' 'quelle'):
            if self.request.POST.get(key):
                docargs[key] = self.request.POST.get(key)
        if 'datum' in docargs:
            docargs['datum'] = convert_to_date(docargs['datum'])
        dokument = Dokument.get_or_insert(key_name, designator=pdf_id, akte=akte, tenant=tenant, **docargs)
        # resave if it existed but something had changed
        for key in docargs.keys():
            if getattr(dokument, key) != docargs[key]:
                logging.debug('%s: key %s has changed', pdf_id, key)
                dokument.put()
                break
        DokumentFile.get_or_insert(key_name, dokument=dokument, akte=akte, data=pdfdata,
                                   tenant=tenant, mimetype='application/pdf',
                                   filename=self.request.POST['pdfdata'].filename)
        oldref = dokument.ref
        newref = list(set(oldref + self.request.POST.get('ref', '').split()))
        if newref != oldref:
            dokument.ref = newref
            dokument.put()
        self.redirect(dokument.get_url())
        self.response.set_status(201)
        self.response.headers["Content-Type"] = 'text/plain'
        self.response.out.write('ok:%s\n' % dokument.designator)