Example #1
0
    def test_gemeinsamImport_withUnpassendenUsername_shouldImportValueAndRepalceName(
            self):
        self.set_up()
        einzelbuchungen = viewcore.database_instance().einzelbuchungen
        einzelbuchungen.add(datum('01.01.2017'), 'Essen', 'some name', -1.54)

        requester.INSTANCE = RequesterStub({
            'https://test.test/getgemeinsam.php':
            self._IMPORT_DATA_GEMEINSAM_WRONG_SELF,
            'https://test.test/deletegemeinsam.php':
            '',
            'https://test.test/getusername.php':
            'Sebastian_Online'
        })

        context = import_data.index(
            PostRequest({
                'action': 'load_online_gemeinsame_transactions',
                'email': '',
                'server': 'test.test',
                'password': ''
            }))

        assert context['element_titel'] == 'Export / Import'
        assert len(
            viewcore.database_instance().gemeinsamebuchungen.content) == 2
        assert viewcore.database_instance(
        ).gemeinsamebuchungen.content.Person[0] == 'Sebastian'
        assert viewcore.database_instance(
        ).gemeinsamebuchungen.content.Person[1] == 'Maureen'

        assert requester.instance().call_count_of(
            'https://test.test/deletegemeinsam.php') == 1
        assert requester.instance().complete_call_count() == 3
Example #2
0
    def test_gemeinsamImport_withUnpassendenKategorie_shouldImportValueAndRequestmapping(
            self):
        self.set_up()
        einzelbuchungen = viewcore.database_instance().einzelbuchungen
        einzelbuchungen.add(datum('01.01.2017'), 'KeinEssen', 'some name',
                            -1.54)

        requester.INSTANCE = RequesterStub({
            'https://test.test/getgemeinsam.php':
            self._IMPORT_DATA_GEMEINSAM,
            'https://test.test/deletegemeinsam.php':
            '',
            'https://test.test/getusername.php':
            'Sebastian'
        })

        context = import_data.index(
            PostRequest({
                'action': 'load_online_gemeinsame_transactions',
                'email': '',
                'server': 'test.test',
                'password': ''
            }))

        assert context['element_titel'] == 'Kategorien zuweisen'
        assert context['import'] == self._IMPORT_DATA_GEMEINSAM
        assert context['unpassende_kategorien'] == ['Essen']

        context = import_data.index(
            PostRequest({
                'action': 'map_and_push',
                'Essen_mapping': 'neue Kategorie anlegen',
                'import': self._IMPORT_DATA_GEMEINSAM
            }))

        assert context['element_titel'] == 'Export / Import'
        assert len(
            viewcore.database_instance().gemeinsamebuchungen.content) == 2
        assert viewcore.database_instance(
        ).gemeinsamebuchungen.content.Person[0] == 'Sebastian'
        assert viewcore.database_instance(
        ).gemeinsamebuchungen.content.Person[1] == 'Maureen'

        assert viewcore.database_instance(
        ).gemeinsamebuchungen.content.Kategorie[0] == 'Essen'
        assert viewcore.database_instance(
        ).gemeinsamebuchungen.content.Kategorie[1] == 'Essen'

        assert requester.instance().call_count_of(
            'https://test.test/deletegemeinsam.php') == 1
        assert requester.instance().complete_call_count() == 3
Example #3
0
    def test_set_kategorien_with_ausgeschlossene_kategoerien_should_hide_ausgeschlossene_kategorien(
            self):
        self.set_up()

        viewcore.database_instance().einzelbuchungen.add(
            datum('20.01.1990'), 'JaEins', 'SomeTitle', -10)
        viewcore.database_instance().einzelbuchungen.add(
            datum('20.01.1990'), 'NeinEins', 'SomeTitle', -10)
        viewcore.database_instance().einzelbuchungen.add(
            datum('20.01.1990'), 'JaZwei', 'SomeTitle', -10)

        configuration.index(
            PostRequest({
                'action': 'set_ausgeschlossene_kategorien',
                'ausgeschlossene_kategorien': 'NeinEins'
            }))

        requester.INSTANCE = RequesterStub(
            {'https://test.test/setkategorien.php': ''})

        context = import_data.index(
            PostRequest({
                'action': 'set_kategorien',
                'email': '',
                'server': 'test.test',
                'password': ''
            }))

        assert requester.instance().data_of_request(
            'https://test.test/setkategorien.php'
        )[0]['kategorien'] == 'JaEins,JaZwei'
