Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 34.34 KB | None | 0 0
  1. import functools
  2. import operator
  3. from itertools import chain
  4.  
  5. import django.db.models.options as options
  6. from django.conf import settings
  7. from django.contrib.humanize.templatetags.humanize import intcomma
  8. from django.db import models
  9. from django.db.models import Q
  10.  
  11. options.DEFAULT_NAMES += 'gds_database',
  12. GDS_URL = settings.GDS_URL
  13.  
  14.  
  15. class GdsModelQuerySet(models.QuerySet):
  16.     def active(self):
  17.         fields_conditions = {
  18.             'has_saved': {
  19.                 'has_saved': 1
  20.             },
  21.             'has_archived': {
  22.                 'has_archived': 0
  23.             },
  24.             'active': {
  25.                 'active': 1
  26.             },
  27.             'deleted_at': {
  28.                 'deleted_at__isnull': True
  29.             }
  30.         }
  31.         model_conditions = {}
  32.         for field in fields_conditions:
  33.             if field in self.model._meta.get_all_field_names():
  34.                 model_conditions.update(fields_conditions[field])
  35.         query_condition = functools.reduce(operator.and_, [Q(model_condition) for model_condition in
  36.                                                            model_conditions.items()])
  37.         return self.filter(query_condition)
  38.  
  39.  
  40. class GdsModel(models.Model):
  41.     objects = GdsModelQuerySet.as_manager()
  42.  
  43.     class Meta:
  44.         abstract = True
  45.  
  46.  
  47. class ArchiveTopics(GdsModel):
  48.     reason = models.CharField(max_length=255)
  49.  
  50.     class Meta:
  51.         managed = False
  52.         db_table = 'archive_topics'
  53.         gds_database = 'gds'
  54.  
  55.  
  56. class Areas(GdsModel):
  57.     name = models.CharField(max_length=255)
  58.     plural_name = models.CharField(max_length=255)
  59.     parent_area = models.ForeignKey('self', models.DO_NOTHING, blank=True, null=True)
  60.     side = models.CharField(max_length=255)
  61.     added_by = models.ForeignKey('Users', models.DO_NOTHING, db_column='added_by')
  62.     created_at = models.DateTimeField(blank=True, null=True)
  63.     updated_at = models.DateTimeField(blank=True, null=True)
  64.     has_archived = models.IntegerField()
  65.     archive_reason_topic = models.ForeignKey(ArchiveTopics, models.DO_NOTHING,
  66.                                              db_column='archive_reason_topic', blank=True,
  67.                                              null=True)
  68.     archive_reason_freetext = models.TextField()
  69.  
  70.     class Meta:
  71.         managed = False
  72.         db_table = 'areas'
  73.         gds_database = 'gds'
  74.  
  75.  
  76. class AreasCategoriesLookup(GdsModel):
  77.     area = models.ForeignKey(Areas, models.DO_NOTHING)
  78.     category = models.ForeignKey('Categories', models.DO_NOTHING)
  79.  
  80.     class Meta:
  81.         managed = False
  82.         db_table = 'areas_categories_lookup'
  83.         gds_database = 'gds'
  84.  
  85.  
  86. class AreasMediaLookup(GdsModel):
  87.     area = models.ForeignKey(Areas, models.DO_NOTHING)
  88.     side_reference = models.CharField(max_length=50)
  89.     media = models.ForeignKey('Medias', models.DO_NOTHING)
  90.  
  91.     class Meta:
  92.         managed = False
  93.         db_table = 'areas_media_lookup'
  94.         gds_database = 'gds'
  95.  
  96.  
  97. class BusinessHoldingGroups(GdsModel):
  98.     name = models.CharField(max_length=100)
  99.     created_at = models.DateTimeField()
  100.     updated_at = models.DateTimeField()
  101.     added_by = models.ForeignKey('Users', models.DO_NOTHING, db_column='added_by', blank=True,
  102.                                  null=True)
  103.  
  104.     class Meta:
  105.         managed = False
  106.         gds_database = 'gds'
  107.         db_table = 'business_holding_groups'
  108.  
  109.  
  110. class Categories(GdsModel):  # CF
  111.     cat_name = models.CharField(max_length=200)
  112.     description = models.TextField()
  113.     active = models.IntegerField(blank=True, null=True)
  114.     created_at = models.DateTimeField(blank=True, null=True)
  115.     updated_at = models.DateTimeField(blank=True, null=True)
  116.  
  117.     class Meta:
  118.         managed = False
  119.         gds_database = 'gds'
  120.         db_table = 'categories'
  121.  
  122.  
  123. class CategoriesCsLookup(GdsModel):
  124.     category = models.ForeignKey(Categories, models.DO_NOTHING)
  125.     cs = models.ForeignKey('Cs', models.DO_NOTHING, blank=True, null=True)
  126.     cs_template = models.ForeignKey('CsTemplates', models.DO_NOTHING, blank=True, null=True)
  127.     sort = models.IntegerField()
  128.  
  129.     class Meta:
  130.         managed = False
  131.         gds_database = 'gds'
  132.         db_table = 'categories_cs_lookup'
  133.  
  134.  
  135. class Cc(GdsModel):
  136.     hashed_id = models.CharField(max_length=40)
  137.     name = models.CharField(max_length=100)
  138.     description = models.TextField()
  139.     cog = models.ForeignKey('Co', models.DO_NOTHING, blank=True, null=True)
  140.     active = models.IntegerField()
  141.     required = models.IntegerField()
  142.     created_at = models.DateTimeField(blank=True, null=True)
  143.     updated_at = models.DateTimeField(blank=True, null=True)
  144.  
  145.     class Meta:
  146.         managed = False
  147.         gds_database = 'gds'
  148.         db_table = 'cc'
  149.  
  150.  
  151. class Cg(GdsModel):  # Care Giver
  152.     hashed_id = models.CharField(max_length=40)
  153.     title = models.CharField(max_length=20)
  154.     first_name = models.CharField(max_length=50)
  155.     last_name = models.CharField(max_length=50)
  156.     nickname = models.CharField(max_length=50)
  157.     pronoun = models.CharField(max_length=50)
  158.     noun = models.CharField(max_length=50)
  159.     description = models.TextField()
  160.     active = models.IntegerField()
  161.     created_at = models.DateTimeField(blank=True, null=True)
  162.     updated_at = models.DateTimeField(blank=True, null=True)
  163.     added_by = models.IntegerField(blank=True, null=True)
  164.     deleted_at = models.DateTimeField(blank=True, null=True)
  165.  
  166.     class Meta:
  167.         managed = False
  168.         gds_database = 'gds'
  169.         db_table = 'cg'
  170.  
  171.     def __str__(self):
  172.         return self.title
  173.  
  174.  
  175. class CgLookup(GdsModel):
  176.     cg = models.ForeignKey(Cg, models.DO_NOTHING)
  177.     fk_table = models.CharField(max_length=200)
  178.     fk_id = models.IntegerField()
  179.     pref = models.IntegerField()
  180.     created_at = models.DateTimeField()
  181.     updated_at = models.DateTimeField(blank=True, null=True)
  182.  
  183.     class Meta:
  184.         managed = False
  185.         gds_database = 'gds'
  186.         db_table = 'cg_lookup'
  187.  
  188.  
  189. class CgQualifications(GdsModel):
  190.     cg = models.ForeignKey(Cg, models.DO_NOTHING, related_name='qualifications')
  191.     type = models.CharField(max_length=21)
  192.     start_year = models.TextField()
  193.     end_year = models.TextField()
  194.     name = models.CharField(max_length=255)
  195.     edu = models.ForeignKey('EducationOrganizations', models.DO_NOTHING, blank=True, null=True)
  196.     city = models.ForeignKey('Cities', models.DO_NOTHING, blank=True, null=True)
  197.     country = models.ForeignKey('Countries', models.DO_NOTHING, blank=True, null=True)
  198.     created_at = models.DateTimeField()
  199.     updated_at = models.DateTimeField()
  200.     added_by = models.ForeignKey('Users', models.DO_NOTHING, db_column='added_by', blank=True,
  201.                                  null=True)
  202.     deleted_at = models.DateTimeField(blank=True, null=True)
  203.  
  204.     class Meta:
  205.         managed = False
  206.         gds_database = 'gds'
  207.         db_table = 'cg_qualifications'
  208.  
  209.  
  210. class Cities(GdsModel):
  211.     name = models.CharField(max_length=100)
  212.  
  213.     class Meta:
  214.         managed = False
  215.         gds_database = 'gds'
  216.         db_table = 'cities'
  217.  
  218.  
  219. class Cn(GdsModel):
  220.     hashed_id = models.CharField(max_length=40)
  221.     name = models.CharField(max_length=100)
  222.     description = models.TextField()
  223.     cog = models.ForeignKey('Co', models.DO_NOTHING, blank=True, null=True)
  224.     active = models.IntegerField()
  225.     required = models.IntegerField()
  226.     created_at = models.DateTimeField(blank=True, null=True)
  227.     updated_at = models.DateTimeField(blank=True, null=True)
  228.  
  229.     class Meta:
  230.         managed = False
  231.         gds_database = 'gds'
  232.         db_table = 'cn'
  233.  
  234.  
  235. class Co(GdsModel):  # COG (is a category of Inventories)
  236.     hashed_id = models.CharField(max_length=40)
  237.     co = models.CharField(max_length=100)
  238.     description = models.TextField()
  239.     code = models.CharField(max_length=4, blank=True, null=True)
  240.     active = models.IntegerField()
  241.     created_at = models.DateTimeField(blank=True, null=True)
  242.     updated_at = models.DateTimeField(blank=True, null=True)
  243.     rank = models.IntegerField()
  244.     has_saved = models.IntegerField()
  245.     deleted_at = models.DateTimeField(blank=True, null=True)
  246.  
  247.     class Meta:
  248.         managed = False
  249.         gds_database = 'gds'
  250.         db_table = 'co'
  251.  
  252.     def __str__(self):
  253.         return self.co
  254.  
  255.  
  256. class CoCategoriesLookup(GdsModel):
  257.     co = models.ForeignKey(Co, models.DO_NOTHING)
  258.     category = models.ForeignKey(Categories, models.DO_NOTHING)
  259.  
  260.     class Meta:
  261.         managed = False
  262.         db_table = 'co_categories_lookup'
  263.         gds_database = 'gds'
  264.  
  265.  
  266. class CoCcLookup(GdsModel):
  267.     co_id = models.IntegerField()
  268.     cc_id = models.IntegerField()
  269.     created_at = models.DateTimeField(blank=True, null=True)
  270.     updated_at = models.DateTimeField(blank=True, null=True)
  271.  
  272.     class Meta:
  273.         managed = False
  274.         db_table = 'co_cc_lookup'
  275.         gds_database = 'gds'
  276.  
  277.  
  278. class CoCnLookup(GdsModel):
  279.     co_id = models.IntegerField()
  280.     cn_id = models.IntegerField()
  281.     created_at = models.DateTimeField(blank=True, null=True)
  282.     updated_at = models.DateTimeField(blank=True, null=True)
  283.  
  284.     class Meta:
  285.         managed = False
  286.         db_table = 'co_cn_lookup'
  287.         gds_database = 'gds'
  288.  
  289.  
  290. class CofCriteriaLookup(GdsModel):
  291.     cof = models.ForeignKey('gds.Cof', models.DO_NOTHING)
  292.     criteria = models.ForeignKey('CriteriaTypes', models.DO_NOTHING)
  293.     set_value = models.CharField(max_length=255)
  294.     created_at = models.DateTimeField(blank=True, null=True)
  295.     updated_at = models.DateTimeField(blank=True, null=True)
  296.  
  297.     class Meta:
  298.         managed = False
  299.         db_table = 'cof_criteria_lookup'
  300.         gds_database = 'gds'
  301.  
  302.  
  303. class Complexities(GdsModel):
  304.     complexity = models.CharField(max_length=150)
  305.     complexity_type = models.ForeignKey('ComplexityTypes', models.DO_NOTHING, blank=True, null=True)
  306.     active = models.IntegerField()
  307.     created_at = models.DateTimeField(blank=True, null=True)
  308.     updated_at = models.DateTimeField(blank=True, null=True)
  309.  
  310.     class Meta:
  311.         managed = False
  312.         gds_database = 'gds'
  313.         db_table = 'complexities'
  314.  
  315.  
  316. class ComplexityTypes(GdsModel):
  317.     type = models.CharField(max_length=200, blank=True, null=True)
  318.     active = models.IntegerField()
  319.     created_at = models.DateTimeField(blank=True, null=True)
  320.     updated_at = models.DateTimeField(blank=True, null=True)
  321.  
  322.     class Meta:
  323.         managed = False
  324.         gds_database = 'gds'
  325.         db_table = 'complexity_types'
  326.  
  327.  
  328. class Config(GdsModel):
  329.     config_name = models.CharField(max_length=255)
  330.     config_value = models.CharField(max_length=255)
  331.  
  332.     class Meta:
  333.         managed = False
  334.         db_table = 'config'
  335.         gds_database = 'gds'
  336.  
  337.  
  338. class Countries(GdsModel):
  339.     country_code = models.CharField(max_length=2)
  340.     country_name = models.CharField(max_length=45)
  341.  
  342.     class Meta:
  343.         managed = False
  344.         gds_database = 'gds'
  345.         db_table = 'countries'
  346.  
  347.     def __str__(self):
  348.         return self.country_name
  349.  
  350.  
  351. class Cp(GdsModel):  # care partner
  352.     hashed_id = models.CharField(max_length=40)
  353.     cp_name = models.CharField(max_length=100)
  354.     destination = models.ForeignKey('Destinations', models.DO_NOTHING, db_column='destination',
  355.                                     blank=True, null=True)
  356.     description = models.TextField()
  357.     code = models.CharField(max_length=4, blank=True, null=True)
  358.     type = models.CharField(max_length=8, blank=True, null=True)
  359.     bhg = models.ForeignKey(BusinessHoldingGroups, models.DO_NOTHING, blank=True, null=True)
  360.     transfer_cp = models.CharField(max_length=10, blank=True, null=True)
  361.     transfer_airport = models.CharField(max_length=10, blank=True, null=True)
  362.     discount = models.CharField(max_length=10, blank=True, null=True)
  363.     alt = models.IntegerField()
  364.     active = models.IntegerField()
  365.     rank = models.IntegerField()
  366.     created_at = models.DateTimeField(blank=True, null=True)
  367.     updated_at = models.DateTimeField(blank=True, null=True)
  368.     added_by = models.ForeignKey('Users', models.DO_NOTHING, db_column='added_by', blank=True,
  369.                                  null=True)
  370.     deleted_at = models.DateTimeField(blank=True, null=True)
  371.  
  372.     class Meta:
  373.         managed = False
  374.         gds_database = 'gds'
  375.         db_table = 'cp'
  376.  
  377.     def __str__(self):
  378.         return self.cp_name
  379.  
  380.  
  381. class CpCategoriesLookup(GdsModel):
  382.     cp = models.ForeignKey(Cp, models.DO_NOTHING)
  383.     category = models.ForeignKey(Categories, models.DO_NOTHING)
  384.  
  385.     class Meta:
  386.         managed = False
  387.         gds_database = 'gds'
  388.         db_table = 'cp_categories_lookup'
  389.  
  390.  
  391. class CriteriaTypes(GdsModel):
  392.     criteria = models.CharField(max_length=200)
  393.     cost_driver = models.IntegerField(blank=True, null=True)
  394.     active = models.IntegerField()
  395.     private = models.IntegerField()
  396.     rank = models.IntegerField(blank=True, null=True)
  397.     created_at = models.DateTimeField(blank=True, null=True)
  398.     updated_at = models.DateTimeField(blank=True, null=True)
  399.     added_by = models.ForeignKey('Users', models.DO_NOTHING, db_column='added_by', blank=True,
  400.                                  null=True)
  401.  
  402.     class Meta:
  403.         managed = False
  404.         gds_database = 'gds'
  405.         db_table = 'criteria_types'
  406.  
  407.  
  408. class Cs(GdsModel):
  409.     name = models.CharField(max_length=255)
  410.     type = models.CharField(max_length=11, blank=True, null=True)
  411.     is_substep = models.IntegerField()
  412.     created_at = models.DateTimeField(blank=True, null=True)
  413.     updated_at = models.DateTimeField(blank=True, null=True)
  414.  
  415.     class Meta:
  416.         managed = False
  417.         gds_database = 'gds'
  418.         db_table = 'cs'
  419.  
  420.     def __str__(self):
  421.         return self.name
  422.  
  423.  
  424. class CsSubstepsLookup(GdsModel):
  425.     cs = models.ForeignKey(Cs, models.DO_NOTHING, related_name="care_steps")
  426.     substep = models.ForeignKey(Cs, models.DO_NOTHING)
  427.     sort = models.IntegerField()
  428.  
  429.     class Meta:
  430.         managed = False
  431.         gds_database = 'gds'
  432.         db_table = 'cs_substeps_lookup'
  433.  
  434.  
  435. class CsTemplateCs(GdsModel):
  436.     cs = models.ForeignKey(Cs, models.DO_NOTHING)
  437.     cs_template = models.ForeignKey('CsTemplates', models.DO_NOTHING)
  438.     sort = models.IntegerField()
  439.  
  440.     class Meta:
  441.         managed = False
  442.         gds_database = 'gds'
  443.         db_table = 'cs_template_cs'
  444.  
  445.  
  446. class CsTemplates(GdsModel):
  447.     name = models.CharField(max_length=255)
  448.     created_at = models.DateTimeField(blank=True, null=True)
  449.     updated_at = models.DateTimeField(blank=True, null=True)
  450.  
  451.     class Meta:
  452.         managed = False
  453.         gds_database = 'gds'
  454.         db_table = 'cs_templates'
  455.  
  456.  
  457. class Currency(GdsModel):
  458.     country = models.ForeignKey(Countries, models.DO_NOTHING, db_column='country')
  459.     currency = models.CharField(max_length=100, blank=True, null=True)
  460.     currency_code = models.CharField(max_length=100)
  461.     currency_symbol = models.CharField(max_length=100)
  462.     active = models.IntegerField()
  463.     created_at = models.DateTimeField(blank=True, null=True)
  464.     updated_at = models.DateTimeField(blank=True, null=True)
  465.  
  466.     class Meta:
  467.         managed = False
  468.         gds_database = 'gds'
  469.         db_table = 'currency'
  470.  
  471.     def __str__(self):
  472.         return self.currency
  473.  
  474.  
  475. class Destinations(GdsModel):
  476.     hashed_id = models.CharField(max_length=40)
  477.     country = models.ForeignKey(Countries, models.DO_NOTHING, blank=True, null=True)
  478.     name = models.CharField(max_length=50)
  479.     active = models.IntegerField()
  480.     description = models.TextField()
  481.     rank = models.IntegerField()
  482.     created_at = models.DateTimeField(blank=True, null=True)
  483.     updated_at = models.DateTimeField(blank=True, null=True)
  484.     added_by = models.ForeignKey('Users', models.DO_NOTHING, db_column='added_by', blank=True,
  485.                                  null=True)
  486.  
  487.     class Meta:
  488.         managed = False
  489.         gds_database = 'gds'
  490.         db_table = 'destinations'
  491.  
  492.     def __str__(self):
  493.         return '{} - {}'.format(self.name, self.country)
  494.  
  495.  
  496. class DiscountPolicies(GdsModel):
  497.     cp = models.ForeignKey(Cp, models.DO_NOTHING)
  498.     min_price = models.CharField(max_length=100)
  499.     amount = models.CharField(max_length=100)
  500.     category = models.ForeignKey(Categories, models.DO_NOTHING, blank=True, null=True)
  501.     rule = models.CharField(max_length=255)
  502.  
  503.     class Meta:
  504.         managed = False
  505.         gds_database = 'gds'
  506.         db_table = 'discount_policies'
  507.  
  508.  
  509. class EducationOrganizations(GdsModel):
  510.     name = models.CharField(max_length=200)
  511.     created_at = models.DateTimeField()
  512.     updated_at = models.DateTimeField()
  513.     added_by = models.ForeignKey('Users', models.DO_NOTHING, db_column='added_by', blank=True,
  514.                                  null=True)
  515.     deleted_at = models.DateTimeField(blank=True, null=True)
  516.  
  517.     class Meta:
  518.         managed = False
  519.         gds_database = 'gds'
  520.         db_table = 'education_organizations'
  521.  
  522.  
  523. class Inventory(GdsModel):  # also knows as Co
  524.  
  525.     argument_types = ['co.co']  # used for search
  526.  
  527.     hashed_id = models.CharField(max_length=40)
  528.     cp = models.ForeignKey(Cp, models.DO_NOTHING, blank=True, null=True)
  529.     co = models.ForeignKey(Co, models.DO_NOTHING, blank=True, null=True)
  530.     cg = models.ForeignKey(Cg, models.DO_NOTHING, blank=True, null=True)
  531.     min_price = models.DecimalField(max_digits=15, decimal_places=2)
  532.     max_price = models.DecimalField(max_digits=15, decimal_places=2)
  533.     preop = models.IntegerField()
  534.     currency = models.ForeignKey(Currency, models.DO_NOTHING, blank=True, null=True)
  535.     cat = models.ForeignKey(Categories, models.DO_NOTHING, blank=True, null=True)
  536.     notes = models.CharField(max_length=255, blank=True, null=True)
  537.     consultation = models.CharField(max_length=100, blank=True, null=True)
  538.     planned_tests = models.CharField(max_length=100, blank=True, null=True)
  539.     possible_tests = models.CharField(max_length=100, blank=True, null=True)
  540.     type = models.CharField(max_length=3)
  541.     night_icu = models.CharField(max_length=30, blank=True, null=True)
  542.     night_hospital = models.CharField(max_length=30, blank=True, null=True)
  543.     night_country = models.CharField(max_length=30, blank=True, null=True)
  544.     active = models.IntegerField()
  545.     package = models.IntegerField()
  546.     promo = models.IntegerField()
  547.     start_timestamp = models.DateTimeField(blank=True, null=True)
  548.     end_timestamp = models.DateTimeField(blank=True, null=True)
  549.     created_at = models.DateTimeField(blank=True, null=True)
  550.     updated_at = models.DateTimeField(blank=True, null=True)
  551.     session_id = models.CharField(max_length=100)
  552.     added_by = models.ForeignKey('Users', models.DO_NOTHING, db_column='added_by')
  553.     has_saved = models.IntegerField()
  554.     has_archived = models.IntegerField()
  555.     archive_reason_topic = models.ForeignKey(ArchiveTopics, models.DO_NOTHING,
  556.                                              db_column='archive_reason_topic', blank=True,
  557.                                              null=True)
  558.     archive_reason_freetext = models.TextField(blank=True, null=True)
  559.     deleted_at = models.DateTimeField(blank=True, null=True)
  560.  
  561.     class Meta:
  562.         managed = False
  563.         gds_database = 'gds'
  564.         db_table = 'inventory'
  565.  
  566.     def __str__(self):
  567.         if self.co:
  568.             return self.co.co
  569.         return 'Invalid inventory'
  570.  
  571.     def _yesno(self, val):
  572.         answer = 'Y' if val else 'N'
  573.         return answer
  574.  
  575.     def _string_to_bool(self, s):
  576.         return len(s.strip()) > 0
  577.  
  578.     def is_available(self):
  579.         return any([self._string_to_bool(self.get_cg()),
  580.                     self._string_to_bool(self.get_cp_code())])
  581.  
  582.     def get_cp_code(self):
  583.         if self.cp:
  584.             return self.cp.code
  585.         return ' '
  586.  
  587.     def get_co_name(self):
  588.         if self.co:
  589.             return self.co.co
  590.  
  591.     def get_package(self):
  592.         return 'Package: {}'.format(self._yesno(self.package))
  593.  
  594.     def get_promo(self):
  595.         return 'Promo: {}'.format(self._yesno(self.promo))
  596.  
  597.     def _format_price(self, price):
  598.         decimal_part_formatted_price = "{0:.2f}".format(price)
  599.         humanized_formatted_price = intcomma(decimal_part_formatted_price, use_l10n=False)
  600.         return humanized_formatted_price.rstrip('0').rstrip(
  601.             '.') if '.' in humanized_formatted_price else humanized_formatted_price
  602.  
  603.     def get_min_max_price(self):
  604.         currency_symbol = ''
  605.         currency_code = ''
  606.         if self.currency:
  607.             currency_symbol = self.currency.currency_symbol
  608.             currency_code = self.currency.currency_code
  609.         if self.min_price and self.max_price:
  610.             return '{}{}-{} {}'.format(currency_symbol, self._format_price(self.min_price),
  611.                                        self._format_price(self.max_price), currency_code)
  612.         elif self.min_price:
  613.             return '{}{} {}'.format(currency_symbol, self._format_price(self.min_price),
  614.                                     currency_code)
  615.         return ''
  616.  
  617.     def get_min_price(self):
  618.         return 'MIN:{}'.format(self.min_price)
  619.  
  620.     def get_max_price(self):
  621.         return 'MAX:{}'.format(self.max_price)
  622.  
  623.     def get_preop(self):
  624.         return 'Pre-Op Incl: {}'.format(self._yesno(self.preop))
  625.  
  626.     def get_type(self):
  627.         if self.type:
  628.             return '{}: Y'.format(self.type)
  629.         return ''
  630.  
  631.     def get_night_hospital(self):
  632.         if self.night_hospital:
  633.             return 'N/H: {}'.format(self.night_hospital)
  634.         return ''
  635.  
  636.     def get_night_country(self):
  637.         if self.night_country:
  638.             return 'N/C: {}'.format(self.night_country)
  639.         return ''
  640.  
  641.     def get_name(self):
  642.         if self.co:
  643.             return self.co.co
  644.         return 'Name does not exist'
  645.  
  646.     def get_cg(self):
  647.         if self.cg:
  648.             return self.cg.first_name
  649.         return ''
  650.  
  651.     def get_cp(self):
  652.         if self.cp:
  653.             return self.cp.cp_name
  654.         return ''
  655.  
  656.     def get_owner(self):
  657.         if self.cg and self.cp:
  658.             return '{cg_title} {cg_first_name} ({cp_code})'.format(cg_title=self.cg.title,
  659.                                                                    cg_first_name=self.cg.first_name,
  660.                                                                    cp_code=self.cp.code)
  661.         elif self.cg and not self.cp:
  662.             return '{cg_title} {cg_first_name}'.format(cg_title=self.cg.title,
  663.                                                        cg_first_name=self.cg.first_name)
  664.         elif self.cp and not self.cg:
  665.             return self.cp.code
  666.         else:
  667.             return 'Bad Inventory!'
  668.  
  669.     def get_price(self):
  670.         currency_code = None
  671.         if self.currency:
  672.             currency_code = self.currency.currency_code
  673.         if self.max_price:
  674.             return '{}: {} - {}'.format(currency_code, self.min_price, self.max_price)
  675.         return '{}: {}'.format(currency_code, self.min_price)
  676.  
  677.     def get_criteria_types(self):
  678.         inventory_criteria_lookups = InventoryCriteriaLookup.objects.filter(inventory=self.id)
  679.         criteria_types_ids = inventory_criteria_lookups.values_list('criteria', flat=True)
  680.         return CriteriaTypes.objects.filter(id__in=criteria_types_ids).filter(cost_driver=1)
  681.  
  682.     def get_criteria_values(self):
  683.         all_criterias = InventoryCriteriaLookup.objects.filter(inventory=self.id)
  684.         criterias_with_costdriver_by_default = all_criterias.filter(
  685.             Q(criteria__cost_driver=1) & (Q(cost_driver__isnull=True))
  686.         )
  687.         criterias_with_overrided_costdriver = all_criterias.filter(cost_driver=1)
  688.         criterias_with_overrided_costdriver = sorted(criterias_with_overrided_costdriver,
  689.                                                      key=lambda instance: instance.id)
  690.         all_costdrivers_with_criteria = sorted(
  691.             chain(criterias_with_costdriver_by_default, criterias_with_overrided_costdriver),
  692.             key=lambda instance: instance.id)
  693.         return all_costdrivers_with_criteria
  694.  
  695.  
  696. class Cof(GdsModel):  # inventory fees
  697.     hashed_id = models.CharField(max_length=40)
  698.     name = models.CharField(max_length=255)
  699.     co = models.ForeignKey(Co, models.DO_NOTHING, blank=True, null=True)
  700.     cp = models.ForeignKey('Cp', models.DO_NOTHING, blank=True, null=True)
  701.     cg = models.ForeignKey(Cg, models.DO_NOTHING, blank=True, null=True)
  702.     min_price = models.CharField(max_length=100)
  703.     max_price = models.CharField(max_length=100)
  704.     currency = models.ForeignKey('Currency', models.DO_NOTHING, blank=True, null=True)
  705.     cat = models.ForeignKey(Categories, models.DO_NOTHING, blank=True, null=True)
  706.     notes = models.CharField(max_length=255)
  707.     type = models.CharField(max_length=3)
  708.     night_hospital = models.CharField(max_length=30)
  709.     night_country = models.CharField(max_length=30)
  710.     active = models.IntegerField()
  711.     package = models.IntegerField()
  712.     promo = models.IntegerField()
  713.     added_by = models.ForeignKey('Users', models.DO_NOTHING, db_column='added_by')
  714.     start_timestamp = models.DateTimeField(blank=True, null=True)
  715.     end_timestamp = models.DateTimeField(blank=True, null=True)
  716.     created_at = models.DateTimeField(blank=True, null=True)
  717.     updated_at = models.DateTimeField(blank=True, null=True)
  718.     deleted_at = models.DateTimeField(blank=True, null=True)
  719.  
  720.     class Meta:
  721.         managed = False
  722.         gds_database = 'gds'
  723.         db_table = 'cof'
  724.  
  725.  
  726. class InventoryCcoLookup(GdsModel):
  727.     inventory = models.ForeignKey(Inventory, models.DO_NOTHING)
  728.     co = models.ForeignKey(Co, models.DO_NOTHING)
  729.  
  730.     class Meta:
  731.         managed = False
  732.         gds_database = 'gds'
  733.         db_table = 'inventory_cco_lookup'
  734.  
  735.  
  736. class InventoryCofLookup(GdsModel):
  737.     inventory = models.ForeignKey(Inventory, models.DO_NOTHING)
  738.     cof = models.ForeignKey(Cof, models.DO_NOTHING)
  739.     created_at = models.DateTimeField(blank=True, null=True)
  740.     updated_at = models.DateTimeField(blank=True, null=True)
  741.  
  742.     class Meta:
  743.         managed = False
  744.         gds_database = 'gds'
  745.         db_table = 'inventory_cof_lookup'
  746.  
  747.  
  748. class InventoryComplexitiesLookup(GdsModel):
  749.     complexity = models.ForeignKey(Complexities, models.DO_NOTHING)
  750.     inventory = models.ForeignKey(Inventory, models.DO_NOTHING)
  751.     created_at = models.DateTimeField(blank=True, null=True)
  752.     updated_at = models.DateTimeField(blank=True, null=True)
  753.  
  754.     class Meta:
  755.         managed = False
  756.         gds_database = 'gds'
  757.         db_table = 'inventory_complexities_lookup'
  758.  
  759.  
  760. class InventoryCriteriaLookup(GdsModel):
  761.     inventory = models.ForeignKey(Inventory, models.DO_NOTHING)
  762.     criteria = models.ForeignKey(CriteriaTypes, models.DO_NOTHING)
  763.     set_value = models.CharField(max_length=255)
  764.     cost_driver = models.IntegerField(blank=True, null=True)
  765.     created_at = models.DateTimeField()
  766.     updated_at = models.DateTimeField()
  767.  
  768.     def __str__(self):
  769.         return '{}: {}'.format(self.criteria.criteria, self.set_value)
  770.  
  771.     class Meta:
  772.         managed = False
  773.         gds_database = 'gds'
  774.         db_table = 'inventory_criteria_lookup'
  775.  
  776.  
  777. class InventorySources(GdsModel):
  778.     inventory = models.ForeignKey(Inventory, models.DO_NOTHING)
  779.     name = models.CharField(max_length=255, blank=True, null=True)
  780.     source = models.CharField(max_length=20, blank=True, null=True)
  781.     stamp = models.DateField(blank=True, null=True)
  782.     found_user = models.ForeignKey('Users', models.DO_NOTHING, blank=True, null=True)
  783.  
  784.     class Meta:
  785.         managed = False
  786.         gds_database = 'gds'
  787.         db_table = 'inventory_sources'
  788.  
  789.  
  790. class Knowledge(GdsModel):
  791.     hashed_id = models.CharField(max_length=40)
  792.     knowledge = models.TextField()
  793.     updated_by = models.ForeignKey('Users', models.DO_NOTHING, db_column='updated_by', blank=True,
  794.                                    null=True)
  795.     has_archived = models.IntegerField()
  796.     archive_reason_topic = models.ForeignKey(ArchiveTopics, models.DO_NOTHING,
  797.                                              db_column='archive_reason_topic', blank=True,
  798.                                              null=True)
  799.     archive_reason_freetext = models.TextField(blank=True, null=True)
  800.     is_moderated = models.IntegerField()
  801.     email_sent = models.IntegerField()
  802.     updated_at = models.DateTimeField()
  803.     created_at = models.DateTimeField()
  804.  
  805.     class Meta:
  806.         managed = False
  807.         db_table = 'knowledge'
  808.         gds_database = 'gds'
  809.  
  810.  
  811. class KnowledgeMediaLookup(GdsModel):
  812.     knowledge = models.ForeignKey(Knowledge, models.DO_NOTHING)
  813.     custom_file_name = models.CharField(max_length=255)
  814.     media = models.ForeignKey('Medias', models.DO_NOTHING)
  815.  
  816.     class Meta:
  817.         managed = False
  818.         db_table = 'knowledge_media_lookup'
  819.         gds_database = 'gds'
  820.  
  821.  
  822. class KnowledgeSources(GdsModel):
  823.     knowledge = models.ForeignKey(Knowledge, models.DO_NOTHING)
  824.     name = models.CharField(max_length=255, blank=True, null=True)
  825.     source = models.CharField(max_length=20, blank=True, null=True)
  826.     stamp = models.DateField(blank=True, null=True)
  827.     found_user = models.ForeignKey('Users', models.DO_NOTHING, blank=True, null=True)
  828.  
  829.     class Meta:
  830.         managed = False
  831.         db_table = 'knowledge_sources'
  832.         gds_database = 'gds'
  833.  
  834.  
  835. class MediaLookup(GdsModel):
  836.     fk_table = models.CharField(max_length=200)
  837.     fk_id = models.IntegerField()
  838.     media = models.ForeignKey('Medias', models.DO_NOTHING)
  839.     created_at = models.DateTimeField()
  840.     updated_at = models.DateTimeField()
  841.  
  842.     class Meta:
  843.         managed = False
  844.         gds_database = 'gds'
  845.         db_table = 'media_lookup'
  846.  
  847.  
  848. class MediaTypes(GdsModel):
  849.     content_type = models.CharField(max_length=50)
  850.     created_at = models.DateTimeField()
  851.     updated_at = models.DateTimeField(blank=True, null=True)
  852.  
  853.     class Meta:
  854.         managed = False
  855.         gds_database = 'gds'
  856.         db_table = 'media_types'
  857.  
  858.  
  859. class Medias(GdsModel):
  860.     content_url = models.CharField(max_length=512)
  861.     media_type = models.ForeignKey(MediaTypes, models.DO_NOTHING, blank=True, null=True)
  862.     extension_type = models.CharField(max_length=5, blank=True, null=True)
  863.     caption = models.CharField(max_length=255)
  864.     is_title_photo = models.IntegerField()
  865.     is_logo = models.IntegerField()
  866.     is_profile = models.IntegerField()
  867.     is_icon = models.IntegerField()
  868.     created_at = models.DateTimeField()
  869.     updated_at = models.DateTimeField(blank=True, null=True)
  870.     added_by = models.ForeignKey('Users', models.DO_NOTHING, db_column='added_by')
  871.     deleted_at = models.DateTimeField(blank=True, null=True)
  872.  
  873.     class Meta:
  874.         managed = False
  875.         gds_database = 'gds'
  876.         db_table = 'medias'
  877.  
  878.  
  879. class Migrations(GdsModel):
  880.     migration = models.CharField(max_length=255)
  881.     batch = models.IntegerField()
  882.  
  883.     class Meta:
  884.         managed = False
  885.         gds_database = 'gds'
  886.         db_table = 'migrations'
  887.  
  888.  
  889. class PasswordResets(GdsModel):
  890.     email = models.CharField(max_length=255)
  891.     token = models.CharField(max_length=255)
  892.     created_at = models.DateTimeField()
  893.  
  894.     class Meta:
  895.         managed = False
  896.         gds_database = 'gds'
  897.         db_table = 'password_resets'
  898.  
  899.  
  900. class Questions(GdsModel):  # CA
  901.     hashed_id = models.CharField(max_length=40)
  902.     question = models.TextField()
  903.     rnu = models.IntegerField()
  904.     answer = models.TextField()
  905.     active = models.IntegerField()
  906.     created_at = models.DateTimeField()
  907.     updated_at = models.DateTimeField(blank=True, null=True)
  908.     added_by = models.ForeignKey('Users', models.DO_NOTHING, db_column='added_by', blank=True,
  909.                                  null=True)
  910.     new_record_notification_sent = models.IntegerField()
  911.     deleted_at = models.DateTimeField(blank=True, null=True)
  912.  
  913.     def get_answer_link(self):
  914.         return '{}/answers?id={}'.format(GDS_URL, self.hashed_id)
  915.  
  916.     def __str__(self):
  917.         return '{}'.format(self.question)
  918.  
  919.     class Meta:
  920.         managed = False
  921.         gds_database = 'gds'
  922.         db_table = 'questions'
  923.  
  924.  
  925. class QuestionsLookup(GdsModel):
  926.     question = models.ForeignKey(Questions, models.DO_NOTHING)
  927.     fk_table = models.CharField(max_length=200)
  928.     fk_id = models.IntegerField()
  929.     created_at = models.DateTimeField()
  930.     updated_at = models.DateTimeField(blank=True, null=True)
  931.  
  932.     class Meta:
  933.         managed = False
  934.         gds_database = 'gds'
  935.         db_table = 'questions_lookup'
  936.  
  937.  
  938. class TransferPoliciesAirport(GdsModel):
  939.     cp = models.ForeignKey(Cp, models.DO_NOTHING)
  940.     airport_code = models.CharField(max_length=15)
  941.     min_price = models.CharField(max_length=100)
  942.     status = models.CharField(max_length=3)
  943.     restriction = models.CharField(max_length=255)
  944.  
  945.     class Meta:
  946.         managed = False
  947.         gds_database = 'gds'
  948.         db_table = 'transfer_policies_airport'
  949.  
  950.  
  951. class TransferPoliciesCp(GdsModel):
  952.     cp = models.ForeignKey(Cp, models.DO_NOTHING)
  953.     min_price = models.CharField(max_length=100)
  954.     status = models.CharField(max_length=3)
  955.     restriction = models.CharField(max_length=255)
  956.  
  957.     class Meta:
  958.         managed = False
  959.         gds_database = 'gds'
  960.         db_table = 'transfer_policies_cp'
  961.  
  962.  
  963. class Userroles(GdsModel):
  964.     role = models.CharField(max_length=50)
  965.     role_safe = models.CharField(max_length=50)
  966.     created_at = models.DateTimeField(blank=True, null=True)
  967.     updated_at = models.DateTimeField(blank=True, null=True)
  968.  
  969.     class Meta:
  970.         managed = False
  971.         gds_database = 'gds'
  972.         db_table = 'userroles'
  973.  
  974.  
  975. class Users(GdsModel):
  976.     role = models.ForeignKey(Userroles, models.DO_NOTHING, blank=True, null=True)
  977.     first_name = models.CharField(max_length=50)
  978.     last_name = models.CharField(max_length=50)
  979.     email = models.CharField(unique=True, max_length=255)
  980.     username = models.CharField(unique=True, max_length=100)
  981.     password = models.CharField(max_length=60)
  982.     active = models.IntegerField()
  983.     remember_token = models.CharField(max_length=100, blank=True, null=True)
  984.     created_at = models.DateTimeField()
  985.     updated_at = models.DateTimeField(blank=True, null=True)
  986.  
  987.     class Meta:
  988.         managed = False
  989.         gds_database = 'gds'
  990.         db_table = 'users'
  991.  
  992.  
  993. def get_medias_for(fk_table, fk_id, media_filter_options={}):
  994.     media_lookups = MediaLookup.objects.filter(fk_table=fk_table, fk_id=fk_id)
  995.     media_ids = media_lookups.values_list('media', flat=True)
  996.     medias = Medias.objects.filter(id__in=media_ids, **media_filter_options)
  997.     return medias
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement