2015-09-30 13:33:05 -07:00
|
|
|
from annoying.functions import get_object_or_None
|
2014-05-04 19:47:15 -07:00
|
|
|
from datetime import datetime
|
2015-07-24 18:39:25 -07:00
|
|
|
from django.contrib.auth.models import User
|
2012-06-26 08:33:29 -07:00
|
|
|
from django.db import models
|
2015-09-30 13:33:05 -07:00
|
|
|
from itertools import chain
|
2013-04-04 09:39:20 -07:00
|
|
|
from snipts.models import Snipt
|
2015-09-30 13:33:05 -07:00
|
|
|
from teams.models import Team
|
2012-06-26 08:33:29 -07:00
|
|
|
|
2015-07-24 18:28:31 -07:00
|
|
|
|
2012-07-18 21:39:43 -07:00
|
|
|
class UserProfile(models.Model):
|
2013-01-15 21:09:54 -08:00
|
|
|
|
2019-01-23 15:52:55 -08:00
|
|
|
LIST_VIEW_CHOICES = (("N", "Normal"), ("C", "Compact"))
|
2013-10-18 21:41:00 -07:00
|
|
|
|
2019-01-23 15:52:55 -08:00
|
|
|
EDITOR_CHOICES = (("C", "CodeMirror"), ("T", "Textarea"))
|
2013-02-04 11:40:45 -08:00
|
|
|
|
2019-01-23 15:52:55 -08:00
|
|
|
THEME_CHOICES = (("D", "Default"), ("A", "Pro Adams"))
|
2013-01-15 21:09:54 -08:00
|
|
|
|
2013-02-05 12:24:51 -08:00
|
|
|
EDITOR_THEME_CHOICES = (
|
2019-01-23 15:52:55 -08:00
|
|
|
("default", "Default"),
|
|
|
|
("ambiance", "Ambiance"),
|
|
|
|
("blackboard", "Blackboard"),
|
|
|
|
("cobalt", "Cobalt"),
|
|
|
|
("eclipse", "Eclipse"),
|
|
|
|
("elegant", "Elegant"),
|
|
|
|
("erlang-dark", "Erlang Dark"),
|
|
|
|
("lesser-dark", "Lesser Dark"),
|
|
|
|
("monokai", "Monokai"),
|
|
|
|
("neat", "Neat"),
|
|
|
|
("night", "Night"),
|
|
|
|
("rubyblue", "Ruby Blue"),
|
|
|
|
("solarized dark", "Solarized Dark"),
|
|
|
|
("solarized light", "Solarized Light"),
|
|
|
|
("twilight", "Twilight"),
|
|
|
|
("vibrant-ink", "Vibrant Ink"),
|
|
|
|
("xq-dark", "XQ Dark"),
|
2013-02-05 12:24:51 -08:00
|
|
|
)
|
|
|
|
|
2013-02-08 21:06:22 -08:00
|
|
|
# User
|
2019-03-11 13:45:49 -07:00
|
|
|
user = models.OneToOneField(User, on_delete=models.CASCADE)
|
2015-07-24 18:28:31 -07:00
|
|
|
is_pro = models.BooleanField(default=False)
|
2015-07-22 11:01:27 -07:00
|
|
|
teams_beta_seen = models.BooleanField(default=False)
|
|
|
|
teams_beta_applied = models.BooleanField(default=False)
|
2015-07-24 18:28:31 -07:00
|
|
|
pro_date = models.DateTimeField(blank=True, null=True)
|
|
|
|
stripe_id = models.CharField(max_length=100, null=True, blank=True)
|
2019-01-23 15:52:55 -08:00
|
|
|
list_view = models.CharField(
|
|
|
|
max_length=1, null=False, blank=False, default="N", choices=LIST_VIEW_CHOICES
|
|
|
|
)
|
2013-01-13 09:51:25 -08:00
|
|
|
|
2013-02-08 21:06:22 -08:00
|
|
|
# Blog
|
2015-07-24 18:28:31 -07:00
|
|
|
blog_title = models.CharField(max_length=250, null=True, blank=True)
|
2019-01-23 15:52:55 -08:00
|
|
|
blog_theme = models.CharField(
|
|
|
|
max_length=1, null=False, blank=False, default="A", choices=THEME_CHOICES
|
|
|
|
)
|
2015-07-24 18:28:31 -07:00
|
|
|
blog_domain = models.CharField(max_length=250, null=True, blank=True)
|
2013-02-08 21:06:22 -08:00
|
|
|
|
|
|
|
# Editor
|
2019-01-23 15:52:55 -08:00
|
|
|
default_editor = models.CharField(
|
|
|
|
max_length=250, null=False, blank=False, default="C", choices=EDITOR_CHOICES
|
|
|
|
)
|
|
|
|
editor_theme = models.CharField(
|
|
|
|
max_length=250,
|
|
|
|
null=False,
|
|
|
|
blank=False,
|
|
|
|
default="default",
|
|
|
|
choices=EDITOR_THEME_CHOICES,
|
|
|
|
)
|
2012-07-08 20:21:36 -07:00
|
|
|
|
2013-04-04 09:39:20 -07:00
|
|
|
def get_blog_posts(self):
|
2019-01-23 15:52:55 -08:00
|
|
|
return Snipt.objects.filter(user=self.user, blog_post=True, public=True)
|
2013-04-04 09:39:20 -07:00
|
|
|
|
2013-02-11 22:32:44 -08:00
|
|
|
def get_primary_blog_domain(self):
|
|
|
|
if not self.blog_domain:
|
|
|
|
return None
|
|
|
|
else:
|
2019-01-23 15:52:55 -08:00
|
|
|
return self.blog_domain.split(" ")[0]
|
2013-02-11 22:32:44 -08:00
|
|
|
|
2013-04-05 07:22:14 -07:00
|
|
|
def get_user_profile_url(self):
|
|
|
|
|
|
|
|
# If the user has a blog domain, use that.
|
|
|
|
if self.blog_domain:
|
2019-01-23 15:52:55 -08:00
|
|
|
url = "http://{}".format(self.get_primary_blog_domain())
|
2013-04-05 07:22:14 -07:00
|
|
|
|
|
|
|
# Otherwise, if they have blog posts, use their Snipt blog URL.
|
|
|
|
elif self.get_blog_posts():
|
2019-04-24 06:09:50 -07:00
|
|
|
url = 'https://{}.snipt.net/'.format(self.user.username)
|
2013-04-05 07:22:14 -07:00
|
|
|
|
|
|
|
# Otherwise, use their regular Snipt profile page.
|
|
|
|
else:
|
2019-04-24 06:09:50 -07:00
|
|
|
url = 'https://snipt.net/{}/'.format(self.user.username)
|
2013-04-05 07:22:14 -07:00
|
|
|
|
|
|
|
return url
|
|
|
|
|
2013-04-15 12:01:04 -07:00
|
|
|
def has_public_snipts(self):
|
2019-01-23 15:52:55 -08:00
|
|
|
return (
|
|
|
|
True if Snipt.objects.filter(user=self, public=True).count() > 0 else False
|
|
|
|
)
|
2013-02-11 22:32:44 -08:00
|
|
|
|
2015-10-18 08:52:44 -07:00
|
|
|
@property
|
2015-09-30 14:05:54 -07:00
|
|
|
def is_a_team(self):
|
2015-10-18 18:07:01 -07:00
|
|
|
if get_object_or_None(Team, user=self.user, disabled=False):
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
2015-09-30 13:33:05 -07:00
|
|
|
|
|
|
|
def teams(self):
|
2015-10-18 18:07:01 -07:00
|
|
|
teams_owned = Team.objects.filter(owner=self.user, disabled=False)
|
|
|
|
teams_in = Team.objects.filter(members=self.user, disabled=False)
|
2015-09-30 13:33:05 -07:00
|
|
|
return list(chain(teams_owned, teams_in))
|
|
|
|
|
2015-10-18 18:07:01 -07:00
|
|
|
@property
|
|
|
|
def has_teams(self):
|
2019-01-23 15:52:55 -08:00
|
|
|
if len(self.teams()) > 0:
|
2015-10-18 18:07:01 -07:00
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
2014-05-04 19:47:15 -07:00
|
|
|
def get_account_age(self):
|
2019-01-23 15:52:55 -08:00
|
|
|
delta = datetime.now().replace(tzinfo=None) - self.user.date_joined.replace(
|
|
|
|
tzinfo=None
|
|
|
|
)
|
2014-05-04 19:47:15 -07:00
|
|
|
return delta.days
|
|
|
|
|
2019-01-23 15:52:55 -08:00
|
|
|
|
2012-06-26 08:33:29 -07:00
|
|
|
User.profile = property(lambda u: UserProfile.objects.get_or_create(user=u)[0])
|