Esempio n. 1
0
 def hamraz(self, request):
     """
       last app from hamraz
     :param request:
     :return: application address
     """
     try:
         cache_key = 'apk_release_hamraz'
         data = cache.get(cache_key)
         if data:
             # logger_api.info("apk_release_hamraz  memcache", extra={})
             return Response(data)
     except Exception as e:
         cache_key = 'apk_release_hamraz'
         logger_api.error("apk_release_hamraz  get cache  Error",
                          extra={"detail": {
                              "error": e,
                          }})
     queryset = self.queryset.filter(type="hamraz").order_by('-created_at')
     if queryset:
         queryset = queryset[0]
     serializer = self.get_serializer(queryset, many=False)
     # logger_api.error("apk_release_hamraz calculate", extra={})
     cache.set(cache_key, serializer.data,
               CACHE_TIME_APK_RELEASE)  # ست کردن کش
     return Response(serializer.data)
Esempio n. 2
0
def calculate_notification_count_set_cache(user_id):
    try:
        cache_key = 'notification_count_{0}'.format(user_id)
        count = Notification.objects.filter(send_to_id=user_id,
                                            seen=False).count()
        data = {"count": count}
        # logger_api.error("notification_count  calculate", extra={"detail": {"user": user_id, }})
        cache.set(cache_key, data, CACHE_TIME_NOTIF_COUNT)
    except Exception as ex:
        logger_api.error("calculate_notification_count_set_cache  Error",
                         extra={"detail": {
                             "error": ex,
                         }})
Esempio n. 3
0
    def calculate_announcement_count(self, request):
        """ تابع محاسبه تعداد اطلاعیه های خوانده نشده یک کاربر"""
        # kahrizi
        count = 0
        try:
            cache_key = 'announcement_count_{0}'.format(request.user.id)
            announcement_view = AnnouncementSeenHistory.objects.filter(
                user_id=request.user.id).count()

            user_type = get_user_type(request.user)

            if user_type == 'student':
                announcement_not_view = get_queryset_for_student(
                    request_data=request).count()

            elif user_type == 'teacher':
                announcement_not_view = get_queryset_for_teacher(
                    request_data=request)
                announcement_ids = set(
                    list(
                        announcement_not_view.values_list("announcement_id",
                                                          flat=True)))
                announcement_not_view = self.get_queryset().filter(
                    id__in=announcement_ids).count()
            else:
                count = 0
                return count

            count = announcement_not_view - announcement_view

            if count < 0:
                count = 0
                # logger_api.error("AnnouncementCount  calculate_announcement_count Error", extra={
                #     "detail": {
                #         "error": "AnnouncementCount =" + str(count) + "User="******"AnnouncementCount  calculate_announcement_count Error",
                extra={"detail": {
                    "error": e,
                }})

        finally:
            data = {"count": count, "date_calculate": datetime.datetime.now()}
            cache.set(cache_key, data, CACHE_TIME_ANN_COUNT)
            return count
Esempio n. 4
0
def send_sms(activation_code, user, user_name):
    try:
        resp = BagheriApi().send_message(user_name, activation_code)
        if resp and resp.status_code != 200:
            logger_api.error(
                "bagheri send message not send response 200",
                extra={
                    "message": resp,
                    "user error": user,
                })
    except:
        logger_api.error(
            "send_message bagheri api has error",
            extra={
                "user error": user,
            })
