def mutate(self, info, achievement_data=None): threshold = UserMetrics( works_visited=achievement_data.threshold.works_visited, works_found=achievement_data.threshold.works_found) achievement = Achievement(title=achievement_data.title, description=achievement_data.description, points=achievement_data.points, threshold=threshold) achievement.save() return CreateAchievementMutation(achievement=achievement)
def create(name, short_description, long_description, difficulty, image_src, bg_image_src): """ Create a new user """ achievement = Achievement( name=name, short_description=short_description, long_description=long_description, difficulty=difficulty, image_src=image_src, bg_image_src=bg_image_src ) return achievement.save()
def post(self, request): status = 'ok' error = [0, ''] if request.user.is_authenticated(): id = request.POST.get('id', '') tags = request.POST.get('tags', '') text = request.POST.get('text', '') time = request.POST.get('time', '') subject = request.POST.get('subject', '') grade = request.POST.get('grade', '') try: with transaction.atomic(): t = Time.objects.get(id=time) s = Subject.objects.get(id=subject) g = Grade.objects.get(id=grade) try: if id != '' and id != 0 and id != '0': a = MAchievement.objects.get(id=id) else: raise ObjectDoesNotExist except ObjectDoesNotExist as e: a = MAchievement(tags=tags, text=text, time=t, subject=s, grade=g, user=request.user) a.save() else: a.tags = tags a.text = text a.time = t a.subject = s a.grade = g a.user = request.user a.save() except IntegrityError as e: status = 'fail' error = e.args except: status = 'fail' error = [100, 'error inesperado'] else: status = 'fail' error = [401, 'permiso denegado'] return HttpResponse(json.dumps({'status':status, 'error':error}), content_type='application/json')
def award_monthly_achievements(year, month): """ Find the user in the top spot of each activity type's leaderboard (and the "all" leaderboard) and create achievements """ activity_types = { 'all': Achievement.TYPE_ALL, 'run': Achievement.TYPE_RUN, 'ride': Achievement.TYPE_RIDE, 'walk': Achievement.TYPE_WALK, } month_start = datetime.datetime(year, month, 1) # First, let's kill the current achievement holders Achievement.objects.filter(month_start=month_start).delete() for activity_type, activity_enum in activity_types.iteritems(): leader_user_id = get_leaderboard_leader(activity_type, year=year, month=month) user = User.objects.get(id=leader_user_id) achievement = Achievement(user=user, month_start=month_start, activity_type=activity_enum) achievement.save()