Example #1
0
    def addParameters(self, **kwargs):
        """
        Add special parameters for birthday widget *messages.birthday.\**
        :param kwargs: list of parameters for update
        """
        if 'message' in kwargs:
            content = kwargs['message'].get('content')
            template = kwargs['message'].get('template')
            n = int(kwargs['message'].get('number'))
            orientation = kwargs['message'].get('orientation')
        else:
            content = Settings.get('messages.birthday.content')
            template = ""  # todo define templates
            n = int(Settings.get('messages.birthday.number', 20))
            orientation = Settings.get('messages.birthday.orientation', 20)

        persons = sorted(Person.getPersons(onlyactive=True), key=lambda p: p.birthday)
        p = [(val.birthday - int(datetime.datetime.now().strftime('%j')), idx, persons[idx].lastname, persons[idx].birthdate) for (idx, val) in enumerate(persons)]
        idx = min(filter(lambda x: x[0] >= 0, p))

        person = OrderedDict()
        try:
            for i in range(idx[1] - (n / 2) + 1, idx[1] + (n / 2) + 2):
                _p = persons[i % (len(p))]
                if _p.birthdate.strftime('%d.%m.') not in person.keys():
                    person[_p.birthdate.strftime('%d.%m.')] = []
                person[_p.birthdate.strftime('%d.%m.')].append(_p)
            for _p in persons:
                if _p.birthdate.strftime('%d.%m.') in person.keys() and _p not in person[_p.birthdate.strftime('%d.%m.')]:
                    person[_p.birthdate.strftime('%d.%m.')].append(_p)
        except:
            pass

        kwargs.update({'content': content, 'template': template, 'persons': person, 'daynum': int((datetime.datetime.now()).strftime('%j')), 'orientation': orientation})
        self.params = kwargs
def getAdminData(self):
    if request.args.get('action') == '':
        pass

    elif request.args.get('action') == 'upload':
        if request.files:
            uploadfile = request.files['uploadfile']
            filename = werkzeug.secure_filename(uploadfile.filename)
            fname = os.path.join(current_app.config.get('PATH_TMP'), filename)
            uploadfile.save(fname)
            xlsfile = XLSFile(fname)
            uploadfiles[uploadfile.filename] = xlsfile
            return render_template('admin.persons.upload2.html', sheets=uploadfiles[uploadfile.filename].getSheets())

    elif request.args.get('action') == 'upload_sheet':  # sheet selector
        definitionfile = uploadfiles[request.args.get('filename')]
        return render_template('admin.persons.upload3.html', cols=definitionfile.getCols(request.args.get('sheetname')))

    elif request.args.get('action') == 'testimport':  # build data for preview
        col_definition = dict(zip(request.form.keys(), request.form.values()))
        col_definition.update({'sheet': request.args.get('sheetname'), 'department': request.args.get('department')})

        deffile = uploadfiles[request.args.get('filename')]
        vals = deffile.getValues(col_definition)
        return render_template('admin.persons.uploadpreview.html', vals=vals)

    elif request.args.get('action') == 'doimport':  # do import and store values
        col_definition = dict(zip(request.form.keys(), request.form.values()))
        col_definition.update({'sheet': request.args.get('sheetname'), 'department': request.args.get('department')})

        deffile = uploadfiles[request.args.get('filename')]

        vals = deffile.getValues(col_definition)
        states = []

        if request.form.get('add_new'):  # add new keys ( item state=-1)
            states.append('-1')

        if request.form.get('add_update'):  # update existing keys (item state=1)
            states.append('1')

        for key in vals['persons']:  # item list
            if key['state'] in states:  # import only with correct state
                if key['state'] == '-1':  # add key
                    key['birthdate'] = datetime.strptime(key['birthdate'], '%d.%m.%Y')
                    key['active'] = key['active'] == 'Aktiv'
                    p = Person(key['firstname'], key['lastname'], key['salutation'], key['grade'], key['position'], key['identifier'], key['active'], key['birthdate'], key['remark'], int(request.args.get('department')))
                    db.session.add(p)
                    db.session.commit()
                    key['state'] = '0'

                elif key['state'] == '1':  # update key
                    p = Person.getPersons(id=key['dbid'])
                    if p:
                        p.firstname = key['firstname']
                        p.lastname = key['lastname']
                        p.grade = key['grade']
                        p.position = key['position']
                        db.session.commit()
        return ""