Example #4
0
def handle_request(request, import_prefix='', gemeinsam=False):
    print(request)
    imported_values = pandas.DataFrame([],
                                       columns=('Datum', 'Kategorie', 'Name',
                                                'Wert', ''))
    if request.method == "POST":
        if post_action_is(request, 'load_online_transactions'):
            serverurl = request.values['server']

            serverurl = _add_protokoll_if_needed(serverurl)
            _save_server_creds(serverurl, request.values['email'])

            r = requests.post(serverurl + '/getabrechnung.php',
                              data={
                                  'email': request.values['email'],
                                  'password': request.values['password']
                              })
            print(r.content)

            response = handle_request(PostRequest(
                {'import': r.content.decode("utf-8")}),
                                      import_prefix='Internet')
            r = requests.post(serverurl + '/deleteitems.php',
                              data={
                                  'email': request.values['email'],
                                  'password': request.values['password']
                              })
            return response

        if post_action_is(request, 'load_online_gemeinsame_transactions'):
            serverurl = request.values['server']
            serverurl = _add_protokoll_if_needed(serverurl)
            _save_server_creds(serverurl, request.values['email'])
            print(serverurl)

            online_username = requester.instance().post(
                serverurl + '/getusername.php',
                data={
                    'email': request.values['email'],
                    'password': request.values['password']
                })
            print('online username: '******'/getgemeinsam.php',
                data={
                    'email': request.values['email'],
                    'password': request.values['password']
                })
            print(online_content)

            table = _parse_table(online_content)
            print('table before person mapping', table)
            table.Person = table.Person.map(
                lambda x: viewcore.database_instance().name
                if x == online_username else configuration_provider.
                get_configuration('PARTNERNAME'))
            online_content = "#######MaschinenimportStart\n"
            online_content = online_content + table.to_csv(index=False)
            online_content = online_content + "#######MaschinenimportEnd\n"

            response = handle_request(PostRequest({'import': online_content}),
                                      import_prefix='Internet_Gemeinsam',
                                      gemeinsam=True)

            requester.instance().post(serverurl + '/deletegemeinsam.php',
                                      data={
                                          'email': request.values['email'],
                                          'password':
                                          request.values['password']
                                      })
            return response

        elif post_action_is(request, 'set_kategorien'):
            kategorien = ','.join(
                sorted(viewcore.database_instance().einzelbuchungen.
                       get_kategorien_ausgaben(
                           hide_ausgeschlossene_kategorien=True)))
            serverurl = request.values['server']

            serverurl = _add_protokoll_if_needed(serverurl)
            _save_server_creds(serverurl, request.values['email'])

            serverurl = serverurl + '/setkategorien.php'

            requester.instance().post(serverurl,
                                      data={
                                          'email': request.values['email'],
                                          'password':
                                          request.values['password'],
                                          'kategorien': kategorien
                                      })
        else:
            print(request.values)
            content = request.values['import'].replace('\r', '')
            FileSystem.instance().write(
                '../Import/' + import_prefix + 'Import_' + str(datetime.now()),
                content)

            imported_values = _parse_table(content)
            datenbank_kategorien = set(viewcore.database_instance().
                                       einzelbuchungen.get_alle_kategorien())
            nicht_passende_kategorien = []
            for imported_kategorie in set(imported_values.Kategorie):
                if imported_kategorie not in datenbank_kategorien:
                    nicht_passende_kategorien.append(imported_kategorie)

            if 'Person' in imported_values.columns:
                gemeinsam = True

            if not nicht_passende_kategorien:
                print('keine unpassenden kategorien gefunden')
                print('beginne mit dem direkten import')
                _import(imported_values, gemeinsam)

                context = viewcore.generate_base_context('import')
                last_elements = []
                for row_index, row in imported_values.iterrows():
                    last_elements.append((row_index, row.Datum, row.Name,
                                          row.Kategorie, row.Wert))
                context['ausgaben'] = last_elements
                return 'import.html', context

            elif _mapping_passt(request.values, nicht_passende_kategorien):
                print(
                    'import kann durchgeführt werden, weil mapping vorhanden')
                imported_values = _map_kategorien(imported_values,
                                                  nicht_passende_kategorien,
                                                  request.values)
                _import(imported_values, gemeinsam)

                context = viewcore.generate_base_context('import')
                last_elements = []
                for row_index, row in imported_values.iterrows():
                    last_elements.append((row_index, row.Datum, row.Name,
                                          row.Kategorie, row.Wert))
                context['ausgaben'] = last_elements

                return 'import.html', context

            print("Nicht passende Kategorien: ", nicht_passende_kategorien)
            options = ['neue Kategorie anlegen']
            for kategorie_option in datenbank_kategorien:
                options.append('als ' + str(kategorie_option) + ' importieren')
            options = sorted(options)
            options.insert(0, 'neue Kategorie anlegen')
            context = viewcore.generate_base_context('import')
            context['element_titel'] = 'Kategorien zuweisen'
            context['unpassende_kategorien'] = nicht_passende_kategorien
            context['optionen'] = options
            context['import'] = request.values['import']
            context['transaction_id'] = 'requested'
            return 'import_mapping.html', context

    context = viewcore.generate_base_context('import')
    context[
        'ONLINE_DEFAULT_SERVER'] = configuration_provider.get_configuration(
            'ONLINE_DEFAULT_SERVER')
    context['ONLINE_DEFAULT_USER'] = configuration_provider.get_configuration(
        'ONLINE_DEFAULT_USER')
    return 'import.html', context