Ejemplo n.º 1
0
    def get(self, request, *args, **kwargs):
        user_id = isUserLoggedIn(request)

        if not user_id:
            return HttpResponseRedirect('/')

        try:
            user = User.objects.get(id=user_id)
        except:
            return HttpResponse('No user.')

        user_symptoms = []
        for symptom in Symptom.objects.all():
            try:
                UserSingleSymptomSeverity.objects.filter(
                    user_symptom_severities__user=user, symptom=symptom)

                user_symptoms.append(symptom)
            except ObjectDoesNotExist:
                pass

        symptoms_chart = self.getSymptomsScatterChart(user)
        factors_chart = self.getFactorsScatterChart(user)

        return render(
            request, self.template_name,
            dict(user=user,
                 user_symptoms=user_symptoms,
                 symptoms_chart=symptoms_chart,
                 factors_chart=factors_chart))
Ejemplo n.º 2
0
    def get(self, request, *args, **kwargs):
        user_id = isUserLoggedIn(request)

        if not user_id:
            return HttpResponseRedirect('/')

        user = User.objects.get(id=user_id)

        years = [r for r in range(1900, datetime.today().year + 1)]
        years.reverse()

        ethnicity_list = self.getEthnicityList()

        wrong_password = True if 'wrong_password' in kwargs else False
        password_changed = True if 'password_changed' in kwargs else False
        email_confirmation = True if 'email_confirmation' in kwargs else False
        profile_is_changed = True if 'profile_is_changed' in kwargs else False
        return render(
            request, self.template_name,
            dict(user=user,
                 years=years,
                 ethnicity_list=ethnicity_list,
                 profile_is_changed=profile_is_changed,
                 email_confirmation=email_confirmation,
                 password_changed=password_changed,
                 wrong_password=wrong_password,
                 GOOGLE_MAP_KEY=GOOGLE_MAP_KEY))
Ejemplo n.º 3
0
    def get(self, request, *args, **kwargs):
        if 'action' in request.GET and request.GET['action'] == 'logout':
            request.session.flush()
            return HttpResponseRedirect('/')

        userId = isUserLoggedIn(request)

        if userId:
            return HttpResponseRedirect('/user/%s' % userId)

        incorrect_login = True if 'incorrect_login' in kwargs else False

        return render(
            request, self.template_name,
            dict(symptoms=Symptom.objects.all(),
                 incorrect_login=incorrect_login))
Ejemplo n.º 4
0
    def get(self, request, *args, **kwargs):
        user_id = isUserLoggedIn(request)

        if not user_id:
            return HttpResponseRedirect('/')

        try:
            user = User.objects.get(id=user_id)
        except:
            return HttpResponse('No user.')

        symptom_id = kwargs['symptom_id'] if 'symptom_id' in kwargs else None
        factor_id = kwargs['factor_id'] if 'factor_id' in kwargs else None

        if symptom_id is None or factor_id is None:
            return HttpResponse('Provided Parameter is invalid.')

        symptom = Symptom.objects.get(id=symptom_id)
        factor = Factor.objects.get(id=factor_id)

        # init variables (scores, users) with symptom, factor
        self.initVariables(symptom, factor)

        statisctics_charts = self.getStatisticsChart(symptom, factor)

        user_timelines = user_timelines = [dict(
            user=dict(
                name=user.first_name + ' ' + user.last_name,
                sex='M' if user.gender == 'male' else 'F',
                age=datetime.today().year-user.birth_year
            ),
            chart=self.getTimelineChart(user, symptom, factor)
        ) for user in self.getUsers()]

        return render(request, self.template_name, dict(
            symptom=symptom,
            factor=factor,
            statisctics_charts=statisctics_charts,
            user_timelines=user_timelines
        ))
Ejemplo n.º 5
0
    def get(self, request, *args, **kwargs):
        user_id = isUserLoggedIn(request)

        if not user_id:
            return HttpResponseRedirect('/')

        try:
            user = User.objects.get(id=user_id)
        except:
            return HttpResponse('No user.')

        symptom_id = kwargs['symptom_id'] if 'symptom_id' in kwargs else None

        if symptom_id is None:
            return HttpResponse('Provided Parameter is invalid.')

        symptom = Symptom.objects.get(id=symptom_id)

        rows = []
        factors = []
        users = User.objects.all()
        for user in users:
            for factor in user.getFactorsBySymptom(symptom):
                if factor not in factors:
                    factors.append(factor)

        for factor in factors:
            score = self.getAvgSymptomScore(symptom, factor)
            rows.append(
                dict(factor=factor,
                     score=score,
                     face_class=self.getFaceClassName(score),
                     user_count=len(
                         self.getUsersByFactorAndSymptom(symptom, factor))))

        return render(request, self.template_name,
                      dict(symptom=symptom, rows=rows))