Example #3
0
    def addParameters(self, **kwargs):
        """
        Add special parameters for birthday widget *messages.birthday.\**
        :param kwargs: list of parameters for update
        """
        if 'message' in kwargs:
            content = kwargs['message'].get('content')
            template = kwargs['message'].get('template')
            n = int(kwargs['message'].get('number'))
            orientation = kwargs['message'].get('orientation')
        else:
            content = Settings.get('messages.birthday.content')
            template = ""  # todo define templates
            n = int(Settings.get('messages.birthday.number', 20))
            orientation = Settings.get('messages.birthday.orientation', 20)

        persons = sorted(Person.getPersons(onlyactive=True), key=lambda p: p.birthday)
        val, idx = min((val.birthday - int(datetime.datetime.now().strftime('%j')), idx) for (idx, val) in enumerate(persons) if val.birthday - int(datetime.datetime.now().strftime('%j')) >= 0)
        person = OrderedDict()

        # calculate correct slice of birthdays
        #for j in range(idx - (n / 2), idx + (n / 2)):
        #    p = persons[j % (len(persons))]
        #    if p.birthdate.strftime('%d.%m.') not in person.keys():
        #        person[p.birthdate.strftime('%d.%m.')] = []
        #    person[p.birthdate.strftime('%d.%m.')].append(p)

        x = 0
        while len(person.keys()) < n / 2:  # lower
            p = persons[(idx - x) % (len(persons))]
            if p.birthdate.strftime('%d.%m.') not in person.keys():
                person[p.birthdate.strftime('%d.%m.')] = []
            person[p.birthdate.strftime('%d.%m.')].append(p)
            x += 1
        x = 1
        person = OrderedDict(sorted(person.items(), key=lambda t: t[0]))  # order dates

        while len(person.keys()) < n:  # upper
            p = persons[(idx + x) % (len(persons))]
            if p.birthdate.strftime('%d.%m.') not in person.keys():
                person[p.birthdate.strftime('%d.%m.')] = []
            person[p.birthdate.strftime('%d.%m.')].append(p)
            x += 1

        kwargs.update({'content': content, 'template': template, 'persons': person, 'daynum': int((datetime.datetime.now()).strftime('%j'))})
        self.params = kwargs
Example #4
0
    def getValues(self, coldefinition):
        states = {'-1': 0, '0': 0, '1': 0}
        worksheet = self.book.sheet_by_name(self.book.sheet_names()[int(
            coldefinition['sheet'])])

        def getPosForCol(name):
            ret = -65
            while len(name) > 0:
                ret += ord(name[0])
                name = name[1:]
            return ret

        def evalValue(crow, ccol):
            try:
                return unicode(worksheet.cell_value(crow, ccol))
            except:
                return ""

        importpersons = []
        # state -1:new, 0:no changes, 1:need update
        for row in range(self.headerrow + 1, worksheet.nrows - 1):
            item = {
                'dbid': None,
                'state': '0',
                'department': coldefinition['department'],
            }
            for key in coldefinition:
                item[key] = evalValue(row, getPosForCol(coldefinition[key]))

            # check if key exists
            p = Person.getPersons(identifier=item['identifier'])
            if p:
                item['dbid'] = p.id
                if "%s%s%s%s" % (p.firstname, p.lastname, p.grade,
                                 p.position) != "%s%s%s%s" % (
                                     item['firstname'], item['lastname'],
                                     item['grade'], item['position']):
                    item['state'] = '1'  # updated
            else:
                item['state'] = '-1'  # mark as new

            states[str(item['state'])] += 1
            importpersons.append(item)

        return dict(persons=importpersons, states=states)
