def test_apply_tarifs_changes(self): tarifs = Tarifs.get_hebergement_tarifs(81) tarif_pk = tarifs[0].pk self.assertEqual(len(tarifs), 1) self.assertEqual(tarifs[0].min, 50) self.assertEqual(tarifs[0].max, 60) # New values given self.view.request = TestRequest( form={'tarif_min_LOW_SEASON_WEEK': '100', 'tarif_max_LOW_SEASON_WEEK': '200', 'tarif_heb_pk': 81}) portal = self.layer['portal'] login(portal, 'manager') self.view.apply_tarifs_changes() transaction.commit() # New line inserted with new values tarifs = Tarifs.get_hebergement_tarifs(81) self.assertTrue(len(tarifs) > 1) for tarif in tarifs: if tarif.type == 'LOW_SEASON' and tarif.subtype == 'WEEK': self.assertNotEqual(tarif.pk, tarif_pk) self.assertEqual(tarif.min, 100) self.assertEqual(tarif.max, 200) self.assertEqual(tarif.valid, True)
def test_apply_tarifs_changes_exist(self): """ cf if to_confirm_exist: in tarif_edition """ tarifs = Tarifs.get_hebergement_tarifs(81) tarif_pk = tarifs[0].pk self.assertEqual(len(tarifs), 1) self.assertEqual(tarifs[0].min, 50) self.assertEqual(tarifs[0].max, 60) # Same values given self.view.request = TestRequest( form={'tarif_min_LOW_SEASON_WEEK': '50', 'tarif_max_LOW_SEASON_WEEK': '60', 'tarif_heb_pk': 81}) # No new line inserted self.view.apply_tarifs_changes() transaction.commit() tarifs = Tarifs.get_hebergement_tarifs(81) for tarif in tarifs: if tarif.type == 'LOW_SEASON' and tarif.subtype == 'WEEK': self.assertEqual(tarif.pk, tarif_pk) self.assertEqual(tarif.min, 50) self.assertEqual(tarif.max, 60)
def _update_tarif(heb_pk, type, subtype, min, max, cmt, valid): """ Verify that the values in DB are different then insert new line If to_confirm with same value valid = True return If to_confirm with different value valid = False If tarif exists with same value do nothing If tarif does not exists with same value insert new tarif """ to_confirm_exist = Tarifs.get_hebergement_tarif_to_confirm_with_value( heb_pk=heb_pk, type=type, subtype=subtype, min=min, max=max, cmt=cmt) if to_confirm_exist: to_confirm_exist.valid = True to_confirm_exist.save() return if type == 'CHARGES': to_confirm = Tarifs.get_hebergement_tarif_to_confirm(heb_pk, type) else: to_confirm = Tarifs.get_hebergement_tarif_to_confirm(heb_pk, type, subtype) if to_confirm: to_confirm.valid = False to_confirm.save() exist = Tarifs.exists_tarifs(heb_pk=heb_pk, type=type, subtype=subtype, min=min, max=max, cmt=cmt) if not exist: # Insert new tarifs line tarif = Tarifs(heb_pk=heb_pk, type=type, subtype=subtype, min=min, max=max, cmt=cmt, date=datetime.now(), user=api.user.get_current().id, valid=valid) tarif.add()
def test_get_tarifs_to_confirm(self): tarifs = Tarifs.get_tarifs_to_confirm() self.assertEqual(len(tarifs), 4) self.assertEqual(tarifs[0].heb_pk, 81) self.assertEqual(tarifs[1].heb_pk, 82) self.assertEqual(tarifs[2].heb_pk, 83) self.assertEqual(tarifs[3].heb_pk, 84)
def mappingNotification(self, values): listing = [] for notif in values: table_pk = notif.table_pk column = notif.column if notif.table == 'link_hebergement_metadata': link = LinkHebergementMetadata.first( link_met_pk=notif.table_pk) meta = Metadata.first(met_pk=link.metadata_fk) table_pk = '%s (Heb: %s)' % (notif.table_pk, link.heb_fk) column = '%s (%s)' % (notif.column, meta.met_titre_fr) elif notif.table == 'tarifs': tarif = Tarifs.first(pk=notif.table_pk) table_pk = "%s (Heb: %s)" % (notif.table_pk, tarif.heb_pk) column = "%s (%s/%s)" % (notif.column, tarif.type, tarif.subtype) listing.append( type( 'obj', (object, ), { 'pk': notif.pk, 'origin': notif.origin, 'table': notif.table, 'column': column, 'table_pk': table_pk, 'old_value': notif.old_value, 'new_value': notif.new_value, 'date': notif.date, 'treated': notif.treated, 'cmt': notif.cmt, 'user': notif.user })) return listing
def test_get_hebergement_tarifs_charges(self): """ Get only one CHARGES even there is several active. Get the highest pk """ tarifs = Tarifs.get_hebergement_tarifs(85) self.assertEqual(len(tarifs), 1) self.assertEqual(tarifs[0].pk, 10)
def test_get_hebergement_tarif_to_confirm_with_value(self): tarif = Tarifs.get_hebergement_tarif_to_confirm_with_value( heb_pk=84, type='LOW_SEASON', subtype='WEEKEND', min='550', max='650', cmt=None) self.assertNotEqual(tarif, None) self.assertEqual(tarif.pk, 7) tarif = Tarifs.get_hebergement_tarif_to_confirm_with_value( heb_pk=84, type='LOW_SEASON', subtype='WEEKEND', min='551', max='651', cmt=None) self.assertEqual(tarif, None)
def get_item(self, item): # Get tarif to confirm values type = getattr(item, 'type', '') subtype = getattr(item, 'subtype', '') if not type and not subtype: return u'' to_confirm_item = Tarifs.get_hebergement_tarif_to_confirm(self.table.heb_pk, type, subtype) # Replace tarif to tarif_to_confirm if exists item = to_confirm_item and to_confirm_item or item return item
def test_apply_tarifs_changes_no_heb(self): tarifs = Tarifs.get_hebergement_tarifs(81) tarif_pk = tarifs[0].pk self.assertEqual(len(tarifs), 1) self.assertEqual(tarifs[0].min, 50) self.assertEqual(tarifs[0].max, 60) # No heb given self.view.request = TestRequest( form={'tarif_min_LOW_SEASON_WEEK': '100', 'tarif_max_LOW_SEASON_WEEK': '200'}) self.view.apply_tarifs_changes() transaction.commit() # Nothing changes tarifs = Tarifs.get_hebergement_tarifs(81) for tarif in tarifs: if tarif.type == 'LOW_SEASON' and tarif.subtype == 'WEEK': self.assertEqual(tarif.pk, tarif_pk) self.assertEqual(tarif.min, 50) self.assertEqual(tarif.max, 60)
def test_exists_tarifs(self): tarif_not_valid = Tarifs.exists_tarifs(heb_pk=84, type='LOW_SEASON', subtype='WEEKEND', min='550', max='650', cmt=None) self.assertEqual(tarif_not_valid, False) tarif_wrong_value = Tarifs.exists_tarifs(heb_pk=84, type='LOW_SEASON', subtype='WEEKEND', min='251', max='260', cmt=None) self.assertEqual(tarif_wrong_value, False) tarif = Tarifs.exists_tarifs(heb_pk=84, type='LOW_SEASON', subtype='WEEKEND', min='250', max='260', cmt=None) self.assertEqual(tarif, True)
def getLastTarifsChanges(self): """ Get last tarifs changes in origin DB for hebs that are in the 2 databases """ if self.origin == 'PIVOT': # Attention je fais 2 fois cette requete gdw_hebs = self.pg_session.query(Hebergement.heb_code_cgt).all() gdw_hebs_cgt = [i.heb_code_cgt for i in gdw_hebs] last_changes = TarifView.get_last_changes(self.date) last_changes = [i for i in last_changes if i.heb_code_cgt in gdw_hebs_cgt] elif self.origin == 'GDW': pivot_hebs = self.mysql_session.query(HebergementView.heb_code_cgt).all() pivot_hebs_cgt = [i.heb_code_cgt for i in pivot_hebs] last_changes = Tarifs.get_last_changes(self.date, session=self.pg_session, cgt_not_empty=True) last_changes = [i for i in last_changes if i.hebergement.heb_code_cgt in pivot_hebs_cgt] return last_changes
def compareTarifs(self): """ Compare tarifs between pivot and gites_wallons databases First get the last tarifs changes from the origin database Then compare with the destination database if the data is different Must be the same type, subtype, and heb pivot_tarif: code_cgt: GRNA1153 Basse saison - (Semaine) 200 gdw_tarif: heb_pk: 81 LOW_SEASON WEEK 100 return [{'table': 'tarifs', 'pk': tarif.pk, 'diff': [('min', 100, 110), ('max', 200, 210)]}, {'table': 'tarifs', 'pk': tarif.pk, 'diff': [('min', 50, 55), ('cmt', 'foo', 'bar')]}] """ origin_changes = self.getLastTarifsChanges() dest_hebergements = self.getHebergementsByCodeCgt( [i.heb_code_cgt for i in origin_changes]) result = [] for origin_change in origin_changes: dest_heb = dest_hebergements[origin_change.heb_code_cgt] comparisons = [] if self.origin == 'PIVOT': gdw_tarifs = Tarifs.get_hebergement_tarifs( heb_pk=dest_heb.heb_pk, session=self.pg_session) pivot_tarif = origin_change comparisons = self.get_tarifs_comparisons_pivot( gdw_tarifs, pivot_tarif) elif self.origin == 'GDW': gdw_tarif = origin_change pivot_tarifs = TarifView.get( heb_code_cgt=dest_heb.heb_code_cgt) comparisons = self.get_tarifs_comparisons_gdw( pivot_tarifs, gdw_tarif) result.extend(comparisons) return result
def values(self): heb = self._get_heb() if heb.type.type_heb_type == 'gite': tarifs_types = TarifsType.get(gite=True) else: tarifs_types = TarifsType.get(chambre=True) self.tarifs = Tarifs.get_hebergement_tarifs(heb.heb_pk) tarifs_table = [] for tarifs_type in tarifs_types: if self.section_types and tarifs_type.type not in self.section_types: continue if self.section_subtypes and tarifs_type.subtype not in self.section_subtypes: continue line = self._get_tarif_line(tarifs_type) if line: tarifs_table.append(line) return tarifs_table
def compareTarifs(self): """ Compare tarifs between pivot and gites_wallons databases First get the last tarifs changes from the origin database Then compare with the destination database if the data is different Must be the same type, subtype, and heb pivot_tarif: code_cgt: GRNA1153 Basse saison - (Semaine) 200 gdw_tarif: heb_pk: 81 LOW_SEASON WEEK 100 return [{'table': 'tarifs', 'pk': tarif.pk, 'diff': [('min', 100, 110), ('max', 200, 210)]}, {'table': 'tarifs', 'pk': tarif.pk, 'diff': [('min', 50, 55), ('cmt', 'foo', 'bar')]}] """ origin_changes = self.getLastTarifsChanges() dest_hebergements = self.getHebergementsByCodeCgt([i.heb_code_cgt for i in origin_changes]) result = [] for origin_change in origin_changes: dest_heb = dest_hebergements[origin_change.heb_code_cgt] comparisons = [] if self.origin == 'PIVOT': gdw_tarifs = Tarifs.get_hebergement_tarifs(heb_pk=dest_heb.heb_pk, session=self.pg_session) pivot_tarif = origin_change comparisons = self.get_tarifs_comparisons_pivot(gdw_tarifs, pivot_tarif) elif self.origin == 'GDW': gdw_tarif = origin_change pivot_tarifs = TarifView.get(heb_code_cgt=dest_heb.heb_code_cgt) comparisons = self.get_tarifs_comparisons_gdw(pivot_tarifs, gdw_tarif) result.extend(comparisons) return result
def getLastTarifsChanges(self): """ Get last tarifs changes in origin DB for hebs that are in the 2 databases """ if self.origin == 'PIVOT': # Attention je fais 2 fois cette requete gdw_hebs = self.pg_session.query(Hebergement.heb_code_cgt).all() gdw_hebs_cgt = [i.heb_code_cgt for i in gdw_hebs] last_changes = TarifView.get_last_changes(self.date) last_changes = [ i for i in last_changes if i.heb_code_cgt in gdw_hebs_cgt ] elif self.origin == 'GDW': pivot_hebs = self.mysql_session.query( HebergementView.heb_code_cgt).all() pivot_hebs_cgt = [i.heb_code_cgt for i in pivot_hebs] last_changes = Tarifs.get_last_changes(self.date, session=self.pg_session, cgt_not_empty=True) last_changes = [ i for i in last_changes if i.hebergement.heb_code_cgt in pivot_hebs_cgt ] return last_changes
def test_get_hebergement_tarifs_to_confirm(self): tarif = Tarifs.get_hebergement_tarif_to_confirm(heb_pk=83, type='LOW_SEASON', subtype='WEEK') self.assertNotEqual(tarif, None) self.assertEqual(tarif.pk, 6)
def test_get_hebergement_tarifs(self): tarifs = Tarifs.get_hebergement_tarifs(81) self.assertEqual(len(tarifs), 1) self.assertEqual(tarifs[0].pk, 2)
def values(self): return Tarifs.get_tarifs_to_confirm()