Ejemplo n.º 6
0
    def post(self, request, *args, **kwargs):
        user_id = isUserLoggedIn(request)

        if not user_id:
            return HttpResponseRedirect('/')

        try:
            user = User.objects.get(id=user_id)
        except ObjectDoesNotExist:
            return HttpResponse('No user.')

        params = request.POST

        if params['action'] == 'add':
            if 'selected_uss_id' in params:
                # update Update Symptom Severities
                uss = UserSymptomSeverities.objects.get(
                    id=params['selected_uss_id'])

                uss.created_at = datetime.strptime(
                    '%s %s' % (params['date'], params['time']), '%m/%d/%Y %H:%M:%S')
                uss.title = params['title']

                uss.save()

                usss_list = UserSingleSymptomSeverity.objects.filter(
                    user_symptom_severities=uss)
                for usss in usss_list:
                    usss.delete()
            else:
                # create User Symptom Severities
                uss = self.createUserSymptomSeverities(
                    user, params['date'], params['time'], params['title'])

            for symptom_id in params.getlist('symptom_IDs'):
                symptom = Symptom.objects.get(id=symptom_id)

                description_key = 'symptom_%s_description' % symptom_id
                description = params[description_key] if description_key in params else ''

                selected_level_key = 'symptom_%s_level' % symptom_id
                selected_level = params[selected_level_key] if selected_level_key in params else None

                self.createUserSingleSymptomSeverity(
                    uss, symptom, selected_level, description)

        elif params['action'] == 'delete':
            uss = UserSymptomSeverities.objects.get(id=params['uss_id'])
            uss.delete()

        if params['action'] == "add_cus":
            symptom_id = params['symptom_id']
            cus = CurrentUserSymptom(
                user=user, symptom=Symptom.objects.get(id=symptom_id))
            cus.save()

            return JsonResponse(dict(added=True, cus_id=cus.id))

        elif params['action'] == 'delete_cus':
            symptom_id = params['symptom_id']
            cus = CurrentUserSymptom.objects.get(
                user=user, symptom__id=symptom_id)
            cus.delete()

            return JsonResponse(dict(removed=True))

        elif params['action'] in ['edit', 'date_filter']:
            if 'uss_id' in params:
                kwargs['uss_id'] = params['uss_id']

            if 'date_filter' in params:
                kwargs['date_filter'] = params['date_filter']

        return self.get(request, *args, **kwargs)
