<gh_stars>0
__author__ = 'stanley'
from google.appengine.ext import ndb
from education import Education
from work import Work
from reference import Reference
from location import Location
from term import Term
from project import Project
from honor import Honor
from image import Image
from language import Language
from google.appengine.api import images


class User(ndb.Model):
    id = ndb.StringProperty()
    given_name = ndb.StringProperty()
    surname = ndb.StringProperty()
    gender = ndb.StringProperty()
    eds = ndb.StructuredProperty(Education, repeated=True)
    works = ndb.StructuredProperty(Work, repeated=True)
    followers = ndb.StringProperty(repeated=True)  # emails
    skills = ndb.StringProperty(repeated=True)
    cover_pic = ndb.StructuredProperty(Image)
    profile_pic = ndb.StructuredProperty(Image)
    total_num_of_elems = ndb.IntegerProperty()
    reference = ndb.StructuredProperty(Reference)
    term = ndb.StructuredProperty(Term)
    projects = ndb.StructuredProperty(Project, repeated=True)
    honors = ndb.StructuredProperty(Honor, repeated=True)
    languages = ndb.StructuredProperty(Language, repeated=True)
    views = ndb.IntegerProperty()
    location = ndb.StructuredProperty(Location)
    date = ndb.DateTimeProperty(auto_now_add=True)
    # notif + msg

    def append_edu(self, edu):
        self.eds.insert(0, edu)

    def append_work(self, work):
        self.works.insert(0, work)

    def append_project(self, project):
        self.projects.insert(0, project)

    def append_honor(self, honor):
        self.honors.insert(0, honor)

    def append_language(self, lang):
        self.languages.insert(0, lang)

    def append_follower(self, followees_email):
        self.followers.append(followees_email)

    def remove_follower(self, followees_email):
        if followees_email not in self.followers:
            raise Exception
        self.followers.remove(followees_email)

    def get_cover_url(self):
        if self.cover_pic is None:
            return "assets/images/home/default_cover.jpg"
        return images.get_serving_url(self.cover_pic.img) + '=s0'

    def get_profile_url(self):
        if self.profile_pic is None:
            if self.gender == 'Male':
                return "assets/images/male.png"
            elif self.gender == 'Female':
                return "assets/images/female.png"
            else:
                return "assets/images/no_cover.png"

        return images.get_serving_url(self.profile_pic.img) + '=s0'

    def get_profile_url_mirrored(self):
        if self.profile_pic is None:
            if self.gender == 'Male':
                return "assets/images/male_mir.png"
            elif self.gender == 'Female':
                return "assets/images/female_mir.png"
            else:
                return "assets/images/no_cover.png"

        return images.get_serving_url(self.profile_pic.img) + '=s0'

    def get_views_num_for_print(self):
        if self.views == 0:
            return 'No'
        elif 0 < self.views < 1000:
            return str(self.views)
        elif 999 < self.views < 1000000:
            return str(int(self.views) / 1000) + ',' + str(int(self.views) % 1000)
        else:
            return '+1,000,000'

    def get_followers_num_for_print(self):
        if len(self.followers) == 0:
            return 'No Followers'
        elif len(self.followers) == 1:
            return '1 Follower'
        elif 1 < len(self.followers) < 1000:
            return str(len(self.followers)) + ' Followers'
        elif 999 < len(self.followers) < 1000000:
            return str(len(self.followers) / 1000) + ',' + str(len(self.followers) % 1000) + ' Followers'
        else:
            return '+1,000,000 Followers'

    def get_highest_edu(self):
        if len(self.eds) == 0:
            return None
        highest_edu = self.eds[0]
        i = 1
        while i < len(self.eds):
            if self.eds[i] > highest_edu:
                highest_edu = self.eds[i]
            i += 1
        return highest_edu

    def get_highest_degree(self):
        ed = self.get_highest_edu()
        if ed is None:
            return None
        return ed.degree

    def get_location(self):
        if self.location is None:
            return "Unknown Location"
        return self.location.city + ', ' + self.location.region + ', ' + self.location.country

    def has_project(self):
        return len(self.projects) > 0

    def get_prop(self):
        if self.gender == 'Male':
            return 'his'
        elif self.gender == 'Female':
            return 'her'
        else:
            return 'the user\'s'

def user_key(_id):
    return ndb.Key('User', _id)