Esempio n. 5
0
 def list(self, request, *args, **kwargs):
     try:
         cache_key = 'announcement_count_{0}'.format(request.user.id)
         data = cache.get(cache_key)
         if data:
             if AnnouncementViewSet.near_next_announcement_date and datetime.datetime.now(
             ) >= AnnouncementViewSet.near_next_announcement_date:
                 """ اگر زمان انتشار نزدیک ترین اطلاعیه ای که در آینده باید  منتشر شده بود رسیده 
                  و نزدیکترین زمان انتشاراطلاعیه بعدی تغییر کند
                  یعنی تعداد  اطلاعیه در کش معتبر نیست و باید مجددا حساب شود 
                  """
                 AnnouncementViewSet.near_next_announcement_date = get_near_next_announcement_publish_date(
                 )
                 AnnouncementViewSet.last_announcement_date = get_last_announcement_publish_date(
                 )
             elif data[
                     'date_calculate'] >= AnnouncementViewSet.last_announcement_date:
                 """ 
                 تاریخ ساخته شدن کش بزرگتر از  تاریخ انتشار اخرین اطلاعیه باشد
                 یعنی تعداد اطلاعیه ها در کش معتبر است 
                 """
                 # logger_api.info("AnnouncementCount memcache", extra={
                 #     "detail": {
                 #         "user": request.user.id,
                 #     }
                 # })
                 data = {"count": data['count']}
                 return Response(data)
             else:
                 """ در این حالت اطلاعیه جدید منتشر شده و باید تعداد اطلاعیه مجددا محاسبه شود"""
                 pass
     except Exception as e:
         logger_api.error("AnnouncementCount  get cache  Error",
                          extra={"detail": {
                              "error": e,
                          }})
     # logger_api.error("AnnouncementCount calculate", extra={"detail": {"user": request.user.id, }})
     count = self.calculate_announcement_count(request)
     data = {"count": count, "date_calculate": datetime.datetime.now()}
     cache.set(cache_key, data, CACHE_TIME_ANN_COUNT)
     data = {"count": data['count']}
     return Response(data)
Esempio n. 6
0
    def get_token(self):
        post_req = {
            "username": self.username,
            "password": self.password,
        }
        get_res = requests.post(
            url=self.server_api + "/api/auth/",
            data=json.dumps(post_req),
            headers={"Content-Type": "application/json"}
        )
        if get_res.status_code == 200:
            token = get_res.json().get("token")
            return token

        else:

            logger_api.error("get_token bagheri api", extra={
                "detail": {
                    "error": get_res,
                }
            })
            return ""
Esempio n. 7
0
    def set_teachers(self, next_url=None):
        token = self.get_token()
        print(next_url)

        if next_url is None:
            get_res = requests.get(
                url=self.server_api + "/api/v1/teachers/",
                headers={"authorization": "JWT " + token}
            )

        else:
            get_res = requests.get(
                url=next_url,
                headers={"authorization": "JWT " + token}
            )

        if get_res.status_code == 200:
            results = get_res.json()
            next_url = results["next"]
            teachers = results["results"]
            # 1: coach | 2:camp | 3: county | 4:province | 5: province_f | 6:province_m | 7: country
            level_choices = {1: 4, 2: 3, 3: 8, 4: 9, 5: 6, 6: 5, 7: 7}

            list_error_teachers = []
            for _teacher in teachers:
                try:
                    teacher = Teacher.objects.filter(username=_teacher["username"])
                    if not teacher.exists():
                        teacher = Teacher()
                        teacher.username = _teacher["username"]
                        teacher.password = _teacher["username"]
                    else:
                        teacher = teacher.get()

                    teacher.first_name = _teacher["first_name"]
                    teacher.last_name = _teacher["last_name"]
                    teacher.gender = _teacher["gender"]
                    teacher.is_active = True

                    # if BaseUser.objects.filter(phone_number=_teacher["phone_number"]).exclude(id=teacher.id).exists():
                    #     list_error_teachers.append({
                    #         "teacher_phone_error": _teacher
                    #     })
                    #     continue

                    teacher.phone_number = _teacher["phone_number"]

                    teacher.save()

                    try:
                        teacher.coach_levels.clear()
                    except Exception as e:
                        print(e)

                    try:
                        for level in _teacher["levels"]:
                            teacher.coach_levels.add(level_choices[level])
                    except:
                        list_error_teachers.append({
                            "teacher_coach_levels_error": _teacher
                        })
                        continue

                    teacher.updated_at = datetime.datetime.now()
                    teacher.save()

                except Exception as e:

                    logger_api.error(
                        "teacher has error >> {0}".format(datetime.datetime.now()),
                        extra={
                            "teachers error": (_teacher, e),
                        })
                    continue

            if list_error_teachers:
                logger_api.error(
                    "teachers bagheri api {0} >> ".format(datetime.datetime.now()) + str(next_url),
                    extra={
                        "teachers error": list_error_teachers,
                    })

            if next_url:
                self.set_teachers(next_url=next_url)

            else:
                logger_api.info("Completed teachers completion.")

        elif get_res.status_code == 401:
            self.set_teachers(next_url)

        else:
            logger_api.error("teachers bagheri api", extra={
                "detail": {
                    "error": get_res,
                }
            })