Example #5
0
    def addParameters(self, **kwargs):
        """
        Add special parameters for birthday widget *messages.birthday.\**
        :param kwargs: list of parameters for update
        """
        if 'message' in kwargs:
            content = kwargs['message'].get('content')
            template = kwargs['message'].get('template')
            n = int(kwargs['message'].get('number'))
            orientation = kwargs['message'].get('orientation')
        else:
            content = Settings.get('messages.birthday.content')
            template = ""  # todo define templates
            n = int(Settings.get('messages.birthday.number', 20))
            orientation = Settings.get('messages.birthday.orientation', 20)

        persons = sorted(Person.getPersons(onlyactive=True),
                         key=lambda p: p.birthday)
        p = [((val.birthday - int(datetime.datetime.now().strftime('%j'))) %
              365, idx, persons[idx].lastname, persons[idx].birthdate)
             for (idx, val) in enumerate(persons)]
        idx = min(filter(lambda x: x[0] >= 0, p))

        person = OrderedDict()
        try:
            for i in range(idx[1] - (n / 2) + 1, idx[1] + (n / 2) + 2):
                _p = persons[i % (len(p))]
                if _p.birthdate.strftime('%d.%m.') not in person.keys():
                    person[_p.birthdate.strftime('%d.%m.')] = []
                person[_p.birthdate.strftime('%d.%m.')].append(_p)
            for _p in persons:
                if _p.birthdate.strftime('%d.%m.') in person.keys(
                ) and _p not in person[_p.birthdate.strftime('%d.%m.')]:
                    person[_p.birthdate.strftime('%d.%m.')].append(_p)
        except:
            pass

        kwargs.update({
            'content': content,
            'template': template,
            'persons': person,
            'daynum': int((datetime.datetime.now()).strftime('%j')),
            'orientation': orientation
        })
        self.params = kwargs
    def getValues(self, coldefinition):
        states = {'-1': 0, '0': 0, '1': 0}
        worksheet = self.book.sheet_by_name(self.book.sheet_names()[int(coldefinition['sheet'])])

        def getPosForCol(name):
            ret = -65
            while len(name) > 0:
                ret += ord(name[0])
                name = name[1:]
            return ret

        def evalValue(crow, ccol):
            try:
                return unicode(worksheet.cell_value(crow, ccol))
            except:
                return ""

        importpersons = []
        # state -1:new, 0:no changes, 1:need update
        for row in range(self.headerrow + 1, worksheet.nrows - 1):
            item = {'dbid': None, 'state': '0', 'department': coldefinition['department'], }
            for key in coldefinition:
                item[key] = evalValue(row, getPosForCol(coldefinition[key]))

            # check if key exists
            p = Person.getPersons(identifier=item['identifier'])
            if p:
                item['dbid'] = p.id
                if "%s%s%s%s" % (p.firstname, p.lastname, p.grade, p.position) != "%s%s%s%s" % (item['firstname'], item['lastname'], item['grade'], item['position']):
                    item['state'] = '1'  # updated
            else:
                item['state'] = '-1'  # mark as new

            states[str(item['state'])] += 1
            importpersons.append(item)

        return dict(persons=importpersons, states=states)
