from django.db import models from django.urls import reverse from django.utils.html import format_html, format_html_join from django.utils import timezone import settings from products.models import ProductLink class PostType(models.IntegerChoices): REVIEW = 0, ('review') GUIDE = 1, ('guide') HOWTO = 2, ('how-to') class TemplateType(models.IntegerChoices): STORY = 0, ('story') GALLERY = 1, ('gallery') class PostStatus(models.IntegerChoices): ASSIGNED = 0, ('Assigned') TURNEDIN = 1, ('turned in') PUBLISHED = 2, ('published') class Post(models.Model): # an entry in a feed user = models.ForeignKey(settings.AUTH_USER_MODEL, null=True, on_delete=models.SET_NULL) title = models.CharField(max_length=512, blank=True, null=True) body = models.TextField(blank=True, null=True) url = models.CharField(max_length=512, blank=True, null=True) edit_url = models.CharField(max_length=512, blank=True, null=True) date_last_pub = models.DateField() guid = models.CharField(max_length=512, blank=True, null=True, db_index=True) author = models.CharField(max_length=255, blank=True, null=True) post_type = models.IntegerField(choices=PostType.choices, default=PostType.GUIDE) template_type = models.IntegerField(choices=TemplateType.choices, default=TemplateType.STORY) update_frequency = models.BigIntegerField(help_text="In days") products = models.ManyToManyField(ProductLink, blank=True, null=True) needs_update = models.BooleanField(default=False) is_live = models.BooleanField(default=True) post_status = models.IntegerField(choices=PostStatus.choices, default=PostStatus.PUBLISHED) class Meta: ordering = ('date_last_pub',) def __str__(self): return self.title def time_since_update(self): td = timezone.localdate() - self.date_last_pub return int(td.days) #def get_needs_update(self): # if self.time_since_update() > self.update_frequency: # return True # else: # return False def days_overdue(self): if self.needs_update == True: return self.time_since_update() - self.update_frequency else: return 0 def admin_url(self): return format_html('%s' % (self.url, self.url)) admin_link.short_description = 'Link' def save(self, *args, **kwargs): td = timezone.localdate() - self.date_last_pub if td.days > self.update_frequency and self.post_status != 1: self.needs_update = True else: self.needs_update = False super(Post, self).save() #URL,This Article,Type,Lead,Previous Leads,Other Testers,Notes/Docs,Last Pub Date,Update Next,Months Since Update,Update Frequency (Months),Updates per year,Prev. Updates,"18 Mo Traffic Trend ''' row[0] #url row[1] #title row[2] #post_type row[3] #author row[7] #date_last_pub row[10] #update_frequency with open(path) as f: reader = csv.reader(f) count = 0 for row in reader: if count > 1: if row[2] == "Deals": # don't care about deals posts continue elif row[2] == "Buying Guide": gtype = PostType.GUIDE else: gtype = PostType.HOWTO if row[10] == "Retired": continue else: print(int(row[10])) print(gtype) d = datetime.strptime(row[7], '%m/%d/%Y') post, created = Post.objects.get_or_create( title = str(row[1]).strip(), url = str(row[0]).strip(), date_last_pub = d, author = str(row[3]).strip(), post_type = gtype, update_frequency = int(row[10]*30), ) user = User.objects.get(username="luxagraf") user for post in Post.objects.all(): if post.author == "Scott Gilbertson": post.user = user post.save() '''