Esempio n. 8
0
    def set_students(self, next_url=None):
        token = self.get_token()
        print(next_url)

        if next_url is None:
            get_res = requests.get(
                url=self.server_api + "/api/v1/students/?limit=100",
                headers={"authorization": "JWT " + token}
            )

        else:
            get_res = requests.get(
                url=next_url,
                headers={"authorization": "JWT " + token}
            )

        if get_res.status_code == 200:
            results = get_res.json()
            next_url = results["next"]
            students = results["results"]
            list_error_student = []
            for _student in students:
                try:
                    student = Student.objects.filter(username=_student["username"])
                    if not student.exists():
                        student = Student()
                        student.username = _student["username"]
                        student.password = _student["username"]
                    else:
                        student = student.get()

                    student.first_name = _student["first_name"]
                    student.last_name = _student["last_name"]
                    student.gender = _student["gender"]
                    student.is_active = True

                    # if BaseUser.objects.filter(phone_number=_student["phone_number"]).exclude(id=student.id).exists():
                    #     list_error_student.append({
                    #         "student_phone_error": _student
                    #     })
                    #     continue

                    student.phone_number = _student["phone_number"]
                    try:
                        school = School.objects.get(code_bagheri=_student["school_code"])
                    except:
                        list_error_student.append({
                            "school_not_find": _student
                        })
                        continue

                    student.school = school
                    student.updated_at = datetime.datetime.now()

                    student.save()

                except Exception as e:
                    logger_api.error(
                        "student has error >> {0}".format(datetime.datetime.now()),
                        extra={
                            "student error": _student,
                            "error": e,
                        })
                    continue

            if list_error_student:
                logger_api.error(
                    "student bagheri api {0} >> ".format(datetime.datetime.now()) + str(next_url),
                    extra={
                        "student error": list_error_student,
                    })

            if next_url:
                self.set_students(next_url=next_url)

            else:
                logger_api.info("Completed student completion.")

        elif get_res.status_code == 401:
            self.set_students(next_url)

        else:
            logger_api.error("province bagheri api", extra={
                "detail": {
                    "error": get_res,
                }
            })
Esempio n. 9
0
    def set_schools(self, next_url=None):
        token = self.get_token()
        print(next_url)
        students_count = {
            "1": 50,
            "2": 75,
            "3": 100,
            "4": 150,
            "5": 200,
            "6": 300,
        }

        if next_url is None:
            get_res = requests.get(
                url=self.server_api + "/api/v1/schools/?limit=100",
                headers={"authorization": "JWT " + token}
            )

        else:
            get_res = requests.get(
                url=next_url,
                headers={"authorization": "JWT " + token}
            )

        if get_res.status_code == 200:
            results = get_res.json()
            next_url = results["next"]
            schools = results["results"]
            list_error_school = []
            for _school in schools:
                school = School.objects.filter(code_bagheri=_school["code"])
                if not school.exists():
                    school = School()
                    school.code_bagheri = _school["code"]
                else:
                    school = school.get()

                school.name = _school["name"]
                school.gender = _school["gender"]
                school.student_count = students_count.get(str(_school["students_count"]), 400)
                school.anjoman_count = _school["number_of_users"]

                try:
                    school.county = County.objects.get(code=_school["county_id"])
                except:
                    list_error_school.append({
                        "school.county": _school
                    })
                    continue

                try:
                    school.province = Province.objects.get(code=_school["province_id"])
                except:
                    list_error_school.append({
                        "school.province": _school
                    })
                    continue

                # if _school["camp_id"] is not None:
                #     try:
                #         school.camp = Camp.objects.get(code=_school["camp_id"])
                #     except:
                #         list_error_school.append({
                #             "school.camp": _school
                #         })
                #         # continue

                try:
                    school.teacher = Teacher.objects.get(username=_school["teacher_id"])

                except:
                    list_error_school.append({
                        "school.Teacher": _school
                    })
                    continue

                school.updated_at = datetime.datetime.now()
                school.active = True
                school.save()

            if list_error_school:
                logger_api.error(
                    "schools bagheri api {0} >> ".format(datetime.datetime.now()) + str(next_url),
                    extra={
                        "school error": list_error_school,
                    })

            if next_url:
                self.set_schools(next_url=next_url)

            else:
                logger_api.info("Completed schools completion.")

        elif get_res.status_code == 401:
            self.set_schools(next_url)

        else:
            logger_api.error("province bagheri api", extra={
                "detail": {
                    "error": get_res,
                }
            })