Ejemplo n.º 7
0
    def get(self, request, *args, **kwargs):
        user_id = isUserLoggedIn(request)

        if not user_id:
            return HttpResponseRedirect('/')

        try:
            user = User.objects.get(id=user_id)
        except ObjectDoesNotExist:
            return HttpResponse('No user.')

        cus_list = CurrentUserSymptom.objects.filter(user=user)

        current_date = datetime.now().strftime('%m/%d/%Y')
        current_time = '{}:{}:{}'.format('%02d' % datetime.now().hour,
                                         '%02d' % datetime.now().minute,
                                         '%02d' % datetime.now().second)

        selected_uss = None
        symptoms = []
        if 'uss_id' in kwargs:
            selected_uss = UserSymptomSeverities.objects.get(
                id=kwargs['uss_id'])

            current_date = selected_uss.created_at.strftime('%m/%d/%Y')
            current_time = '{}:{}:{}'.format(
                '%02d' % selected_uss.created_at.hour,
                '%02d' % selected_uss.created_at.minute,
                '%02d' % selected_uss.created_at.second)

            for symptom in Symptom.objects.all():
                _symptom = dict(
                    id=symptom.id, name=symptom.name, disabled=False)
                try:
                    UserSingleSymptomSeverity.objects.get(
                        user_symptom_severities=selected_uss, symptom=symptom)

                    _symptom['disabled'] = True
                except ObjectDoesNotExist:
                    pass

                symptoms.append(_symptom)

            selected_uss = dict(
                id=selected_uss.id,
                title=selected_uss.title,
                date=selected_uss.created_at.strftime('%m/%d/%Y'),
                usss_list=[dict(
                    id=usss.id,
                    symptom=usss.getSymptom(),
                    level=usss.getLevel(),
                    description=usss.description
                ) for usss in selected_uss.getUSSSList()]
            )
        else:
            for symptom in Symptom.objects.all():
                _symptom = dict(
                    id=symptom.id, name=symptom.name, disabled=False)
                try:
                    CurrentUserSymptom.objects.get(user=user, symptom=symptom)

                    _symptom['disabled'] = True
                except ObjectDoesNotExist:
                    pass

                symptoms.append(_symptom)

        date_filter = None
        if 'date_filter' in kwargs:
            date_filter = kwargs['date_filter']

        org_symptoms = [dict(
            id=symptom.id,
            name=symptom.name,
            levels=symptom.getSymptomLevels()
        ) for symptom in Symptom.objects.all()]

        try:
            start_timestamp = datetime.strptime(
                '%s 00:00:00' % date_filter, '%m/%d/%Y %H:%M:%S')
            end_timestamp = datetime.strptime(
                '%s 23:59:59' % date_filter, '%m/%d/%Y %H:%M:%S')

            uss_list = [dict(
                id=uss.id,
                date=uss.created_at.strftime('%m/%d/%Y'),
                time=uss.created_at.strftime('%H:%M:%S'),
                title=uss.title
            ) for uss in UserSymptomSeverities.objects.filter(user=user, created_at__range=(start_timestamp, end_timestamp)).order_by('-created_at')]
        except:
            uss_list = [dict(
                id=uss.id,
                date=uss.created_at.strftime('%m/%d/%Y'),
                time=uss.created_at.strftime('%H:%M:%S'),
                title=uss.title
            ) for uss in UserSymptomSeverities.objects.filter(user=user).order_by('-created_at')]

        default_title = datetime.now().strftime('%m/%d Update')

        return render(request, self.template_name, dict(
            user_id=user_id,
            default_title=default_title,
            selected_uss=selected_uss,
            cus_list=cus_list,
            uss_list=uss_list,
            symptoms=symptoms,
            date_filter=date_filter,
            org_symptoms=org_symptoms,
            current_date=current_date,
            current_time=current_time
        ))