def getAdminContent(self, **params):
    module = request.view_args['module'].split('/')

    if len(module) < 2:
        module.append(u'1')

    if len(module) > 2 and module[2] == 'upload':  # upload persons
        params.update({'': int(module[1]), 'depid': module[1]})
        return render_template('admin.persons.upload.html', **params)

    if request.method == 'POST':
        if request.form.get('action') == 'createperson':  # add person
            params.update({'person': Person('', '', '', '', '', '', False, datetime.now(), '', int(module[1])), 'settings': Settings.get('persons.settings')})
            return render_template('admin.persons_edit.html', **params)

        elif request.form.get('action').startswith('editperson_'):  # edit person
            params.update({'person': Person.getPersons(id=request.form.get('action').split('_')[-1]), 'settings': Settings.get('persons.settings')})
            return render_template('admin.persons_edit.html', **params)

        elif request.form.get('action') == 'updateperson':  # save person
            if request.form.get('person_id') != 'None':
                person = Person.getPersons(id=request.form.get('person_id'))
            else:
                person = Person('', '', '', '', '', '', False, datetime.now(), '', int(module[1]))
                db.session.add(person)

            person.firstname = request.form.get('firstname')
            person.lastname = request.form.get('lastname')
            person.salutation = request.form.get('salutation')
            person.grade = request.form.get('grade')
            person._dept = int(request.form.get('dept'))
            person.position = request.form.get('position')
            person.active = 'active' in request.form.keys()
            try:
                person.birthdate = datetime.strptime('{} 00:00:00'.format(request.form.get('birthdate')), "%d.%m.%Y %H:%M:%S")
            except ValueError:
                pass
            person.identifier = request.form.get('identifier')
            person.remark = request.form.get('remark')
            db.session.commit()

        elif request.form.get('action').startswith('deleteperson_'):
            db.session.delete(Person.getPersons(id=request.form.get('action').split('_')[-1]))
            db.session.commit()

        elif request.form.get('action') == 'updategrades':
            grades = request.form.getlist('grade')
            while grades[-1] == grades[-2] == u"":  # remove last empty entries
                grades = grades[:-2]
            _settings = Settings.get('persons.settings')
            if 'positions' not in _settings.keys():
                _settings['positions'] = []
            _settings['grades'] = zip(*[grades[i::2] for i in range(2)])
            Settings.set('persons.settings', _settings)
            db.session.commit()

        elif request.form.get('action') == 'updatepositions':
            _settings = Settings.get('persons.settings')
            if 'grades' not in _settings.keys():
                _settings['grades'] = []
            _settings['positions'] = request.form.get('positions').replace('\r', '').split('\n')
            Settings.set('persons.settings', _settings)
            db.session.commit()

    if int(module[1]) == 0:
        params.update({'settings': Settings.get('persons.settings')})
        return render_template("admin.persons.settings.html", **params)
    persons = Person.getPersons(dept=int(module[1]))
    chars = {}
    for p in persons:
        chars[p.fullname[0].upper()] = 0
    params.update({'persons': persons, 'chars': sorted(chars), 'depid': module[1]})
    return render_template("admin.persons.html", **params)
Example #8
0
def getAdminContent(self, **params):
    module = request.view_args['module'].split('/')

    if len(module) < 2:
        module.append(u'1')

    if len(module) > 2 and module[2] == 'upload':  # upload persons
        params.update({'': int(module[1]), 'depid': module[1]})
        return render_template('admin.persons.upload.html', **params)

    if request.method == 'POST':
        if request.form.get('action') == 'createperson':  # add person
            params.update({
                'person':
                Person('', '', '', '', '', '', False, datetime.now(), '',
                       int(module[1])),
                'settings':
                Settings.get('persons.settings')
            })
            return render_template('admin.persons_edit.html', **params)

        elif request.form.get('action').startswith(
                'editperson_'):  # edit person
            params.update({
                'person':
                Person.getPersons(
                    id=request.form.get('action').split('_')[-1]),
                'settings':
                Settings.get('persons.settings')
            })
            return render_template('admin.persons_edit.html', **params)

        elif request.form.get('action') == 'updateperson':  # save person
            if request.form.get('person_id') != 'None':
                person = Person.getPersons(id=request.form.get('person_id'))
            else:
                person = Person('', '', '', '', '', '', False, datetime.now(),
                                '', int(module[1]))
                db.session.add(person)

            person.firstname = request.form.get('firstname')
            person.lastname = request.form.get('lastname')
            person.salutation = request.form.get('salutation')
            person.grade = request.form.get('grade')
            person._dept = int(request.form.get('dept'))
            person.position = request.form.get('position')
            person.active = 'active' in request.form.keys()
            try:
                person.birthdate = datetime.strptime(
                    '{} 00:00:00'.format(request.form.get('birthdate')),
                    "%d.%m.%Y %H:%M:%S")
            except ValueError:
                pass
            person.identifier = request.form.get('identifier')
            person.remark = request.form.get('remark')
            _additional = {}
            for field in Settings.get('persons.settings',
                                      {}).get('additional', []):
                if field.split('=')[0] in request.form.keys(
                ) and request.form.get(field.split('=')[0]).strip() != '':
                    _additional[field.split('=')[0]] = request.form.get(
                        field.split('=')[0])
            person._options = yaml.safe_dump(_additional, encoding='utf-8')
            db.session.commit()

        elif request.form.get('action').startswith('deleteperson_'):
            db.session.delete(
                Person.getPersons(
                    id=request.form.get('action').split('_')[-1]))
            db.session.commit()

        elif request.form.get('action') == 'updategrades':
            grades = request.form.getlist('grade')
            while grades[-1] == grades[-2] == u"":  # remove last empty entries
                grades = grades[:-2]
            _settings = Settings.get('persons.settings', {})
            if 'positions' not in _settings.keys():
                _settings['positions'] = []
            _settings['grades'] = zip(*[grades[i::2] for i in range(2)])
            Settings.set('persons.settings', _settings)
            db.session.commit()

        elif request.form.get('action') == 'updatepositions':
            _settings = Settings.get('persons.settings', {})
            if 'grades' not in _settings.keys():
                _settings['grades'] = []
            _settings['positions'] = request.form.get('positions').replace(
                '\r', '').split('\n')
            Settings.set('persons.settings', _settings)
            db.session.commit()

        elif request.form.get('action') == 'updateadditional':
            _settings = Settings.get('persons.settings', {})
            if 'additional' not in _settings.keys():
                _settings['additional'] = []
            _settings['additional'] = request.form.get('additional').replace(
                '\r', '').split('\n')
            Settings.set('persons.settings', _settings)
            db.session.commit()

    if int(module[1]) == 0:
        params.update({'settings': Settings.get('persons.settings')})
        return render_template("admin.persons.settings.html", **params)
    persons = Person.getPersons(dept=int(module[1]))
    chars = {}
    for p in persons:
        chars[p.fullname[0].upper()] = 0
    params.update({
        'persons': persons,
        'chars': sorted(chars),
        'depid': module[1]
    })
    return render_template("admin.persons.html", **params)
Example #9
0
def getAdminData(self):
    if request.args.get('action') == '':
        pass

    elif request.args.get('action') == 'upload':
        if request.files:
            uploadfile = request.files['uploadfile']
            filename = werkzeug.secure_filename(uploadfile.filename)
            fname = os.path.join(current_app.config.get('PATH_TMP'), filename)
            uploadfile.save(fname)
            xlsfile = XLSFile(fname)
            uploadfiles[uploadfile.filename] = xlsfile
            return render_template(
                'admin.persons.upload2.html',
                sheets=uploadfiles[uploadfile.filename].getSheets())

    elif request.args.get('action') == 'upload_sheet':  # sheet selector
        definitionfile = uploadfiles[request.args.get('filename')]
        return render_template('admin.persons.upload3.html',
                               cols=definitionfile.getCols(
                                   request.args.get('sheetname')))

    elif request.args.get('action') == 'testimport':  # build data for preview
        col_definition = dict(zip(request.form.keys(), request.form.values()))
        col_definition.update({
            'sheet': request.args.get('sheetname'),
            'department': request.args.get('department')
        })

        deffile = uploadfiles[request.args.get('filename')]
        vals = deffile.getValues(col_definition)
        return render_template('admin.persons.uploadpreview.html', vals=vals)

    elif request.args.get(
            'action') == 'doimport':  # do import and store values
        col_definition = dict(zip(request.form.keys(), request.form.values()))
        col_definition.update({
            'sheet': request.args.get('sheetname'),
            'department': request.args.get('department')
        })

        deffile = uploadfiles[request.args.get('filename')]

        vals = deffile.getValues(col_definition)
        states = []

        if request.form.get('add_new'):  # add new keys ( item state=-1)
            states.append('-1')

        if request.form.get(
                'add_update'):  # update existing keys (item state=1)
            states.append('1')

        for key in vals['persons']:  # item list
            if key['state'] in states:  # import only with correct state
                if key['state'] == '-1':  # add key
                    try:
                        key['birthdate'] = datetime.strptime(
                            key['birthdate'], '%d.%m.%Y')
                    except:
                        pass
                    key['active'] = key['active'] == 'Aktiv'
                    p = Person(key['firstname'], key['lastname'],
                               key['salutation'], key['grade'],
                               key['position'], key['identifier'],
                               key['active'], key['birthdate'], key['remark'],
                               int(request.args.get('department')))
                    db.session.add(p)
                    db.session.commit()
                    key['state'] = '0'

                elif key['state'] == '1':  # update key
                    p = Person.getPersons(id=key['dbid'])
                    if p:
                        p.firstname = key['firstname']
                        p.lastname = key['lastname']
                        p.grade = key['grade']
                        p.position = key['position']
                        db.session.commit()
        return ""