Esempio n. 10
0
    def set_camps(self, next_url=None):
        token = self.get_token()
        print(next_url)

        if next_url is None:
            get_res = requests.get(
                url=self.server_api + "/api/v1/camps/",
                headers={"authorization": "JWT " + token}
            )

        else:
            get_res = requests.get(
                url=next_url,
                headers={"authorization": "JWT " + token}
            )

        if get_res.status_code == 200:
            results = get_res.json()
            next_url = results["next"]
            camps = results["results"]

            list_error_camps = []
            for _camp in camps:
                camp = Camp.objects.filter(code=_camp["camp_id"])
                if not camp.exists():
                    camp = Camp()
                    camp.slug = _camp["camp_id"]
                    camp.code = _camp["camp_id"]
                else:
                    camp = camp.get()

                try:
                    camp.county = County.objects.get(code=_camp["county_id"])
                except:
                    list_error_camps.append({
                        "camp_error": _camp
                    })
                    continue

                camp.title = _camp["name"]

                try:
                    camp.coach = Teacher.objects.get(username=_camp["teacher_id"])
                except:
                    list_error_camps.append({
                        "teacher_error": _camp
                    })
                    continue

                camp.save()

            if list_error_camps:
                logger_api.error(
                    "camps bagheri api {0} >> ".format(datetime.datetime.now()) + str(next_url),
                    extra={
                        "camp error": list_error_camps,
                    })

            if next_url:
                self.set_camps(next_url=next_url)

            else:
                logger_api.info("Completed camps completion.")

        elif get_res.status_code == 401:
            self.set_camps(next_url)

        else:
            logger_api.error("province bagheri api", extra={
                "detail": {
                    "error": get_res,
                }
            })
Esempio n. 11
0
    def set_provinces(self, next_url=None):
        token = self.get_token()
        print(next_url)

        if next_url is None:
            get_res = requests.get(
                url=self.server_api + "/api/v1/provinces/",
                headers={"authorization": "JWT " + token}
            )

        else:
            get_res = requests.get(
                url=next_url,
                headers={"authorization": "JWT " + token}
            )

        if get_res.status_code == 200:
            results = get_res.json()
            next_url = results["next"]
            provinces = results["results"]

            list_error_teacher = []
            for _province in provinces:
                province = Province.objects.filter(code=_province["code"])
                if not province.exists():
                    province = Province()
                    province.slug = _province["code"]
                    province.code = _province["code"]
                else:
                    province = province.get()

                province.title = _province["name"]
                province.save()
                province.coaches.clear()

                try:
                    if _province["teacher_id"]:
                        province.coaches.add(Teacher.objects.get(username=_province["teacher_id"]))
                except:
                    list_error_teacher.append({
                        "province_name": _province["name"],
                        "teacher_error": _province["teacher_id"]
                    })

                try:
                    if _province["teacher_id_f"]:
                        province.coaches.add(Teacher.objects.get(username=_province["teacher_id_f"]))
                except:
                    list_error_teacher.append({
                        "province_name": _province["name"],
                        "teacher_error": _province["teacher_id_f"]
                    })

                try:
                    if _province["teacher_id_m"]:
                        province.coaches.add(Teacher.objects.get(username=_province["teacher_id_m"]))
                except:
                    list_error_teacher.append({
                        "province_name": _province["name"],
                        "teacher_error": _province["teacher_id_m"]
                    })

                province.save()

            if list_error_teacher:
                logger_api.error(
                    "province bagheri api {0} >> ".format(datetime.datetime.now()) + str(next_url),
                    extra={
                        "teachers_error_not_found": list_error_teacher,
                    })

            if next_url:
                self.set_provinces(next_url=next_url)

            else:
                logger_api.info("Completed provinces completion.")

        elif get_res.status_code == 401:
            self.set_provinces(next_url)

        else:
            logger_api.error("province bagheri api", extra={
                "detail": {
                    "error": get_res,
                }
            })