Ejemplo n.º 8
0
    def get(self, request, *args, **kwargs):
        user_id = isUserLoggedIn(request)

        if not user_id:
            return HttpResponseRedirect('/')

        try:
            user = User.objects.get(id=user_id)
        except ObjectDoesNotExist:
            return HttpResponse('No user.')

        params = request.GET
        if 'action' in params:
            if params['action'] in ['convert-to-daily']:
                factor_id = params['factor_id']

                try:
                    cif = CurrentIntermittentFactor.objects.get(
                        user=user, factor__id=factor_id)
                    cif.delete()

                    if params['action'] == 'convert-to-daily':
                        factor_id = params['factor_id']

                        started_at = '%s %s' % (params['date'], params['time'])
                        started_at = datetime.strptime(started_at,
                                                       '%m/%d/%Y %H:%M:%S')

                        udfs = UserDailyFactorStart(
                            user=user,
                            factor=Factor.objects.get(id=factor_id),
                            created_at=started_at)

                        udfs.save()
                except:
                    pass

            elif params['action'] in ['convert-to-intermittent']:
                udfs_id = params['udfs_id']

                started_at = '%s %s' % (params['date'], params['time'])
                started_at = datetime.strptime(started_at, '%m/%d/%Y %H:%M:%S')

                try:
                    udfs = UserDailyFactorStart.objects.get(id=udfs_id)

                    udfe = UserDailyFactorEnd(user_daily_factor_start=udfs,
                                              created_at=started_at)

                    udfe.save()

                    if params['action'] == 'convert-to-intermittent':
                        cif = CurrentIntermittentFactor(
                            user=user, factor=udfs.getFactor())
                        cif.save()
                except:
                    pass

            return HttpResponseRedirect('/user/%s/update_factors' % user_id)

        current_date = datetime.now().strftime('%m/%d/%Y')
        current_time = '{}:{}:{}'.format('%02d' % datetime.now().hour,
                                         '%02d' % datetime.now().minute,
                                         '%02d' % datetime.now().second)

        cif_list = CurrentIntermittentFactor.objects.filter(user=user)

        [udfs_list, current_title, current_date,
         current_time] = self.gefCurrentValues(user)

        selected_uf = None
        factors = []
        if 'uf_id' in kwargs:
            selected_uf = UserFactors.objects.get(id=kwargs['uf_id'])

            current_date = selected_uf.created_at.strftime('%m/%d/%Y')
            current_time = '{}:{}:{}'.format(
                '%02d' % selected_uf.created_at.hour,
                '%02d' % selected_uf.created_at.minute,
                '%02d' % selected_uf.created_at.second)

            for factor in Factor.objects.all():
                _factor = dict(id=factor.id,
                               title=factor.title,
                               disabled=False)
                try:
                    UserIntermittentFactor.objects.get(
                        user_factors=selected_uf, factor=factor)

                    _factor['disabled'] = True
                except ObjectDoesNotExist:
                    pass

                factors.append(_factor)

            selected_uf = dict(id=selected_uf.id,
                               title=selected_uf.title,
                               uif_list=[
                                   dict(id=uif.id,
                                        factor=uif.getFactor(),
                                        level=uif.getLevel(),
                                        description=uif.description)
                                   for uif in selected_uf.getUIFList()
                               ],
                               udfs_list=[
                                   dict(id=udfs.id,
                                        factor=udfs.getFactor(),
                                        level=udfs.getLevel(
                                            selected_uf.created_at),
                                        description=udfs.getDescription())
                                   for udfs in selected_uf.getUDFSList()
                               ])
        else:
            for factor in Factor.objects.all():
                _factor = dict(id=factor.id,
                               title=factor.title,
                               disabled=False)

                try:
                    CurrentIntermittentFactor.objects.get(user=user,
                                                          factor=factor)

                    _factor['disabled'] = True
                except ObjectDoesNotExist:
                    pass

                if factor.id in [udfs['factor']['id'] for udfs in udfs_list]:
                    _factor['disabled'] = True

                factors.append(_factor)

        org_factors = [
            dict(id=factor.id,
                 title=factor.title,
                 levels=factor.getFactorLevels())
            for factor in Factor.objects.all()
        ]

        date_filter = None
        if 'date_filter' in kwargs:
            date_filter = kwargs['date_filter']

        uf_list = self.getUFList(user, date_filter)

        return render(
            request, self.template_name,
            dict(user_id=user_id,
                 cif_list=cif_list,
                 udfs_list=udfs_list,
                 factors=factors,
                 selected_uf=selected_uf,
                 uf_list=uf_list,
                 org_factors=org_factors,
                 date_filter=date_filter,
                 current_title=current_title,
                 current_date=current_date,
                 current_time=current_time))
