示例#1
0
文件: views.py 项目: rkaregar/Divar
    def get_context_data(self, **kwargs):
        if self.request.user.is_authenticated:
            if not hasattr(self.request.user, 'member'):
                member = Member(user=self.request.user)
                member.save()
                self.request.user.member = member

        context = super().get_context_data(**kwargs)
        context['cats'] = Category.objects.filter(level=1)
        context['max_possible_page'] = max(
            1, int(math.ceil(Advertisement.objects.all().count() / PAGE_SIZE)))

        return context
示例#2
0
def associate_member(request):
    if request.method == 'POST':
        user = request.user
        try:
            member = Member.objects.get(name=request.POST['name'])
        except Exception as e:
            member = Member(name=request.POST['name'])
        member.save()
        user.userprofile.members.add(member)
        division = user.userprofile.divisions.get(name=request.POST['division_name'])
        member.divisions.add(division)
        return HttpResponse('Member created with success')
    else:
        return render(request, 'nodes/associate_member.html')
示例#3
0
def idea_voted(sender, instance, created, **kwargs):
    if created:
        # Check membership
        try:
            member = Member.objects.get(user=instance.user,
                                        company=instance.idea.company)
        except:
            Member(user=instance.user, company=instance.idea.company).save()

        # Create activity
        Activity(company=instance.idea.company,
                 idea=instance.idea,
                 user=instance.user,
                 action='idea_wanted').save()
        update_last_idea_activity(instance.idea)

        # Subscribe to this idea
        subscription, created = Subscription.objects.get_or_create(
            idea=instance.idea, user=instance.user)
        # Create notifications
        create_notification(instance.user, instance.idea, 'idea_wanted')
        # Send idea wanted email
        send_want_email(instance)

    update_vote_count(instance.idea)
示例#4
0
    def form_valid(self, form):
        form.instance.inventory = get_object_or_404(
            Inventory, loans=True, name__iexact=self.kwargs['inv'])

        response = super().form_valid(form)

        notify_bulk(
            Member.users_with_perm(PERMS.inventory.view_loan),
            NotifType.LOAN_REQUESTS,
            f"A new loan has been requested for {form.instance.requester.username} by {self.request.user.username}.",
            reverse('inventory:loan_detail',
                    kwargs={
                        'inv': self.object.inventory.canonical_(),
                        'pk': self.object.id
                    }),
            merge_key=self.object.id)

        notify(
            form.instance.requester, NotifType.LOAN_REQUESTS,
            f"A loan request has been created for you by {self.request.user.username}.",
            reverse('inventory:loan_detail',
                    kwargs={
                        'inv': self.object.inventory.canonical_(),
                        'pk': self.object.id
                    }))

        return response
示例#5
0
def index(request):
    if not Member.objects.filter(user=request.user):
        member = Member(user=request.user, display_name=request.user.username)
        member.save()

    member = request.user.member
    member.remove_expired_rewards

    featured_games = Game.objects.filter(featured=True)
    rewards = Reward.objects.filter(member=member, purchase__isnull=True)
    recommended_games = member.get_recommended_games()

    return render(
        request, 'games/index.html', {
            'featured_games': featured_games,
            'rewards': rewards,
            'recommended_games': recommended_games,
            'member': member
        })
示例#6
0
def make_members(form, electee):
    context = {
        'error': False
    }
    uniqnames = form.cleaned_data.get('new_members').split(',')
    try:
        # validate each submitted uniqname to make sure that a member
        # 	with that uniqname does not alread exist, and that it is
        # 	alphabetic and a valid number of characters
        for name in uniqnames:
            if Member.objects.filter(uniqname=name).exists():
                raise MyError('Uniqname already exists')
    except MyError:
        context = {
            'error': True,
            'error_msg': 'Uniqname ' + name + ' alread exists! None added.'
        }
    else:
        # display message saying members were successfully submitted
        mail_list = []
        for name in uniqnames:
            m = Member(uniqname=name)
            if electee:
                m.status = 'E'
            else:
                m.status = 'A'
            m.save()

            if electee:
                Electee(member=m).save()

            subject = '[HKN] Welcome to the HKN Beta Epsilon Website'
            message = welcome_msg
            from_email = settings.EMAIL_HOST_USER
            to_email = [name + '@umich.edu']

            email = EmailMessage(subject, message, from_email)
            email.to = to_email
            email.cc = [from_email]
            email.send()

    return context