__author__ = 'stanley'
from google.appengine.ext import ndb
from education import Education
from work import Work
from reference import Reference
from location import Location
from term import Term
from project import Project
from honor import Honor
from image import Image
from language import Language
from google.appengine.api import images


class User(ndb.Model):
    id = ndb.StringProperty()
    given_name = ndb.StringProperty()
    surname = ndb.StringProperty()
    gender = ndb.StringProperty()
    eds = ndb.StructuredProperty(Education, repeated=True)
    works = ndb.StructuredProperty(Work, repeated=True)
    followers = ndb.StringProperty(repeated=True)  # emails
    skills = ndb.StringProperty(repeated=True)
    cover_pic = ndb.StructuredProperty(Image)
    profile_pic = ndb.StructuredProperty(Image)
    total_num_of_elems = ndb.IntegerProperty()
    reference = ndb.StructuredProperty(Reference)
    term = ndb.StructuredProperty(Term)
    projects = ndb.StructuredProperty(Project, repeated=True)
    honors = ndb.StructuredProperty(Honor, repeated=True)
    languages = ndb.StructuredProperty(Language, repeated=True)
    views = ndb.IntegerProperty()
    location = ndb.StructuredProperty(Location)
    date = ndb.DateTimeProperty(auto_now_add=True)
    # notif + msg

    def append_edu(self, edu):
        self.eds.insert(0, edu)

    def append_work(self, work):
        self.works.insert(0, work)

    def append_project(self, project):
        self.projects.insert(0, project)

    def append_honor(self, honor):
        self.honors.insert(0, honor)

    def append_language(self, lang):
        self.languages.insert(0, lang)

    def append_follower(self, followees_email):
        self.followers.append(followees_email)

    def remove_follower(self, followees_email):
        if followees_email not in self.followers:
            raise Exception
        self.followers.remove(followees_email)

    def get_cover_url(self):
        if self.cover_pic is None:
            return "assets/images/home/default_cover.jpg"
        return images.get_serving_url(self.cover_pic.img) + '=s0'

    def get_profile_url(self):
        if self.profile_pic is None:
            if self.gender == 'Male':
                return "assets/images/male.png"
            elif self.gender == 'Female':
                return "assets/images/female.png"
            else:
                return "assets/images/no_cover.png"

        return images.get_serving_url(self.profile_pic.img) + '=s0'

    def get_profile_url_mirrored(self):
        if self.profile_pic is None:
            if self.gender == 'Male':
                return "assets/images/male_mir.png"
            elif self.gender == 'Female':
                return "assets/images/female_mir.png"
            else:
                return "assets/images/no_cover.png"

        return images.get_serving_url(self.profile_pic.img) + '=s0'

    def get_views_num_for_print(self):
        if self.views == 0:
            return 'No'
        elif 0 < self.views < 1000:
            return str(self.views)
        elif 999 < self.views < 1000000:
            return str(int(self.views) / 1000) + ',' + str(int(self.views) % 1000)
        else:
            return '+1,000,000'

    def get_followers_num_for_print(self):
        if len(self.followers) == 0:
            return 'No Followers'
        elif len(self.followers) == 1:
            return '1 Follower'
        elif 1 < len(self.followers) < 1000:
            return str(len(self.followers)) + ' Followers'
        elif 999 < len(self.followers) < 1000000:
            return str(len(self.followers) / 1000) + ',' + str(len(self.followers) % 1000) + ' Followers'
        else:
            return '+1,000,000 Followers'

    def get_highest_edu(self):
        if len(self.eds) == 0:
            return None
        highest_edu = self.eds[0]
        i = 1
        while i < len(self.eds):
            if self.eds[i] > highest_edu:
                highest_edu = self.eds[i]
            i += 1
        return highest_edu

    def get_highest_degree(self):
        ed = self.get_highest_edu()
        if ed is None:
            return None
        return ed.degree

    def get_location(self):
        if self.location is None:
            return "Unknown Location"
        return self.location.city + ', ' + self.location.region + ', ' + self.location.country

    def has_project(self):
        return len(self.projects) > 0

    def get_prop(self):
        if self.gender == 'Male':
            return 'his'
        elif self.gender == 'Female':
            return 'her'
        else:
            return 'the user\'s'

def user_key(_id):
    return ndb.Key('User', _id)
# emails # notif + msg