Ejemplo n.º 9
0
    def post(self, request, *args, **kwargs):
        user_id = isUserLoggedIn(request)

        if not user_id:
            return HttpResponseRedirect('/')

        try:
            user = User.objects.get(id=user_id)
        except ObjectDoesNotExist:
            return HttpResponse('No user.')

        params = request.POST

        if params['action'] == 'get_daily_factors':
            [udfs_list, current_title, current_date,
             current_time] = self.gefCurrentValues(user, params)

            return JsonResponse(
                dict(udfs_list=udfs_list,
                     current_title=current_title,
                     current_date=current_date,
                     current_time=current_time))

        elif params['action'] == 'add':
            if 'selected_uf_id' in params:
                # update Update Factors
                uf = UserFactors.objects.get(id=params['selected_uf_id'])

                # uf.created_at = datetime.strptime('%s %s' % (params['date'], params['time']), '%m/%d/%Y %H:%M:%S')
                uf.title = params['title']

                uf.save()

                uif_list = UserIntermittentFactor.objects.filter(
                    user_factors=uf)
                for uif in uif_list:
                    uif.delete()
            else:
                uf = self.createUserFactors(user, params['date'],
                                            params['time'], params['title'])

            for factor_id in params.getlist('factor_Intermittent_IDs'):
                factor = Factor.objects.get(id=factor_id)

                description_key = 'factor_%s_description' % factor_id
                description = params[
                    description_key] if description_key in params else ''

                selected_level_key = 'factor_%s_level' % factor_id
                selected_level = params[
                    selected_level_key] if selected_level_key in params else None

                # create user intermittent factor
                self.createUserIntermittentFactor(uf, factor, selected_level,
                                                  description)

            for udfs_id in params.getlist('udfs_IDs'):
                udfs = UserDailyFactorStart.objects.get(id=udfs_id)

                title = params['title']

                description_key = 'udfs_%s_description' % udfs_id
                description = params[
                    description_key] if description_key in params else ''

                selected_level_key = 'udfs_%s_level' % udfs_id
                selected_level = params[
                    selected_level_key] if selected_level_key in params else None

                # create user daily factor meta
                self.updateUserDailyFactorMeta(udfs, selected_level, title,
                                               description, uf.created_at,
                                               'selected_uf_id' in params)

        elif params['action'] == 'delete':
            uf = UserFactors.objects.get(id=params['uf_id'])
            uf.delete()

        elif params['action'] == 'add_cif':
            factor_id = params['factor_id']

            cif = CurrentIntermittentFactor(
                user=user, factor=Factor.objects.get(id=factor_id))
            cif.save()

            return JsonResponse(dict(added=True, cif_id=cif.id))

        elif params['action'] == 'delete_cif':
            factor_id = params['factor_id']

            cif = CurrentIntermittentFactor.objects.get(user=user,
                                                        factor__id=factor_id)
            cif.delete()

            return JsonResponse(dict(removed=True))

        elif params['action'] == 'add_udfs':
            factor_id = params['factor_id']
            factor = Factor.objects.get(id=factor_id)

            started_at = '%s %s' % (params['date'], params['time'])
            started_at = datetime.strptime(started_at, '%m/%d/%Y %H:%M:%S')

            udfs = UserDailyFactorStart(user=user,
                                        factor=factor,
                                        created_at=started_at)

            udfs.save()

            return JsonResponse(
                dict(added=True,
                     udfs=dict(id=udfs.id,
                               factor=dict(id=udfs.factor.id,
                                           title=udfs.getFactorTitle(),
                                           levels=udfs.getFactorLevels()),
                               disabled=False)))

        elif params['action'] == 'add_udfe':
            udfs_id = params['udfs_id']

            started_at = '%s %s' % (params['date'], params['time'])
            started_at = datetime.strptime(started_at, '%m/%d/%Y %H:%M:%S')

            udfs = UserDailyFactorStart.objects.get(id=udfs_id)

            udfe = UserDailyFactorEnd(user_daily_factor_start=udfs,
                                      created_at=started_at)

            udfe.save()

            return JsonResponse(dict(removed=True))

        elif params['action'] == 'edit_uf':
            kwargs['uf_id'] = params['uf_id']

            if 'date_filter' in params:
                kwargs['date_filter'] = params['date_filter']

        elif params['action'] == 'get_uf_list':
            return JsonResponse(
                dict(uf_list=self.getUFList(user, params['date_filter'])))

        return self.get(request, *args, **kwargs)
Ejemplo n.º 10
0
    def post(self, request, *args, **kwargs):
        params = request.POST
        user_id = isUserLoggedIn(request)

        if not user_id:
            return HttpResponseRedirect('/')

        user = User.objects.get(id=user_id)

        if params['action'] == 'change_password':
            if user.password == params['old_password']:
                user.password = params['new_password']
                kwargs['password_changed'] = True
            else:
                kwargs['wrong_password'] = True

            token = gernerateUserToken(user)

            request.session['wrw_token'] = token

            user.save()

        elif params['action'] == 'change_email':
            confirm_token = str(uuid4())

            user.email_to_confirm = params['new_email']
            user.confirm_token = confirm_token

            href = 'http://127.0.0.1:8000/register/verify-token/%s' % confirm_token

            message = Mail(
                from_email='*****@*****.**',
                to_emails=params['new_email'],
                subject='Email verification',
                html_content=
                '<a href="%s" target="_blank">Click here</a> or visit this URL on the browser: %s'
                % (href, href))

            sg = SendGridAPIClient(SENDGRID_KEY)
            sg.send(message)

            kwargs['email_confirmation'] = True

            user.save()

        elif params['action'] == 'change_profile':
            user.birth_year = params['birth_year']
            user.ethnicity_top = params['ethnicity_top']
            user.ethnicity_second = params['ethnicity_second']
            user.ethnicity_third = params['ethnicity_third']
            user.gender = params['gender']
            user.sexual_orientation = params['sexual_orientation']
            user.city = params['city']
            user.state = params['state']
            user.country = params['country']

            kwargs['profile_is_changed'] = True

            user.save()

        return self.get(request, *args, **kwargs)