示例#7
0
    def test_get_chat(self):
        chat = ChatFactory()
        chat.save()
        Member(user=self.user, chat=chat).save()
        for i in range(30):
            MessageFactory(users=self.user, chat=chat).save()

        self.contacts_url = reverse('get_chat', args=[chat.id])

        response = self.client.get(self.contacts_url)
        self.assertEquals(response.status_code, 200)

        self.assertEquals(len(json.loads(response.json())['messages']), 30)
示例#8
0
 def post(self, request):
     message = self.get_object()
     comment = self.request.POST.get('comment', default="")
     MessageReport.objects.create(member=self.request.user.member,
                                  message=message,
                                  comment=comment)
     django_add_message(
         request, django_message.SUCCESS,
         "A report has been sent. An admin should review this message soon")
     notify_bulk(
         Member.users_with_perm(PERMS.messaging.can_moderate),
         NotifType.OTHER,
         f"A message has been reported for moderation by {request.user.member.username}",
         reverse('message:message_thread_full', args=[message.thread_id]))
     return HttpResponseRedirect(
         reverse('message:message_thread', kwargs={'pk':
                                                   message.thread_id}))
    def get_login_redirect_url(self, request):
        referer = request.META.get('HTTP_REFERER')

        # My apologies for this disgusting hack
        company = None
        try:
            company_slug = referer.split('//')[1].split('/')[1]
            company = Company.objects.get(slug=company_slug)
        except:
            pass

        if company:
            # check membership
            try:
                Member.objects.get(company=company, user=request.user)
            except:
                Member(company=company, user=request.user).save()
        return referer
示例#10
0
    def create(self, validated_data):
        passwd = validated_data.pop('password', None)
        user_name = validated_data.pop('username', None)
        mail = validated_data.pop('email', None)
        nick = validated_data.pop('nick_name', None)

        does_exist = Member.objects.filter(nick_name__iexact=nick)
        if len(does_exist) > 0:
            return

        instance = Member(email=mail, username=user_name, nick_name=nick)
        if passwd is not None:
            instance.set_password(passwd)
        instance.save()
        social = MemberInfo.objects.create(belongs_to=instance)
        return instance
示例#11
0
def comment_added(sender, instance, **kwargs):

    # Check membership
    try:
        member = Member.objects.get(user=instance.created_by,
                                    company=instance.idea.company)
    except:
        Member(user=instance.created_by, company=instance.idea.company).save()

    # Create activity
    Activity(company=instance.idea.company,
             idea=instance.idea,
             user=instance.created_by,
             comment=instance,
             action='comment_added').save()
    update_last_idea_activity(instance.idea)
    update_comment_count(instance.idea)

    # Subscribe to this idea
    subscription, created = Subscription.objects.get_or_create(
        idea=instance.idea, user=instance.created_by)

    # Create notifications
    create_notification(instance.created_by, instance.idea, 'comment_added',
                        instance)

    # Send push notification
    new_comment = {}
    new_comment['author'] = instance.created_by.get_full_name()
    new_comment['author_id'] = str(instance.created_by.id)
    new_comment['comment'] = instance.comment
    # MD5 email
    new_comment['email'] = hashlib.md5(instance.created_by.email).hexdigest()
    push_event(instance.idea, 'new_comment', new_comment)

    # Shoot emails
    send_comment_emails(instance)
示例#12
0
def make_members(form, electee):
    context = {}
    uniqnames = form.cleaned_data.get('new_members').split(',')
    try:
        # validate each submitted uniqname to make sure that a member
        # 	with that uniqname does not alread exist, and that it is
        # 	alphabetic and a valid number of characters
        for name in uniqnames:
            if Member.objects.filter(uniqname=name).exists():
                raise MyError('Uniqname already exists')
    except MyError:
        context = {
            'error': True,
            'error_msg': 'Uniqname ' + name + ' alread exists! None added.'
        }
    else:
        # display message saying members were successfully submitted
        mail_list = []
        for name in uniqnames:
            m = Member(uniqname=name)
            if electee:
                m.status = 'E'
            else:
                m.status = 'A'
            m.save()

            if electee:
                Electee(member=m).save()

            subject = '[HKN] Welcome to the HKN Beta Epsilon Website'
            message = welcome_msg
            from_email = settings.EMAIL_HOST_USER
            to_email = [name + '@umich.edu']

            email = EmailMessage(subject, message, from_email)
            email.to = to_email
            email.cc = [from_email]
            email.send()

    return context
示例#13
0
 def get_object(self, queryset=None):
     if not hasattr(self.request.user, 'member'):
         self.request.user.member = Member(user=self.request.user)
     return self.request.user.member
示例#14
0
def init_member(license):
    Member.load_on_migrate(license)
    print("member worked!!")