def get_can_apply(self, obj): if obj.closed or not obj.apply or not obj.is_developer_ready: return False user = self.get_current_user() if user: if obj.user == user or not user.is_developer or user.pending or not profile_check( user): return False return obj.applicants.filter(id=user.id).count() == 0 and \ obj.participation_set.filter(user=user).count() == 0 return False
def get_can_contribute(self, obj): return profile_check(obj)
def save_task(self, validated_data, instance=None): current_user = self.get_current_user() if current_user and current_user.is_authenticated( ) and not instance and not profile_check(current_user): ValidationError( 'You need complete your profile before you can post tasks') if instance and 'fee' in validated_data and validated_data[ 'fee'] < instance.fee: raise ValidationError({ 'fee': 'You cannot reduce the fee for the task, Please contact [email protected] for assistance' }) skills = None participation = None milestones = None participants = None ratings = None if 'skills' in validated_data: skills = validated_data.pop('skills') if 'participation' in validated_data: participation = validated_data.pop('participation') if 'milestones' in validated_data: milestones = validated_data.pop('milestones') if 'participants' in validated_data: participants = validated_data.pop('participants') if 'ratings' in validated_data: ratings = validated_data.pop('ratings') initial_apply = True initial_closed = False initial_approved = False new_user = None is_update = bool(instance) if instance: initial_apply = instance.apply initial_closed = instance.closed initial_approved = instance.approved if not instance.closed and validated_data.get('closed'): validated_data['closed_at'] = datetime.datetime.utcnow() if not instance.paid and validated_data.get('paid'): validated_data['paid_at'] = datetime.datetime.utcnow() instance = super(TaskSerializer, self).update(instance, validated_data) else: if not current_user or not current_user.is_authenticated(): validated_data['source'] = TASK_SOURCE_NEW_USER if participation or participants: # Close applications if paticipants are provided when creating task validated_data['apply'] = False validated_data['apply_closed_at'] = datetime.datetime.utcnow() if not current_user or not current_user.is_authenticated(): # Create user and add them as the creator or task, indicate if task was unauthenticated email = self.initial_data.get('email', None) first_name = self.initial_data.get('first_name', None) last_name = self.initial_data.get('last_name', None) new_user = get_user_model().objects.create_user( username=email, email=email, password=get_user_model().objects.make_random_password(), first_name=first_name, last_name=last_name, type=USER_TYPE_PROJECT_OWNER, source=USER_SOURCE_TASK_WIZARD) if new_user: validated_data.update({'user': new_user}) user_signed_up.send(sender=get_user_model(), request=None, user=new_user) instance = super(TaskSerializer, self).create(validated_data) self.save_skills(instance, skills) self.save_participants(instance, participants) self.save_participation(instance, participation) self.save_milestones(instance, milestones) self.save_ratings(instance, ratings) if is_update: if not initial_approved and instance.approved: task_approved.send(sender=Task, task=instance) if initial_apply and not instance.apply: task_applications_closed.send(sender=Task, task=instance) if not initial_closed and instance.closed: task_closed.send(sender=Task, task=instance) else: # Triggered here instead of in the post_save signal to allow skills to be attached first # TODO: Consider moving this trigger notify_new_task.delay(instance.id, new_user=bool(new_user)) return instance